Java8 DateTime

1 Date Time API

因为之前的日期时间 API 存在诸多问题,Java 8通过发布新的Date-Time API (JSR 310)来进一步加强对日期与时间的处理,最常用的几个类是:

1.LocalTime:用来操作时间的,如”18:24:44.488”(18点24分44秒488毫秒)
2.LocalDate:用来操作日期的,如”2016-06-12”(2016年6月12日)
3.LocalDateTime:用来操作日期和时间的,如”2018-07-29T18:24:44.488”(2018年7月29日 18点24分44秒488毫秒)

2 案例

因为并不复杂,主要是知道怎么用即可,所以直接贴练习案例了;
主要涉及:时间、日期、时区、转换和格式化:

//main方法
public static void main(String[] args) {
        //1 本地时间
        localTime();
        //2 本地日期
        localDate();
        //3 本地日期时间
        localDateTime();
        //4 时区日期时间
        zonedDateTime();
        //5 操作两个日期
        period();
        //6 操作两个时间
        duration();
        //7 Date Canlder的转换
        toInstant();
        //8 格式化
        formatting();
    }

– –1 localTime() – –


    /**
     * The LocalTime represents time without a date.
     */
    public static void localTime() {
        //An instance of current LocalTime can be created from the system clock
        LocalTime localTime = LocalTime.now();
        System.out.println("localTime: " + localTime);
        //create a LocalTime representing 06:30 AM by parsing a string representation
        LocalTime sixThirty = LocalTime.parse("06:30");
        System.out.println("sixThirty: " + sixThirty);
        //The Factory method “of” can be used to create a LocalTime
        LocalTime sixThirty1 = LocalTime.of(6, 30);
        System.out.println("sixThirty1: " + sixThirty1);
        //creates a LocalTime by parsing a string and adds an hour to it by using the “plus” API
        LocalTime sevenThirty = LocalTime.parse("06:30").plus(1, ChronoUnit.HOURS);
        System.out.println("sevenThirty: " + sevenThirty);
        //Various getter methods are available which can be used to get specific units of time like hour
        int currentHour = LocalTime.parse("06:30").getHour();
        System.out.println("currentHour: " + currentHour);
        //check if a specific time is before or after another specific time
        boolean isbefore = LocalTime.parse("06:30").isBefore(LocalTime.parse("07:30"));
        System.out.println("currentHour: " + currentHour);
        //The max, min and noon time of a day can be obtained by constants in LocalTime class
        LocalTime maxTime = LocalTime.MAX;
        System.out.println("maxTime: " + maxTime);
    }

localTime()执行结果:

----------------localTime()------------------
localTime: 18:49:07.315
sixThirty: 06:30
sixThirty1: 06:30
sevenThirty: 07:30
currentHour: 6
currentHour: 6
maxTime: 23:59:59.999999999 

