JAVA中常用时间类JDK8新增时间类

JDK8新增时间类

1、ZoneId时区

(1)、方法
    static Set<string> getAvailableZoneIds()    //	获取Java中支持的所有时区
    static ZoneId systemDefault()               //	获取系统默认时区
    static ZoneId of(String zoneId)             //	获取一个指定时区
(2)、例子
public static void main(String[] args) {
    //  获取java支持的所有时区
    Set<String> zoneIds = ZoneId.getAvailableZoneIds();
    System.out.println(zoneIds.size());//目前版本有601个时区 版本不同时区个数不同
    System.out.println(zoneIds);       //[Asia/Aden, America/Cuiaba,....US/Pacific, Europe/Monaco]
    //  获取系统默认时区:Asia/Shanghai
    ZoneId zoneId = ZoneId.systemDefault();
    System.out.println(zoneId);
    //  获取一个指定时区
    ZoneId of = ZoneId.of("SystemV/EST5EDT");
    System.out.println(of);             //SystemV/EST5EDT  目前不知道有啥用
}

2、Instant时间戳

(1)、方法
static Instant now()                        	//	获取当前时间的Instant对象(标准时间)
static Instant ofxxxx(long epochMilli)      	//	根据 (秒/毫秒/纳秒)获取Instant对象
    ZonedDateTime atZone(ZoneId zone)           //	指定时区
    boolean isXxx(Instant otherInstant)         //	判系列的方法
    Instant minusXxx(long millisToSubtract)     //	减少时间系列的方法
    Instant plusXxx(long millisToSubtract)      //	增加时间系列的方法
(2)、例子
public static void main(String[] args) {
    //  获取当前时间的Instant对象(标准时间)
    Instant nowTime = Instant.now();
    System.out.println(nowTime);                                    //  2023-01-17T11:19:49.729922200Z
    //  根据 (秒/毫秒/纳秒)获取Instant对象
    Instant instant1 = Instant.ofEpochMilli(1000L);                 //  参数是毫秒
    System.out.println(instant1);                                   //  1970-01-01T00:00:01Z
    Instant instant2 = Instant.ofEpochSecond(2L);                   //  参数是秒
    System.out.println(instant2);                                   //  1970-01-01T00:00:02Z
    Instant instant3 = Instant.ofEpochSecond(1L, 1000000000);   //参数是(秒,微妙)
    System.out.println(instant3);                                   //  1970-01-01T00:00:02Z
    //  指定时区  创建一个带有时区的对象ZonedDateTime
    ZonedDateTime time = Instant.now().atZone(ZoneId.of("Asia/Shanghai"));
    System.out.println(time);                                       //  2023-01-17T19:19:49.734909+08:00[Asia/Shanghai]
    //  判系列的方法
    Instant instant4 = Instant.ofEpochSecond(1L);
    Instant instant5 = Instant.ofEpochSecond(2L);
    //  判断instant4时间是否在instant5之前
    boolean before = instant4.isBefore(instant5);
    System.out.println(before);                                     //  true
    //  判断instant5时间是否在instant4之后
    boolean after = instant5.isAfter(instant4);
    System.out.println(after);                                      //  true
    //  Instant minusXxx(long millisToSubtract)     减少时间系列的方法
    Instant instant6 = Instant.ofEpochSecond(2L);
    System.out.println(instant6);                                   //1970-01-01T00:00:02Z
    //  减一秒
    Instant instant7 = instant6.minusSeconds(1);
    System.out.println(instant7);                                   //1970-01-01T00:00:01Z
    //  +2秒
    Instant instant8 = instant6.plusSeconds(2L);
    System.out.println(instant8);                                   //1970-01-01T00:00:04Z
}

3、ZonedDateTime类

(1)、方法
//  static ZonedDateTime now()                  获取当前时间的ZonedDateTime对象
//  static ZonedDateTime ofXxxx(...)            获取指定时间的ZonedDateTime对象
//  ZonedDateTime   withXxx(时间)                修改时间系列的方法
//  ZonedDateTime   minusXxx(时间)               减少时间系列的方法
//  ZonedDateTime   plusXxx(时间)                增加时间系列的方法
(2)、例子
public static void main(String[] args) {
    //  获取一个  ZonedDateTime一个对象
    ZonedDateTime now = ZonedDateTime.now();
    System.out.println(now);                    //  2023-01-17T21:42:03.007920300+08:00[Asia/Shanghai]
    //  获取指定时间的ZonedDateTime对象:
    ZonedDateTime zdt1 = ZonedDateTime.of(1999, 9, 9,
            9, 9, 9, 0,
            ZoneId.of("Asia/Shanghai"));
    System.out.println(zdt1);                   //  1999-09-09T09:09:09+08:00[Asia/Shanghai]
    //  获取这个Asia/Shanghai时区的原始时间+60秒也就是+1分钟
    Instant instant = Instant.ofEpochSecond(10L);
    ZoneId zid = ZoneId.of("Asia/Shanghai");
    ZonedDateTime zdt2 = ZonedDateTime.ofInstant(instant, zid);
    System.out.println(zdt2);                  //  1970-01-01T08:00:10+08:00[Asia/Shanghai]
    
    //  修改zdt1的年
    ZonedDateTime zdt3 = zdt1.withYear(2000);
    System.out.println(zdt3);                  //   2000-09-09T09:09:09+08:00[Asia/Shanghai]
    //  减少一个月
    ZonedDateTime zdt4 = zdt1.minusMonths(1);
    System.out.println(zdt4);                  //   1999-08-09T09:09:09+08:00[Asia/Shanghai]
    //  增加一年
    ZonedDateTime zdt5 = zdt1.plusYears(1);    //   2000-09-09T09:09:09+08:00[Asia/Shanghai]
    System.out.println(zdt5);
}

