程序员Java学习笔记

Java8学习笔记之新日期API

2017-05-08  本文已影响76人  Joepis

摘要: 原创出处 http://peijie2016.gitee.io 欢迎转载,保留摘要,谢谢!

Java8对日期API做了改进,提供了许多好用的方法和接口。
首先,最基本也最重要的3个对象:

对象的创建

以上这3个对象,都各自有3种创建方式。

  1. 通过.now()得到当前日期时间对象。
  2. 通过.of()直接指定年月日,时分秒。
  3. LocalDateLocalTime通过解析字符串创建,LocalDateTime可以通过前2个对象创建。

LocalDate对象3种创建方式以及相关方法示例

    @Test
    public void localDate() {
        LocalDate date = LocalDate.of(2017, 5, 3);
        int year = date.getYear();// 2017
        Month month = date.getMonth();// MAY
        int day = date.getDayOfMonth();// 3
        DayOfWeek dow = date.getDayOfWeek();// Wednesday
        int len = date.lengthOfMonth();// 31
        boolean leap = date.isLeapYear();// false
        System.out.println(year + ", " + month + ", " + day + ", " + dow + ", " + len + ", " + leap);

        LocalDate date2 = LocalDate.now();
        int year2 = date2.get(ChronoField.YEAR);// 2017
        int month2 = date2.get(ChronoField.MONTH_OF_YEAR);// 5
        int day2 = date2.get(ChronoField.DAY_OF_MONTH);// 3
        int dow2 = date2.get(ChronoField.DAY_OF_WEEK);// 3
        System.out.println(year2 + ", " + month2 + ", " + day2 + ", " + dow2);

        // 写2017-3-18会报错,DateTimeParseException异常,可以传一个DateTimeFormatter自定义格式
        LocalDate date3 = LocalDate.parse("2017-03-18");
        int year3 = date3.get(ChronoField.YEAR);
        int month3 = date3.get(ChronoField.MONTH_OF_YEAR);
        int day3 = date3.get(ChronoField.DAY_OF_MONTH);
        int dow3 = date3.get(ChronoField.DAY_OF_WEEK);
        System.out.println(year3 + ", " + month3 + ", " + day3 + ", " + dow3);
    }

LocalTime对象3种创建方式以及相关方法示例

    @Test
    public void localTime() {
        LocalTime time = LocalTime.of(11, 06, 23);
        int hour = time.getHour();
        int minute = time.getMinute();
        int second = time.getSecond();
        System.out.println(hour + ":" + minute + ":" + second);

        LocalTime time2 = LocalTime.now();
        int hour2 = time2.get(ChronoField.HOUR_OF_DAY);
        int minute2 = time2.get(ChronoField.MINUTE_OF_HOUR);
        int second2 = time2.get(ChronoField.SECOND_OF_MINUTE);
        System.out.println(hour2 + ":" + minute2 + ":" + second2);

        // 写13:5:43会报错,DateTimeParseException异常,可以传一个DateTimeFormatter自定义格式
        LocalTime time3 = LocalTime.parse("13:05:43");
        int hour3 = time3.get(ChronoField.HOUR_OF_DAY);
        int minute3 = time3.get(ChronoField.MINUTE_OF_HOUR);
        int second3 = time3.get(ChronoField.SECOND_OF_MINUTE);
        System.out.println(hour3 + ":" + minute3 + ":" + second3);
    }

LocalDateTime对象3种创建方式以及相关方法示例

    @Test
    public void localDateTime() {
        LocalDate date = LocalDate.now();
        LocalTime time = LocalTime.now();

        // 创建LocalDateTime
        LocalDateTime dateTime = LocalDateTime.of(2017, 5, 3, 11, 53, 23);
        LocalDateTime dateTime2 = LocalDateTime.of(date, time);
        LocalDateTime dateTime3 = date.atTime(11, 53, 23);
        LocalDateTime dateTime4 = date.atTime(time);
        LocalDateTime dateTime6 = time.atDate(date);

        // 转化
        LocalDate date2 = dateTime2.toLocalDate();
        LocalTime time2 = dateTime2.toLocalTime();
    }

时间间隔对象

    @Test
    public void between() {
//        Duration d1 = Duration.between(time1, time2);
//        Duration d2 = Duration.between(dateTime1, dateTime2);
//        Duration d3 = Duration.between(instant1, instant2);
        Period tenDays = Period.between(LocalDate.of(2014, 3, 8), LocalDate.of(2014, 3, 18));

        Duration threeMinutes = Duration.ofMinutes(3);
        Duration threeMinutes2 = Duration.of(3, ChronoUnit.MINUTES);
        Period tenDays2 = Period.ofDays(10);
        Period threeWeeks = Period.ofWeeks(3);
        Period twoYearsSixMonthsOneDay = Period.of(2, 6, 1);
    }

修改日期时间对象

直接使用.with()来修改对应的年月日时分秒属性

    @Test
    public void editDateTime() {
        LocalDate date1 = LocalDate.of(2014, 3, 18);// 2014-03-18
        LocalDate date2 = date1.withYear(2011);// 2011-03-18
        LocalDate date3 = date2.withDayOfMonth(25);// 2011-03-25
        LocalDate date4 = date3.with(ChronoField.MONTH_OF_YEAR, 9);// 2011-09-25
    }

在现有对象上做相对修改

    @Test
    public void editDateTime() {
        LocalDate date1 = LocalDate.of(2014, 3, 18);// 2014-03-18
        LocalDate date2 = date1.plusWeeks(1);// 2014-03-25
        LocalDate date3 = date2.minusYears(3);// 2011-03-25
        LocalDate date4 = date3.plus(6, ChronoUnit.MONTHS);// 2011-09-25
    }

自定义修改日期

如果要对日期做一些复杂的修改,那么可以通过TemporalAdjusters接口实现复杂逻辑。

    @Test
    public void adjust() {
        LocalDate date1 = LocalDate.of(2017, 5, 8);// 2017-05-08
        LocalDate date2 = date1.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));// 2017-05-14
        LocalDate date3 = date2.with(TemporalAdjusters.lastDayOfMonth());// 2017-05-31
    }

jdk为我们实现了一些常用的方法:

自定义格式化输出日期

新版本,我们拥有一个线程安全的类:DateTimeFormatter
来看代码

    @Test
    public void formatPrint() {
        LocalDate date = LocalDate.of(2014, 3, 18);
        String s1 = date.format(DateTimeFormatter.BASIC_ISO_DATE);// 20140318
        String s2 = date.format(DateTimeFormatter.ISO_LOCAL_DATE);// 2014-03-18

        LocalDate date1 = LocalDate.parse("20140318", DateTimeFormatter.BASIC_ISO_DATE);
        LocalDate date2 = LocalDate.parse("2014-03-18", DateTimeFormatter.ISO_LOCAL_DATE);

        // 自定义格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
        String formattedDate = date.format(formatter);
        System.out.println(formattedDate);// 18/03/2014
        LocalDate date3 = LocalDate.parse(formattedDate, formatter);

        // 带时区的日期(本地化)
        DateTimeFormatter italianFormatter = DateTimeFormatter.ofPattern("d. MMMM yyyy", Locale.ITALIAN);
        String formattedDate2 = date.format(italianFormatter); // 18. marzo 2014
        LocalDate date4 = LocalDate.parse(formattedDate2, italianFormatter);
    }
上一篇下一篇

猜你喜欢

热点阅读