Java时间处理

Java8新增的时间处理API

常用时间处理方法的问题

  1. java.util.Datejava.util.Calendar不是线程安全的类
  2. SimpleDateFormat也是非线程安全的类
  3. Calendar获取月份时需要计算,因为月份从0开始

日期/时间类

  • LocalDateLocalTime

    LocalDate类表示一个具体的日期,但不包含具体时间,也不包含时区信息,LocalTime表示一个具体的时间

    LocalDate localDate = LocalDate.of(2017, 1, 4);     // 初始化一个日期:2017-01-04
    LocalTime localTime = LocalTime.of(17, 23, 52);     // 初始化一个时间:17:23:52
  • LocalDateTime

    上述两个类的结合体

    LocalDateTime ldt1 = LocalDateTime.of(2017, Month.JANUARY, 4, 17, 23, 52);
  • Instant

    Instant表示一个时间戳,精确到纳秒

    Instant instant = Instant.ofEpochSecond(120, 100000);
    //参数:seconds表示从1970-01-01 00:00:00开始到现在的秒数,nanos表示纳秒部分(nanos的值不会超过999,999,999)
  • Duration

    Duration表示一个时间段,精确到纳秒

    LocalDateTime from = LocalDateTime.of(2017, Month.JANUARY, 5, 10, 7, 0);    // 2017-01-05 10:07:00
    LocalDateTime to = LocalDateTime.of(2017, Month.FEBRUARY, 5, 10, 7, 0);     // 2017-02-05 10:07:00
    Duration duration = Duration.between(from, to);     // 表示从 2017-01-05 10:07:00 到 2017-02-05 10:07:00 这段时间

日期格式化

  • DateTimeFormatter类处理日期格式化
  • java.time.ZoneId类处理时区

LocalTime to sql.time

LocalTime now = LocalTime.now();
Time time = Time.valueOf(now);

sql.time to LocalTime

//Get SQL time instance
Time sqlTime = new Time(new Date().getTime());
//Get LocalTime from SQL time
LocalTime localtime = sqlTime.toLocalTime();

Date类型与LocalTime转换

// 01. java.util.Date --> java.time.LocalDateTime
public void UDateToLocalDateTime() {
    java.util.Date date = new java.util.Date();
    Instant instant = date.toInstant();
    ZoneId zone = ZoneId.systemDefault();
    LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
}

// 02. java.util.Date --> java.time.LocalDate
public void UDateToLocalDate() {
    java.util.Date date = new java.util.Date();
    Instant instant = date.toInstant();
    ZoneId zone = ZoneId.systemDefault();
    LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
    LocalDate localDate = localDateTime.toLocalDate();
}

// 03. java.util.Date --> java.time.LocalTime
public void UDateToLocalTime() {
    java.util.Date date = new java.util.Date();
    Instant instant = date.toInstant();
    ZoneId zone = ZoneId.systemDefault();
    LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
    LocalTime localTime = localDateTime.toLocalTime();
}

// 04. java.time.LocalDateTime --> java.util.Date
public void LocalDateTimeToUdate() {
    LocalDateTime localDateTime = LocalDateTime.now();
    ZoneId zone = ZoneId.systemDefault();
    Instant instant = localDateTime.atZone(zone).toInstant();
    java.util.Date date = Date.from(instant);
}

// 05. java.time.LocalDate --> java.util.Date
public void LocalDateToUdate() {
    LocalDate localDate = LocalDate.now();
    ZoneId zone = ZoneId.systemDefault();
    Instant instant = localDate.atStartOfDay().atZone(zone).toInstant();
    java.util.Date date = Date.from(instant);
}

// 06. java.time.LocalTime --> java.util.Date
public void LocalTimeToUdate() {
    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);
}

获取时区偏移量

String zoneStr = "Asia/Shanghai";
LocalDateTime dt = LocalDateTime.now();
ZoneId zoneId = ZoneId.of(zoneStr);
ZoneDateTime zdt = dt.atZone(zoneId);
ZoneOffset offset = zdt.getOffset();
System.out.println(offset);
//output: +08:00

将给定时间转化为UTC时间

ZoneId australia = ZoneId.of("Australia/Sydney"); 
String str = "2018-08-31 17:00"; 
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"); 
LocalDateTime localtDateAndTime = LocalDateTime.parse(str, formatter); 
ZonedDateTime dateAndTimeInSydney = ZonedDateTime.of(localtDateAndTime, australia); 

System.out.println("Current date and time in a particular timezone : " + dateAndTimeInSydney); 

ZonedDateTime utcDate = dateAndTimeInSydney.withZoneSameInstant(ZoneOffset.UTC); 

System.out.println("Current date and time in UTC : " + utcDate); 
  • 另一种方式,设置与相应的时区格式化
String input = "2018-08-31 17:00"; 
ZoneId zone = ZoneId.of("Australia/Sydney"); 

DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm").withZone(zone); 
ZonedDateTime utc = ZonedDateTime.parse(input, fmt).withZoneSameInstant(UTC); 

参考链接

参考链接