细节

  • JDK8新增的时间对象都是不可变的
  • 如果我们修改了,减少了,增加时间了
  • 那我们的调用者是不会发生改变的,产生一个新的时间

4、DateTimeFormatter

(1)方法
//  public static DateTimeFormatter ofPattern(String pattern)       获取格式化对象
//  public String format(TemporalAccessor temporal)                 按照指定格式进行格式化
(2)、例子
public static void main(String[] args) {
    DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss EE a");
    //  获取一个时区时间对象
    ZonedDateTime time1 = ZonedDateTime.now();
    //  解析,并格式化时间对象并输出
    System.out.println(dtf1.format(time1));             //  2023-01-17 22:14:55
}

5、LocalDate、LocalTime、LocalDateTime

(1)、方法

LocalDate:只能获取年、月、日———LocalTime只能获取时、分、秒——LocalDateTime都可以获取到

在这里插入图片描述

(2)、LocalDate例子
public static void main(String[] args) {
    //  创建一个LocalDate类型的现在时间的时间对象
    LocalDate time1 = LocalDate.now();
    System.out.println(time1);                              //  2023-01-17
    //  创建一个LocalDate类型的时间对象
    LocalDate time2 = LocalDate.of(2000, 12, 12);
    System.out.println(time2);                              //  2000-12-12

    // 获取其中的年月日
    int year = time1.getYear();
    System.out.println("time1的年份:" + year);                // time1的年份:2023
    Month month = time1.getMonth();
    System.out.println("time1的月份:" + month);               // time1的月份:JANUARY
    System.out.println("time1的月份:" + month.getValue());    // time1的月份:1
    //  另外一种直接获取到阿拉伯数字的方法
    int monthValue = time1.getMonthValue();
    System.out.println("time1的月份:" + monthValue);          // time1的月份:1
    int dayOfMonth = time1.getDayOfMonth();
    System.out.println("现在是这个月的第 " + dayOfMonth + "天");  // 现在是这个月的第 17天
    //  比较两个时间的前后
    if (time1.isAfter(time2)) {                              //time1在time2的时间后面
        System.out.println("time1在time2的时间后面");
    } else {
        System.out.println("time1在time2的时间前面");
    }
    //  time1的时间是2023-01-17
    //  修改时间with  年数修改为2000
    LocalDate withLocalDate = time1.withYear(2000);
    System.out.println(withLocalDate);                          //  2000-01-17
    //  减少时间minus   减少一个年
    LocalDate minusYears = time1.minusYears(1);                 //  2022-01-17
    System.out.println(minusYears);
    //  增加时间plus    增加一个月
    LocalDate plusMonths = time1.plusMonths(1);                 //  2023-02-17
    System.out.println(plusMonths);
}

其他LocalTime、LocalDateTime类似不举例子了

6、时间间隔Duration、Period、ChronoUnit主要是计算两个时间的时间间隔

  • Duration:用于计算两个“时间”间隔(秒,纳秒)
  • Period:用于计算两个“日期”间隔(年、月、日)
  • ChronoUnit:用于计算两个“日期”间隔

7、综合小练习

计算一个人活了多少天了 例如:2000年11月11日;出生

public static void main(String[] args) throws ParseException {
    //  JDK7
    //  计算一个人活了多少天了 例如:2000年11月11日;出生
    //  获取当前时间
    String birthday = "2000年11月11日";
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
    long now = System.currentTimeMillis();
    //  解析字符串获取Date对象,然后获取毫秒值
    Date parse = sdf.parse(birthday);
    long time = parse.getTime();
    //  计算天数
    long days = (now - time) / 1000 / 60 / 60 / 24;
    System.out.println(days);//8103

    //  JDK8
    LocalDate birthdayTime = LocalDate.of(2000, 11, 11);
    LocalDate nowTime = LocalDate.now();
    long lifeDays = ChronoUnit.DAYS.between(birthdayTime, nowTime);
    System.out.println(lifeDays);//8103
}

计算某一年是不是闰年:这次的判断条件是,年数366天的是闰年,月份有29天的是闰年

public static void main(String[] args) {
    //  JDK7
    //  把时间设置为2000年3月1号
    Calendar instance = Calendar.getInstance();
    instance.set(2000, 2, 1);         //month的取值范围是0~11
    instance.add(Calendar.DAY_OF_MONTH, -1);
    int dayOfMonth = instance.get(Calendar.DAY_OF_MONTH);
    System.out.println(dayOfMonth);                    //29
    //  JDK8
    LocalDate of = LocalDate.of(2000,3,1);
    LocalDate localDate = of.minusDays(1);
    int dayOfMonth1 = localDate.getDayOfMonth();
    System.out.println(dayOfMonth1);                    //29
    //  LocalDate有一个直接判断闰年平年的方法
    boolean leapYear = of.isLeapYear();
    System.out.println(leapYear);//ture是闰年,false是平年
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值