Fork me on GitHub

LocalDate、LocalDateTime、LocalTime开发小结

  在我之前的文章《【整理】Java 8新特性总结 》中有提到Date/Time API (JSR 310)对日期与时间的处理。它将服务端对时间的处理进行了统一,使得对时间的处理更加规范和统一。

  本文主要是对Date/Time API开发过程中的小结。

Date 和LocalDate、LocalDateTime、LocalTime之间的转换

  Date转LocalDate、LocalDateTime、LocalTime

public static void main(String[] args) {
    Date date = new Date();
    Instant instant = date.toInstant();
    ZoneId zoneId = ZoneId.systemDefault();

    // atZone()方法返回在指定时区从此Instant生成的ZonedDateTime。
    LocalDate localDate = instant.atZone(zoneId).toLocalDate();
    LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
    LocalTime localTime = instant.atZone(zoneId).toLocalTime();
    System.out.println("Date = " + date);//Wed Apr 24 14:43:25 CST 2019
    System.out.println("LocalDate = " + localDate);//2019-04-24
    System.out.println("LocalDateTime = " + localDateTime);//2019-04-24T14:43:25.030
    System.out.println("LocalTime = " + localTime);//14:43:25.030
}

  LocalDateTime转Date

public static void main(String[] args) {

    LocalTime localTime = LocalTime.now();
    LocalDate localDate = LocalDate.now();
    LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime);
    ZoneId zone = ZoneId.systemDefault();
    Instant instant = localDateTime.atZone(zone).toInstant();
    java.util.Date date = Date.from(instant);
    System.out.println("LocalDate = "+localDate);//2019-04-24
    System.out.println("LocalTime = "+localTime);//14:43:25.030
    System.out.println("LocalDateTime = "+localDateTime);//2019-04-24T14:43:25.030
    System.out.println("Date = "+date);//Wed Apr 24 14:43:25 CST 2019
}

LocalDateTime与LocalTime、LocalDate之间的转换

  LocalDateTime转LocalTime、LocalDate

public static void main(String[] args) {

    LocalDateTime localDateTime = LocalDateTime.now();
    LocalDate localDate = localDateTime.toLocalDate();
    LocalTime localTime = localDateTime.toLocalTime();
    System.out.println("LocalDateTime = "+localDateTime);//2019-04-24T14:43:25.030
    System.out.println("LocalDate = "+localDate);//2019-04-24
    System.out.println("LocalTime = "+localTime);//14:43:25.030      
}

  LocalDate转LocalDateTime

public static void main(String[] args) {
    
  LocalDate localDate =LocalDate.now();
  LocalDateTime localDateTime = localDate.atStartOfDay();
  LocalDateTime localDateTime2 = localDate.atTime(LocalTime.now());
  LocalDateTime localDateTime1 = LocalDateTime.of(localDate,LocalTime.MAX);
  System.out.println(localDate);//2019-04-24
  System.out.println(localDateTime);//2019-04-24T00:00
  System.out.println(localDateTime1);//2019-04-24T23:59:59.999999999
  System.out.println(localDateTime2);//2019-04-24T15:18:41.823
}

String和LocalDateTime之间的转换

  LocalDateTime转String

public static void main(String[] args) {
   //定义时间格式化      
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyy.MM.dd HH:mm");
    LocalDateTime ldt = LocalDateTime.now();
    String time = dtf.format(ldt);
    String time1 = dtf1.format(ldt);
    System.out.println(time);//2019-04-24 15:58:04
    System.out.println(time1);//2019.04.24 15:58
 }

 String转LocalDateTime

public static void main(String[] args) {
     DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
          
     LocalDateTime localDateTime = LocalDateTime.parse("2019-04-24 15:33:23",dtf);
     System.out.println(localDateTime);//2019-04-24T15:33:23      
}

时间戳和LocalDateTime之间的转换

  localDateTime转时间戳

public static void main(String[] args) {
       
  System.out.println(LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli());//1556095208585  毫秒
  System.out.println(LocalDateTime.now().toInstant(ZoneOffset.of("+8")).getEpochSecond());//1556095208585 秒
}

  时间戳转localDateTime

public static void main(String[] args) {
    Long timestamp = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).getEpochSecond();
    LocalDateTime time2 =LocalDateTime.ofEpochSecond(timestamp,0,ZoneOffset.ofHours(8));
   System.out.println(time2);
}

其他

LocalDateTime.now().plus(100000, ChronoUnit.DAYS)
同 LocalDateTime.now().plusDays(100000)
LocalDateTime.now().get(ChronoField.DAY_OF_MONTH)
同 LocalDateTime.now().getDayOfMonth()
ChronoUnit枚举
public enum ChronoUnit implements TemporalUnit {

    NANOS("Nanos", Duration.ofNanos(1)),
   
    MICROS("Micros", Duration.ofNanos(1000)),
    
    MILLIS("Millis", Duration.ofNanos(1000_000)),
   
    SECONDS("Seconds", Duration.ofSeconds(1)),
    
    MINUTES("Minutes", Duration.ofSeconds(60)),
    
    HOURS("Hours", Duration.ofSeconds(3600)),
   
    HALF_DAYS("HalfDays", Duration.ofSeconds(43200)),
   