– –2 localDate() – –

    /**
     * The LocalDate represents a date in ISO format (yyyy-MM-dd) without time.
     */
    public static void localDate() {
        //LocalDate final类型 一旦赋值不可改变
        LocalDate localDate = LocalDate.now();
        System.out.println("localDate: " + localDate);
        //进行一次重新赋值 未报错 但值也未改变
        localDate.of(2008, 8, 8);
        System.out.println("localDate: " + localDate);
        //获取一个日期 方式一
        LocalDate localDate1 = LocalDate.of(2008, 8, 8);
        System.out.println("localDate1: " + localDate1);
        //获取一个日期 方式二
        LocalDate localDate2 = LocalDate.parse("2008-08-09");
        System.out.println("localDate2: " + localDate2);

        //获取明天日期
        LocalDate tomorrow = LocalDate.now().plusDays(1);
        System.out.println("tomorrow: " + tomorrow);

        //获得当前减去一个月的日期(它接受一个枚举时间单位)
        LocalDate previousMonthSameDay = LocalDate.now().minus(1, ChronoUnit.MONTHS);
        System.out.println("previousMonthSameDay: " + previousMonthSameDay);

        //得到一个日期并获取其是一周中的周几
        DayOfWeek saturday = LocalDate.parse("2018-07-28").getDayOfWeek();
        System.out.println("saturday: " + saturday);

        //得到一个日期并获取其是一个月中的哪一天
        int twelve = LocalDate.parse("2018-07-28").getDayOfMonth();
        System.out.println("twelve: " + twelve);

        //当前年份是否是闰年
        boolean leapYear = LocalDate.now().isLeapYear();
        System.out.println("leapYear: " + leapYear);

        //两个时间的关联关系
        boolean notBefore = LocalDate.parse("2016-06-12").isBefore(LocalDate.now());
        System.out.println("2016-06-12 notBefore: " + notBefore);
        boolean isAfter = LocalDate.parse("2016-06-12").isAfter(LocalDate.now());
        System.out.println("2016-06-12 isAfter: " + isAfter);

        //时间边界 获取某个日期的起始时间 如(2016-06-12 00:00:00)
        LocalDateTime beginningOfDay = LocalDate.parse("2016-06-12").atStartOfDay();
        System.out.println("beginningOfDay: " + beginningOfDay);
        //时间边界 获取某个日期的当月的起始天 如(2016-06-12的那个月第一天是2016-06-01)
        LocalDate firstDayOfMonth = LocalDate.parse("2016-06-12").with(TemporalAdjusters.firstDayOfMonth());
        System.out.println("firstDayOfMonth: " + firstDayOfMonth);
    }

localDate()执行结果:

----------------localDate()------------------
localDate: 2018-07-29
localDate: 2018-07-29
localDate1: 2008-08-08
localDate2: 2008-08-09
tomorrow: 2018-07-30
previousMonthSameDay: 2018-06-29
saturday: SATURDAY
twelve: 28
leapYear: false
2016-06-12 notBefore: true
2016-06-12 isAfter: false
beginningOfDay: 2016-06-12T00:00
firstDayOfMonth: 2016-06-01

– –3 localDateTime() – –

    /**
     * The LocalDateTime is used to represent a combination of date and time.
     */
    public static void localDateTime() {
        // LocalDateTime can be obtained from the system clock similar to LocalDate and LocalTime
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println("localDateTime: " + localDateTime);
        //create an instance using the factory “of” and “parse” methods
        LocalDateTime localDateTime1 = LocalDateTime.of(2015, Month.FEBRUARY, 20, 06, 30);
        System.out.println("localDateTime1: " + localDateTime1);

        LocalDateTime localDateTime2 = LocalDateTime.parse("2015-02-20T06:30:00");
        System.out.println("localDateTime2: " + localDateTime2);
        //the usage of “plus” and “minus” methods
        LocalDateTime localDateTimeplusOneDay = localDateTime.plusDays(1);
        System.out.println("localDateTimeplusOneDay: " + localDateTimeplusOneDay);
        LocalDateTime localDateTimeMinusTwoHours = localDateTime.minusHours(2);
        System.out.println("localDateTimeMinusTwoHours: " + localDateTimeMinusTwoHours);
        //LocalDateTime reset
        LocalDateTime localDateTimeReset = localDateTime.withDayOfMonth(10).withYear(2012);
        System.out.println("localDateTimeReset: " + localDateTimeReset);
        //LocalDateTime get month,day and seconds
        Month month = localDateTime.getMonth();
        int day = localDateTime.getDayOfMonth();
        int seconds = localDateTime.getSecond();
        System.out.println("Month: [" + month + "] day: [" + day + "] seconds: [" + seconds + "]");
        //get LocalDate from LocalDateTime
        LocalDate toLocalDate = localDateTime.toLocalDate();
        System.out.println("toLocalDate: " + toLocalDate);
        //get LocalTime from LocalDateTime
        LocalTime toLocalTime = localDateTime.toLocalTime();
        System.out.println("toLocalTime: " + toLocalTime);


    }

localDateTime()执行结果:

