jdk 1.8의 LocalDate, LocalTime, LocalDateTime 평가판

49685 단어 출범하다
주의: LocalDate, LocalTime, LocalDateTime, Instant, 이 몇 가지 종류의 모든 속성은final 수식자가 있기 때문에, 이 종류의 속성을 수정하는 모든 방법은 새로운 대상 복사본으로 되돌아옵니다.원래 객체를 사용하는 경우 수정이 유효하지 않습니다.

1, 획득 시간

        // LocalDate

        //  
        LocalDate localDate = LocalDate.now();                       // 2020-07-07
        //  
        int year1 = localDate.getYear();                             // 2020
        int year2 = localDate.get(ChronoField.YEAR);                 // 2020
        //  
        Month month1 = localDate.getMonth();                         // JULY
        int month2 = localDate.getMonthValue();                      // 7
        int month3 = localDate.get(ChronoField.MONTH_OF_YEAR);       // 7
        //  
        int dayOfMonth1 = localDate.getDayOfMonth();                 // 7
        int dayOfMonth2 = localDate.get(ChronoField.DAY_OF_MONTH);   // 7


        // LocalTime

        //  
        LocalTime localTime = LocalTime.now();                      // 17:11:55.956
        //  
        int hour1 = localTime.getHour();                            // 17
        int hour2 = localTime.get(ChronoField.HOUR_OF_DAY);         // 17
        //  
        int minute1 = localTime.getMinute();                        // 11
        int minute2 = localTime.get(ChronoField.MINUTE_OF_HOUR);    // 11
        //  
        int second1 = localTime.getSecond();                        // 55
        int second2 = localTime.get(ChronoField.SECOND_OF_MINUTE);  // 55


        // LocalDateTime

        //  
        LocalDateTime localDateTime = LocalDateTime.now();           // 2020-07-07T17:22:13.067
        //  LocalDate 
        LocalDate localDate4 = localDateTime.toLocalDate();          // 2020-07-07
        //  LocalTime 
        LocalTime localTime4 = localDateTime.toLocalTime();          // 17:22:13.067
        //  
        int year4 = localDateTime.getYear();                         // 2020
        //  
        int month4 = localDateTime.getMonthValue();                  // 7
        //  
        int dayOfMonth4 = localDate.getDayOfMonth();                 // 7

        //  
        int hour4 = localDateTime.getHour();                         // 17
        //  
        int minute4 = localDateTime.getMinute();                     // 22
        //  
        int second4 = localTime.getSecond();                         // 13


        // Instant

        //  Instant 
        Instant instant = Instant.now();
        //  1970-01-01T00:00:00Z 
        long curSecond = instant.getEpochSecond();                  // 1594120503
        //  1970-01-01T00:00:00Z 
        long curMilli = instant.toEpochMilli();                     // 1594120503969


2, 수정 시간

        // LocalDateTime
        
        //   2020-07-06 20:23:57
        LocalDateTime localDateTime = LocalDateTime.of(2020, 7, 6, 20, 23, 57);
        //  
        localDateTime = localDateTime.withYear(2019);                      // 2019-07-06 20:23:57
        localDateTime = localDateTime.with(ChronoField.YEAR, 2021);        // 2021-07-06 20:23:57
        //  
        localDateTime = localDateTime.withMonth(9);                        // 2021-09-06 20:23:57
        //  
        localDateTime = localDateTime.withDayOfMonth(8);                   // 2021-09-08 20:23:57
        //  
        localDateTime = localDateTime.withHour(5);                         // 2021-09-08 05:23:57
        //  
        localDateTime = localDateTime.withMinute(6);                       // 2021-09-08 05:06:57
        //  
        localDateTime = localDateTime.withSecond(8);                       // 2021-09-08 05:06:08


3, 해석 시간

        //  
        // 2020-05-30
        LocalDate localDate1 = LocalDate.parse("20200530", DateTimeFormatter.BASIC_ISO_DATE);
        // 2020-05-30
        LocalDate localDate2 = LocalDate.parse("2020-05-30", DateTimeFormatter.ISO_LOCAL_DATE);
        // 2020-05-30T08:46:32
        LocalDateTime localDateTime1 = LocalDateTime.parse("2020-05-30T08:46:32", DateTimeFormatter.ISO_LOCAL_DATE_TIME);

        //  
        DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyy/MM/dd");
        // 2020-05-30
        LocalDate localDate3 = LocalDate.parse("2020/05/30", dtf1);

        DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 2020-05-30T08:55:13
        LocalDateTime localDateTime2 = LocalDateTime.parse("2020-05-30 08:55:13", dtf2);


4, 포맷 시간

        LocalDateTime localDateTime = LocalDateTime.now();                             // 2020-07-08T08:59:50.152
        //  
        String s1 = localDateTime.format(DateTimeFormatter.BASIC_ISO_DATE);            // 20200708
        String s2 = localDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE);            // 2020-07-08
        String s3 = localDateTime.format(DateTimeFormatter.ISO_LOCAL_TIME);            // 08:59:50.152
        String s4 = localDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);       // 2020-07-08T08:59:50.152
        //  
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String s5 = localDateTime.format(dtf);                                         // 2020-07-08 08:59:50


