Java 新的日期时间 API

Java 新的日期时间 API

Java8提供了如下几个时间API

时间相关类介绍
LocalDateTime时间处理类,最高精确到纳秒
LocalDate时间处理类,最高精确到天
DateTimeFormatter时间格式化
ZoneId时区设置类

示例:LocalDateTime转Long型值

public static void main(String[] args) throws Exception {
    String str = "20201231121212";
    String pattern = "yyyyMMddHHmmss";

    long res1 = new SimpleDateFormat(pattern).parse(str).getTime();
    System.out.println(res1);

    LocalDateTime ldt = LocalDateTime.parse(str, DateTimeFormatter.ofPattern(pattern));
    Long newSecond = ldt.toEpochSecond(ZoneOffset.of("+8")) * 1000;
    System.out.println(newSecond);
}

结果:
在这里插入图片描述

示例:时间戳转日期类型的字符串

public static String long2str(long dateLong, String pattern) {
    LocalDateTime ldt =LocalDateTime.ofInstant(Instant.ofEpochMilli(dateLong), ZoneId.systemDefault());
    String res = ldt.format(DateTimeFormatter.ofPattern(pattern));
    return res;
}
public static void main(String[] args) throws Exception {
    String str = "20201231121212";
    String pattern = "yyyyMMddHHmmss";

    long longTime = new SimpleDateFormat(pattern).parse(str).getTime();
    System.out.println(longTime);
    String res = long2str(longTime, pattern);
    System.out.println(res);
}

示例1 :获取当前时间

@Test
public void testNowTime() {
    // 获取当前日期
    LocalDate localDate = LocalDate.now();
    System.out.println("当前日期:" + localDate);
    System.out.println("当前日期:" + localDate.getYear() + "-" + localDate.getMonthValue() + "-" + localDate.getDayOfMonth());

    // 获取当天时间
    LocalTime localTime = LocalTime.now();
    System.out.println("当天时间:" + localTime);
    System.out.println("当天时间:" + localTime.getHour() + ":" + localTime.getMinute() + ":" + localTime.getSecond());

    // 当前精确时间
    LocalDateTime now = LocalDateTime.now();
    System.out.println("当前精确时间:" + now);
    System.out.println("当前精确时间:" + now.getYear() + "-" + now.getMonthValue() + "-" + now.getDayOfMonth() + " " + now.getHour() + "-" + now.getMinute() + "-" + now.getSecond());

    // 有时区的当前精确时间
    ZonedDateTime nowZone = LocalDateTime.now().atZone(ZoneId.systemDefault());
    System.out.println("当前精确时间(有时区):" + nowZone);
    System.out.println("当前精确时间(有时区):" + nowZone.getYear() + "-" + nowZone.getMonthValue() + "-" + nowZone.getDayOfMonth() + " " + nowZone.getHour() + "-" + nowZone.getMinute() + "-" + nowZone.getSecond());
}

结果:
在这里插入图片描述

示例2:创建指定的本地化日期时间

 @Test
    public void dateAPI() {
        LocalDate localDate = LocalDate.of(1999,9,21);
        System.out.println(localDate);
        System.out.println(localDate.getYear());
        System.out.println(localDate.getMonth());
        System.out.println(localDate.getDayOfMonth());
        LocalDate localDateParse = LocalDate.parse("1840-01-01");
        System.out.println(localDateParse.getYear());

        LocalTime localTime = LocalTime.of(3, 20);
        System.out.println(localTime);
        System.out.println(localTime.getHour());
        System.out.println(localTime.getMinute());
        LocalTime localTimeParse = LocalTime.parse("11:35");
        System.out.println(localTimeParse.getHour());

        LocalDateTime localDateTime1 = LocalDateTime.of(1999, 9, 21, 12, 12, 12);
        System.out.println(localDateTime1);
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDateTime);
        LocalDate localDate1 = localDateTime.toLocalDate();
        System.out.println(localDate1);
        LocalTime localTime1 = localDateTime.toLocalTime();
        System.out.println(localTime1);
        LocalDateTime localDateTimeParse = LocalDateTime.parse("2999-09-21T11:44:11");
        System.out.println(localDateTimeParse.getMinute());
    }

示例3:使用时区的日期时间API:ZoneId 、ZonedDateTime

@Test
public void zonedDateTime(){
    ZoneId currentZone = ZoneId.systemDefault();
    System.out.println("当期时区: " + currentZone);

    // 获取当前时间日期
    ZonedDateTime date1 = ZonedDateTime.parse("2015-12-03T10:15:30+05:30[Asia/Shanghai]");
    System.out.println("date1: " + date1);

    LocalDateTime localDateTime = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
    System.out.println(localDateTime);
    LocalDateTime localDateTime11 = LocalDateTime.now(ZoneId.of("Europe/Paris"));
    System.out.println(localDateTime11);
}

示例4:时间格式化

