Java8新特性之日期处理

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zl_StepByStep/article/details/82732337

     在Java8之前,操作日期的话不是很方便,有很多地方需要自己去实现,Java8中在java.time包里新增了很多处理日期的类,通过这些类可以使开发者更加便捷的操作日期,这些类都是final修饰的,且都是线程安全的。

下面介绍几个常用类:

LocalDate类

只能处理日期

public class TestLocalDate {
    public static void main(String[] args) {
        //获取今天的日期
        LocalDate date1 = LocalDate.now();
        System.out.println(date1);  // 2018-09-17

        int year = date1.getYear(); //获取年份
        int month = date1.getMonthValue(); //获取月份
        int day = date1.getDayOfMonth();  //获取日

        //日期格式化
        String date2 = date1.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日"));
        System.out.println(date2); //2018年09月17日

        //是否是闰年
        boolean leap = date1.isLeapYear();
        //当前月有多少天
        int len = date1.lengthOfMonth();

        //自定义日期的几种方式
        LocalDate date3 = LocalDate.parse("2019-06-30");
        LocalDate date4 = LocalDate.of(2019,6,30);

        //判断两个日期是否相等
        if(date3.equals(date4)) {
            System.out.println("日期相等");
        }

        //1周后的日期
        LocalDate date5 = date1.plus(1, ChronoUnit.WEEKS);
        System.out.println("一周后的日期:"+date5); //一周后的日期:2018-09-24
    }
}

 

LocalTime类

只能处理时分秒

public class TestLocalTime {
    public static void main(String[] args) {
        //获取当前时分毫秒
        LocalTime time1 = LocalTime.now();
        //获取当前时分秒,不包含毫秒
        LocalTime time2 = LocalTime.now().withNano(0);
        System.out.println(time1 + "\t" + time2);  //22:11:08.251	22:11:08

        //往后顺延一小时
        LocalTime time3 = time1.plusHours(1);
        System.out.println(time3);  //23:11:08.251

        //自定义时间
        LocalTime time4 = LocalTime.parse("06:48:59");
        LocalTime time5 = LocalTime.of(6,48,59);
        System.out.println(time4 +"\t" + time5);  //06:48:59	06:48:59 
    }
}

LocalDateTime类

//可以处理日期和时间
public class TestLocalDateTime {
    public static void main(String[] args) {
        LocalDateTime date1 = LocalDateTime.now();
        System.out.println(date1);  //2018-09-17T22:19:39.679

        LocalDateTime date2 = LocalDateTime.of(2018,12,12,11,59,59);
        System.out.println(date2); //2018-12-12T11:59:59
    }
}

注:LocalDate、LocalTime、LocalDateTime三个类实现的接口差不多都一样,所以有很多方法都差不多:

Period类和Duration类

Period类处理LocalDate,Duration类 处理LocalTime。

public class TestDurationPeriod {
    public static void main(String[] args) {
        LocalDate date1 = LocalDate.of(2018,9,9);
        LocalDate date2 = LocalDate.of(2018,9,11);
        //Period处理LocalDate
        Period period = Period.between(date1, date2);  //两个date相差2天
        System.out.println(period.getDays());  //2

        LocalTime time1 = LocalTime.of(20,1,0);
        LocalTime time2 = LocalTime.of(20,3,0);
        //Duration处理LocalTime
        Duration duration = Duration.between(time1,time2); //两个time相差120秒
        System.out.println(duration.getSeconds()); //120
    }
}

计算出生时间:

LocalDate birthday = LocalDate.of(1996,10,06);
LocalDate today = LocalDate.now();
Period period = Period.between(birthday,today);
System.out.println("我出生"+period.getYears()+"年"+period.getMonths()+
        "个月"+period.getDays()+"天了"); //我出生21年11个月12天了
LocalDate date = LocalDate.of(1996, 10, 6);
LocalTime time = LocalTime.of(22, 20, 00);

LocalDateTime birthday = LocalDateTime.of(date, time);
LocalDateTime now = LocalDateTime.now();
Duration between = Duration.between(birthday, now);

System.out.println("我出生:" + between.toDays() + "天了"); //我出生:8017天了

设定时区

ZonedDateTime类:获取本地的时区时间、 ZoneId:设定时区、 ZonedDateTime:获取指定时区的事件

public class TestZone {
    public static void main(String[] args) {
        //获取本地的时区时间
        ZonedDateTime now = ZonedDateTime.now();
        System.out.println(now);  //2018-09-17T23:09:11.202+08:00[Asia/Shanghai]

        //设定时区为美国洛杉矶
        ZoneId zone = ZoneId.of("America/Los_Angeles");

        //获取指定的时区时间
        ZonedDateTime usa = ZonedDateTime.now(zone);
        System.out.println(usa);  //2018-09-17T08:09:11.202-07:00[America/Los_Angeles]
    }
}

猜你喜欢

转载自blog.csdn.net/zl_StepByStep/article/details/82732337