5, 시간 계산

        LocalDateTime localDateTime = LocalDateTime.now();                             // 2020-07-08T09:11:51.902

        LocalDateTime localDateTime1 = localDateTime.plusDays(1);                      // 2020-07-09T09:11:51.902
        LocalDateTime localDateTime2 = localDateTime.plus(2, ChronoUnit.DAYS);  // 2020-07-10T09:11:51.902

        LocalDateTime localDateTime3 = localDateTime.minusDays(1);                     // 2020-07-07T09:11:51.902

        LocalDateTime localDateTime4 = localDateTime.plusYears(1);                     // 2021-07-08T09:11:51.902
        LocalDateTime localDateTime5 = localDateTime.plusMonths(1);                    // 2020-08-08T09:11:51.902

        LocalDateTime localDateTime6 = localDateTime.plusHours(1);                     // 2020-07-08T10:11:51.902
        LocalDateTime localDateTime7 = localDateTime.plusMinutes(1);                   // 2020-07-08T09:12:51.902
        LocalDateTime localDateTime8 = localDateTime.plusSeconds(1);                   // 2020-07-08T09:11:52.902

6, 시간 도구 클래스

package com.dosrain.shunc.trial.date;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class DateUtil {

    /**
     * Date    LocalDate
     *
     * @param date
     * @return
     */
    public static LocalDate date2LocalDate(Date date) {
        Instant instant = date.toInstant();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        return localDateTime.toLocalDate();
    }

    /**
     * LocalDate   Date
     *
     * @param localDate
     * @return
     */
    public static Date localDate2Date(LocalDate localDate) {
        Instant instant = localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant();
        return Date.from(instant);
    }

    /**
     * Date    LocalDateTime
     *
     * @param date
     * @return
     */
    public static LocalDateTime date2LocalDateTime(Date date) {
        Instant instant = date.toInstant();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        return localDateTime;
    }

    /**
     * LocalDateTime   Date
     *
     * @param localDateTime
     * @return
     */
    public static Date localDateTime2Date(LocalDateTime localDateTime) {
        Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
        return Date.from(instant);
    }

    /**
     *  (LocalDate )
     *
     * @param date
     * @param pattern
     * @return
     */
    public static String formatDate(Date date, String pattern) {
        Instant instant = date.toInstant();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        LocalDate localDate = localDateTime.toLocalDate();
        return localDate.format(DateTimeFormatter.ofPattern(pattern));
    }

    /**
     *  (LocalDate )
     *
     * @param dateString
     * @param pattern
     * @return
     */
    public static Date parseDate(String dateString, String pattern) {
        LocalDate localDate = LocalDate.parse(dateString, DateTimeFormatter.ofPattern(pattern));
        Instant instant = localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant();
        return Date.from(instant);
    }
    /**
     *  (LocalDateTime )
     *
     * @param date
     * @param pattern
     * @return
     */
    public static String formatDateTime(Date date, String pattern) {
        Instant instant = date.toInstant();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        return localDateTime.format(DateTimeFormatter.ofPattern(pattern));
    }

    /**
     *  (LocalDateTime )
     *
     * @param dateString
     * @param pattern
     * @return
     */
    public static Date parseDateTime(String dateString, String pattern) {
        LocalDateTime localDateTime = LocalDateTime.parse(dateString, DateTimeFormatter.ofPattern(pattern));
        Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
        return Date.from(instant);
    }

    /**
     *  LocalDate 
     *
     * @param localDate
     * @return
     */
    public static LocalDate getThisWeekMonday(LocalDate localDate) {
        localDate = localDate.plusDays(1 - localDate.getDayOfWeek().getValue());
        return localDate;
    }

    /**
     *  
     *
     * @param pattern
     * @return
     */
    public static String getLastWeekMonday(String pattern) {
        LocalDate localDate = LocalDate.now();
        localDate = localDate.minusDays(7);
        localDate = getThisWeekMonday(localDate);
        return localDate.format(DateTimeFormatter.ofPattern(pattern));
    }

    /**
     *  1 
     *
     * @param pattern
     * @return
     */
    public static String getLastMonthFirstDay(String pattern) {
        LocalDate localDate = LocalDate.now();
        localDate = localDate.minusMonths(1);
        localDate = localDate.minusDays(localDate.getDayOfMonth() -1);
        return localDate.format(DateTimeFormatter.ofPattern(pattern));
    }


    public static void main(String[] args) {
        System.out.println(DateUtil.formatDate(new Date(), "yyyy-MM-dd"));
        System.out.println(DateUtil.parseDate("2021-08-12", "yyyy-MM-dd"));

        System.out.println(DateUtil.formatDateTime(new Date(), "yyyy-MM-dd HH:mm:ss"));
        System.out.println(DateUtil.parseDateTime("2021-08-12 09:52:03", "yyyy-MM-dd HH:mm:ss"));
        System.out.println(DateUtil.getThisWeekMonday(LocalDate.now()));
        System.out.println(DateUtil.getLastWeekMonday("yyyy-MM-dd"));
        System.out.println(DateUtil.getLastMonthFirstDay("yyyy-MM-dd"));

    }
}

좋은 웹페이지 즐겨찾기