@Test
public void testFormat() {
    LocalDateTime now = LocalDateTime.now();
    System.out.println("当前时间:" + now);
    System.out.println("格式化后:" + now.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
    System.out.println("格式化后:" + now.format(DateTimeFormatter.ISO_LOCAL_DATE));
    System.out.println("格式化后:" + now.format(DateTimeFormatter.ISO_LOCAL_TIME));
    System.out.println("格式化后:" + now.format(DateTimeFormatter.ofPattern("YYYY-MM-dd hh:mm:ss")));
}

结果:
在这里插入图片描述

示例5:时间比较

@Test
public void testComp() {
    LocalDateTime now = LocalDateTime.now();
     LocalDateTime after = LocalDateTime.of(1999,9,21,12,12,12);
    System.out.println(now + " 在 " + after + " 之后吗? " + now.isAfter(after));
    System.out.println(now + " 在 " + after + " 之前吗? " + now.isBefore(after));

    // 时间差
    long day = after.until(now, ChronoUnit.DAYS);
    System.out.println("相差天数: " + day);
    long month = after.until(now, ChronoUnit.MONTHS);
    System.out.println("相差月份: " + month);
    long hours = after.until(now, ChronoUnit.HOURS);
    System.out.println("相差小时: " + hours);
    long minutes = after.until(now, ChronoUnit.MINUTES);
    System.out.println("相差分钟: " + minutes);

    // 距离JDK 14 发布还有多少天?
    LocalDate jdk14 = LocalDate.of(2020, 11, 17);
    LocalDate nowDate = LocalDate.now();
    System.out.println("距离JDK 14 发布还有:" + nowDate.until(jdk14, ChronoUnit.DAYS) + "天");
}

结果:
在这里插入图片描述

示例6:时间加减

@Test
public void testCalculate() {
    LocalDateTime now = LocalDateTime.now();
    System.out.println("当前时间是:"+now);
    LocalDateTime plusTime = now.plusMonths(1).plusDays(2).plusHours(12).plusMinutes(12).plusSeconds(12);
    System.out.println("增加1月2天12小时12分钟12秒时间后:" + plusTime);
    LocalDateTime minusTime = now.minusMonths(2);
    System.out.println("减少2个月时间后:" + minusTime);
}

结果:
在这里插入图片描述

示例7:其它方法

@Test
public void timeFunctionTest() {
    LocalDateTime now = LocalDateTime.now();
    System.out.println("当前时间:" + now);
    // 第十天
    LocalDateTime firstDay = now.withDayOfMonth(10);
    System.out.println("本月第一天:" + firstDay);
    // 最后一天
    LocalDateTime lastDay = now.with(TemporalAdjusters.lastDayOfMonth());
    System.out.println("本月最后一天:" + lastDay);
    // 是否闰年
    System.out.println("今年是否闰年:" + Year.isLeap(now.getYear()));
}

结果
在这里插入图片描述


新旧日期转换:(了解会用就行,不要求掌握)


示例3:java.util.Date 转 java.time.LocalDateTime

public LocalDateTime date2LocalDateTime(Date date) {
    Instant instant = date.toInstant();
    ZoneId zone = ZoneId.systemDefault();
    LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
    return  localDateTime;
}

示例4:java.util.Date 转 java.time.LocalDate

public LocalDate date2LocalDate(Date date) {
    Instant instant = date.toInstant();
    ZoneId zone = ZoneId.systemDefault();
    LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
    LocalDate localDate = localDateTime.toLocalDate();
    return localDate;
}

示例5:java.util.Date 转 java.time.LocalTime

public LocalTime date2LocalTime(Date date) {
    Instant instant = date.toInstant();
    ZoneId zone = ZoneId.systemDefault();
    LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
    LocalTime localTime = localDateTime.toLocalTime();
    return localTime;
}
 

示例6:java.time.LocalDateTime 转 java.util.Date

public Date LocalDateTime2Date(LocalDateTime localDateTime) {
    ZoneId zone = ZoneId.systemDefault();
    Instant instant = localDateTime.atZone(zone).toInstant();
    Date date = Date.from(instant);
    return date;
}

示例7: java.time.LocalDate转 java.util.Date

public Date LocalDate2Date(LocalDate localDate) {
    ZoneId zone = ZoneId.systemDefault();
    Instant instant = localDate.atStartOfDay().atZone(zone).toInstant();
    Date date = Date.from(instant);
    return date;
}

示例8: java.time.LocalTime转 java.util.Date

public Date LocalTime2Date(LocalTime localTime) {
    LocalDate localDate = LocalDate.now();
    LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime);
    ZoneId zone = ZoneId.systemDefault();
    Instant instant = localDateTime.atZone(zone).toInstant();
    Date date = Date.from(instant);
    return date;
}
相关推荐
©️2020 CSDN 皮肤主题: 酷酷鲨 设计师:CSDN官方博客 返回首页