    DAYS("Days", Duration.ofSeconds(86400)),
   
    WEEKS("Weeks", Duration.ofSeconds(7 * 86400L)),
    
    MONTHS("Months", Duration.ofSeconds(31556952L / 12)),
   
    YEARS("Years", Duration.ofSeconds(31556952L)),
   
    DECADES("Decades", Duration.ofSeconds(31556952L * 10L)),
    
    CENTURIES("Centuries", Duration.ofSeconds(31556952L * 100L)),
     
    MILLENNIA("Millennia", Duration.ofSeconds(31556952L * 1000L)),
    
    ERAS("Eras", Duration.ofSeconds(31556952L * 1000_000_000L)),
   
    FOREVER("Forever", Duration.ofSeconds(Long.MAX_VALUE, 999_999_999));

    private final String name;
    private final Duration duration;

    private ChronoUnit(String name, Duration estimatedDuration) {
        this.name = name;
        this.duration = estimatedDuration;
    }
 
}
ChronoField枚举
public enum ChronoField implements TemporalField {

    NANO_OF_SECOND("NanoOfSecond", NANOS, SECONDS, ValueRange.of(0, 999_999_999)),
    
    NANO_OF_DAY("NanoOfDay", NANOS, DAYS, ValueRange.of(0, 86400L * 1000_000_000L - 1)),
    
    MICRO_OF_SECOND("MicroOfSecond", MICROS, SECONDS, ValueRange.of(0, 999_999)),
    
    MICRO_OF_DAY("MicroOfDay", MICROS, DAYS, ValueRange.of(0, 86400L * 1000_000L - 1)),
    
    MILLI_OF_SECOND("MilliOfSecond", MILLIS, SECONDS, ValueRange.of(0, 999)),
    
    MILLI_OF_DAY("MilliOfDay", MILLIS, DAYS, ValueRange.of(0, 86400L * 1000L - 1)),
    
    SECOND_OF_MINUTE("SecondOfMinute", SECONDS, MINUTES, ValueRange.of(0, 59), "second"),
    
    SECOND_OF_DAY("SecondOfDay", SECONDS, DAYS, ValueRange.of(0, 86400L - 1)),
    
    MINUTE_OF_HOUR("MinuteOfHour", MINUTES, HOURS, ValueRange.of(0, 59), "minute"),
    
    MINUTE_OF_DAY("MinuteOfDay", MINUTES, DAYS, ValueRange.of(0, (24 * 60) - 1)),
    
    HOUR_OF_AMPM("HourOfAmPm", HOURS, HALF_DAYS, ValueRange.of(0, 11)),
    
    CLOCK_HOUR_OF_AMPM("ClockHourOfAmPm", HOURS, HALF_DAYS, ValueRange.of(1, 12)),
    
    HOUR_OF_DAY("HourOfDay", HOURS, DAYS, ValueRange.of(0, 23), "hour"),
    
    CLOCK_HOUR_OF_DAY("ClockHourOfDay", HOURS, DAYS, ValueRange.of(1, 24)),
    
    AMPM_OF_DAY("AmPmOfDay", HALF_DAYS, DAYS, ValueRange.of(0, 1), "dayperiod"),
    
    DAY_OF_WEEK("DayOfWeek", DAYS, WEEKS, ValueRange.of(1, 7), "weekday"),
ALIGNED_DAY_OF_WEEK_IN_MONTH(
"AlignedDayOfWeekInMonth", DAYS, WEEKS, ValueRange.of(1, 7)),
ALIGNED_DAY_OF_WEEK_IN_YEAR(
"AlignedDayOfWeekInYear", DAYS, WEEKS, ValueRange.of(1, 7)), DAY_OF_MONTH("DayOfMonth", DAYS, MONTHS, ValueRange.of(1, 28, 31), "day"), DAY_OF_YEAR("DayOfYear", DAYS, YEARS, ValueRange.of(1, 365, 366)), EPOCH_DAY("EpochDay", DAYS, FOREVER, ValueRange.of((long) (Year.MIN_VALUE * 365.25), (long) (Year.MAX_VALUE * 365.25))), ALIGNED_WEEK_OF_MONTH("AlignedWeekOfMonth", WEEKS, MONTHS, ValueRange.of(1, 4, 5)), ALIGNED_WEEK_OF_YEAR("AlignedWeekOfYear", WEEKS, YEARS, ValueRange.of(1, 53)), MONTH_OF_YEAR("MonthOfYear", MONTHS, YEARS, ValueRange.of(1, 12), "month"), YEAR("Year", YEARS, FOREVER, ValueRange.of(Year.MIN_VALUE, Year.MAX_VALUE), "year"), private final String name; private final TemporalUnit baseUnit; private final TemporalUnit rangeUnit; private final ValueRange range; private final String displayNameKey; private ChronoField(String name, TemporalUnit baseUnit, TemporalUnit rangeUnit, ValueRange range) { this.name = name; this.baseUnit = baseUnit; this.rangeUnit = rangeUnit; this.range = range; this.displayNameKey = null; } }

   

posted @ 2019-04-24 17:31  JackpotHan  阅读(1185)  评论(0编辑  收藏  举报