----------------localDateTime()------------------
localDateTime: 2018-07-29T18:49:07.350
localDateTime1: 2015-02-20T06:30
localDateTime2: 2015-02-20T06:30
localDateTimeplusOneDay: 2018-07-30T18:49:07.350
localDateTimeMinusTwoHours: 2018-07-29T16:49:07.350
localDateTimeReset: 2012-07-10T18:49:07.350
Month: [JULY] day: [29] seconds: [7]
toLocalDate: 2018-07-29
toLocalTime: 18:49:07.350

– –4 zonedDateTime() – –

    /**
     * Java 8 provides ZonedDateTime when we need to deal with time zone specific date and time.
     * The ZoneId is an identifier used to represent different zones. There are about 40 different time zones.
     */
    public static void zonedDateTime() {
        //Create a Zone for Tokyo
        ZoneId tokyoZoneId = ZoneId.of("Asia/Tokyo");
        System.out.println("tokyoZoneId: " + tokyoZoneId);
        //A set of all zone ids can be obtained
        Set<String> allZoneIds = ZoneId.getAvailableZoneIds();
        System.out.println("allZoneIds: " + allZoneIds);
        // Get the current date and time
        ZonedDateTime zonedDateTime = ZonedDateTime.parse("2012-12-12T10:15:30+05:30[Asia/Tokyo]");
        System.out.println("zonedDateTime: " + zonedDateTime);

        //The LocalDateTime can be converted to a specific zone
        LocalDateTime localDateTime = LocalDateTime.now();
        ZonedDateTime tokyoZonedDateTime = ZonedDateTime.of(localDateTime, tokyoZoneId);
        System.out.println("tokyoZonedDateTime: " + tokyoZonedDateTime);

        //Get currentZone
        ZoneId currentZone = ZoneId.systemDefault();
        System.out.println("currentZone: " + currentZone);

        //The ZonedDateTime provides parse method to get time zone specific date time
        ZonedDateTime parseZonedDateTime = ZonedDateTime.parse("2015-05-03T10:15:30+01:00[Asia/Tokyo]");
        System.out.println("parseZonedDateTime: " + parseZonedDateTime);
        //获取一个固定(2小时)偏移量(相对于格林尼治时间)的时间
        ZoneOffset offset = ZoneOffset.of("+02:00");
        OffsetDateTime offSetByTwo = OffsetDateTime.of(localDateTime, offset);
        System.out.println("localDateTime: " + localDateTime + " and offSetByTwo: " + offSetByTwo);
    }

zonedDateTime()执行结果:

----------------zonedDateTime()------------------
tokyoZoneId: Asia/Tokyo
allZoneIds: [Asia/Aden, America/Cuiaba, Etc/GMT+9, Etc/GMT+8, Africa/Nairobi...]
zonedDateTime: 2012-12-12T10:15:30+09:00[Asia/Tokyo]
tokyoZonedDateTime: 2018-07-29T18:49:07.370+09:00[Asia/Tokyo]
currentZone: Asia/Shanghai
parseZonedDateTime: 2015-05-03T10:15:30+09:00[Asia/Tokyo]
localDateTime: 2018-07-29T18:49:07.370 and offSetByTwo: 2018-07-29T18:49:07.370+02:00

– –5 period() – –

    /**
     * The Period class is widely used to modify values of given a date
     * or to obtain the difference between two dates.
     */
    public static void period() {

        //Get a initialDate
        LocalDate initialDate = LocalDate.parse("2018-05-10");
        System.out.println("initialDate: " + initialDate);

        //The Date can be manipulated using Period as shown in the following code snippet.
        LocalDate finalDate = initialDate.plus(Period.ofDays(5));
        System.out.println("finalDate: " + finalDate);

        //The Period class has various getter methods such as getYears,
        // getMonths and getDays to get values from a Period object
        int betweenTwoDate = Period.between(finalDate, initialDate).getDays();
        System.out.println("betweenTwoDate: " + betweenTwoDate);

        //The Period between two dates can be obtained in a specific unit such as days or month or years,
        // using ChronoUnit.between
        Long intervalDays = ChronoUnit.DAYS.between(initialDate, finalDate);
        Long intervalMonths = ChronoUnit.MONTHS.between(initialDate, finalDate);
        System.out.println("intervalDays: " + intervalDays + ", intervalMonths: " + intervalMonths);
    }

