Java常用API----JDK8常用时间类,时区和格式化

1.ZoneId时区:方法:

static Set<String> getAvailableZoneIds()------>获取Java中支持的所有时区

static ZoneId systemDefault()------->获取Java中支持的所有时区

static ZoneId of(String zoneId)-------->获取一个指定的时区

代码演示:

package a08datedemo;

import java.time.ZoneId;
import java.util.Set;

public class ZoneIdDemo1 {
    public static void main(String[] args) {
        //1.获取所有的时区名称
        Set<String> zoneIds = ZoneId.getAvailableZoneIds();
        System.out.println(zoneIds.size());//600  :一共有600个时区
        System.out.println(zoneIds);
        //2.获取当前系统的默认时区
        ZoneId zoneId = ZoneId.systemDefault();
        System.out.println(zoneId);//Asia/Shanghai
        //3.获取指定的时区
        ZoneId zoneId1 = ZoneId.of("Africa/Nairobi");
        System.out.println(zoneId1);//Africa/Nairobi

    }
}

运行结果:

2.Instant时间戳:方法:

static Instant now()------------>获取当前时间的Instant对象(标准时间)

static Instant ofXxxx(long epochMilli)------------>根据(秒/毫秒/纳秒)获取Instant对象

ZoneDateTime atZone(ZoneId zone)------------>指定时区

boolean isXxx(Instant otherInstant)------------>判断系列的方法

Instant minusXxx(long millisToSubtract)------------>减少时间系列的方法

Instant plusXxx(long millisToSubtract)------------>增加时间系列的方法

代码演示:

package a08datedemo;

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class InstantDemo1 {
    public static void main(String[] args) {
        //1.获取当前时间的Instant对象(标准时间)
//        Instant now = Instant.now();
//        System.out.println(now);//2024-03-25T02:20:21.636813800Z

        //2.根据(秒/毫秒/纳秒)获取Instant对象
        Instant instant1 = Instant.ofEpochMilli(0L);
        System.out.println(instant1);//1970-01-01T00:00:00Z

        Instant instant2 = Instant.ofEpochSecond(1L);
        System.out.println(instant2);//1970-01-01T00:00:01Z

        Instant instant3 = Instant.ofEpochSecond(1L, 1000000000L);
        System.out.println(instant3);//1970-01-01T00:00:02Z

        //3.制定时区
        ZonedDateTime time = Instant.now().atZone(ZoneId.of("Asia/Shanghai"));
        System.out.println(time);//2024-03-25T10:26:56.498282+08:00[Asia/Shanghai]

        //4.判断
        Instant instant4=Instant.ofEpochMilli(0L);
        Instant instant5=Instant.ofEpochMilli(1000L);
        //isBefore:判断调用者代表的时间是否在参数表示时间的前面
        boolean result1 = instant4.isBefore(instant5);
        System.out.println(result1);//true:instant4在instant5前面
        //isAfter:判断调用者代表的时间是否在参数表示时间的后面
        boolean result2 = instant4.isAfter(instant5);
        System.out.println(result2);//false:instant4不在instant5后面

        //6.减少时间系列的方法
        Instant instant6 = Instant.ofEpochMilli(3000L);
        System.out.println(instant6);//1970-01-01T00:00:03Z
        Instant instant7 = instant6.minusSeconds(1);//往前减一秒
        System.out.println(instant7);//1970-01-01T00:00:02Z


    }
}

运行结果:

3.ZoneDateTime带时区的时间:方法:

static ZoneDateTime now()---------->获取当前时间的ZoneDateTime对象

static ZoneDateTime ofXxxx(...)---------->获取指定时间的ZoneDateTime对象

ZoneDateTime withXxx(时间)---------->修改时间系列的方法

ZoneDateTime minusXxx(时间)---------->减少时间系列的方法

ZoneDateTime plusXxx(时间)---------->增加时间系列的方法

代码演示:

package a08datedemo;

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class ZoneDateTimeDemo1 {
    public static void main(String[] args) {
        //1.获取当前时间对象(带时区)
        ZonedDateTime now = ZonedDateTime.now();
        System.out.println(now);

        //2.获取指定时间对象(带时区)
        //年月日时分秒纳秒方式指定
        ZonedDateTime time1=ZonedDateTime.of(2023,10,1,
                11,12,12,0,
                ZoneId.of("Asia/Shanghai"));
        System.out.println(time1);
        //通过Instant+时区的方式指定获取时间对象
        Instant instant=Instant.ofEpochMilli(0L);
        ZoneId zoneId=ZoneId.of("Asia/Shanghai");
        ZonedDateTime time2=ZonedDateTime.ofInstant(instant,zoneId);
        System.out.println(time2);

        //3.修改时间系列的方法
        ZonedDateTime time3 = time2.withYear(2000);
        System.out.println(time3);

        //4.减少时间:
        ZonedDateTime time4 = time3.minusYears(1);
        System.out.println(time4);
    }
}

运行结果:

4.DateTimeFormatter用于时间的格式化和解析:方法:

static DateTimeFormatter ofPattern(格式)--------->获取格式对象

String format(时间对象)---------->按照指定方式格式化

代码演示:

package a08datedemo;

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class DateTimeFormatterDemo1 {
    public static void main(String[] args) {
        ZonedDateTime time = Instant.now().atZone(ZoneId.of("Asia/Shanghai"));
        //解析/格式化器
        DateTimeFormatter dtf1=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss EE a");
        //格式化
        System.out.println(dtf1.format(time));
        
    }
}

运行结果:

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值