period()执行结果:

----------------period()------------------
initialDate: 2018-05-10
finalDate: 2018-05-15
betweenTwoDate: -5
intervalDays: 5, intervalMonths: 0

– –6 duration() – –

    /**
     * the Duration class is use to deal with Time.
     */
    public static void duration() {

        //Get a initialTime
        LocalTime initialTime = LocalTime.of(6, 30, 0);
        System.out.println("initialTime: " + initialTime);

        //Add a duration of 30 seconds to make a LocalTime of 06:30:30am
        LocalTime finalTime = initialTime.plus(Duration.ofSeconds(30));
        System.out.println("finalTime: " + finalTime);

        //The Duration between two instants can be obtained either as a Duration or as a specific unit
        Long intervalSeconds = Duration.between(finalTime, initialTime).getSeconds();
        System.out.println("intervalSeconds: " + intervalSeconds);
        //Use the between() method of the ChronoUnit class
        Long intervalSeconds1 = ChronoUnit.SECONDS.between(finalTime, initialTime);
        System.out.println("intervalSeconds1: " + intervalSeconds1);
    }

duration()执行结果:

----------------duration()------------------
initialTime: 06:30
finalTime: 06:30:30
intervalSeconds: -30
intervalSeconds1: -30

– –7 toInstant() – –

    /**
     * the toInstant() method which helps to convert existing Date and Calendar instance to new Date Time API
     */
    public static void toInstant() {

        //Date transform to LocalDateTime
        LocalDateTime localDateTime = LocalDateTime.ofInstant(new Date().toInstant(), ZoneId.systemDefault());
        System.out.println("localDateTime: " + localDateTime);
        //Calendar transform to LocalDateTime
        LocalDateTime localDateTime1 = LocalDateTime.ofInstant(Calendar.getInstance().toInstant(), ZoneId.systemDefault());
        System.out.println("localDateTime1: " + localDateTime1);

        //The LocalDateTime can be constructed from epoch seconds as below
        //The result of the below code would be a LocalDateTime representing 2016-06-13T11:34:50
        LocalDateTime localDateTime2 = LocalDateTime.ofEpochSecond(1465817690, 0, ZoneOffset.UTC);
        System.out.println("localDateTime2: " + localDateTime2);
    }

toInstant()执行结果:

----------------toInstant()------------------
localDateTime: 2018-07-29T18:49:07.373
localDateTime1: 2018-07-29T18:49:07.377
localDateTime2: 2016-06-13T11:34:50

– –8 formatting() – –

    /**
     * the easy formatting of Date and Time
     */
    public static void formatting() {
        //Get the LocalDateTime
        LocalDateTime localDateTime = LocalDateTime.of(2015, Month.JANUARY, 25, 6, 30);
        //标准格式化
        String localDateFormat = localDateTime.format(DateTimeFormatter.ISO_DATE);
        System.out.println("localDateFormat: " + localDateFormat);
        //格式化成yyyy/MM/dd
        String localDateFormat1 = localDateTime.format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));
        System.out.println("localDateFormat1: " + localDateFormat1);
        //格式化成SIMPLIFIED_CHINESE
        String localDateFormat2 = localDateTime.format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM).withLocale(Locale.CHINA));
        System.out.println("localDateFormat2: " + localDateFormat2);
    }

formatting()执行结果:

----------------formatting()------------------
localDateFormat: 2015-01-25
localDateFormat1: 2015/01/25
localDateFormat2: 2015-1-25 6:30:00

涉及代码:–>GitHub

参考文献:
[1] http://www.baeldung.com/java-8-date-time-intro
[2] https://www.tutorialspoint.com/java8/java8_datetime_api.htm
[3] https://docs.oracle.com/javase/7/docs/api/java/util/Date.html
[4] https://java2blog.com/java-8-tutorial/

猜你喜欢

转载自blog.csdn.net/weixx3/article/details/81228425
今日推荐