Happy3w

Java中时间数据转换关系

2018.01.26
假设已经有如下变量:
long l;
Date date;
LocalDateTime localDateTime;
Timestamp timestamp;
Calendar c;
String str;
Instant instant;

ZoneId zoneId = ZoneId.of("UTC+8").normalized();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
快捷查询窗口:
Source: Target:
=
=
下表中等号表示可以直接赋值;空白表示没有可用的转换方法;其他为代码示例
  • long->long
    =
  • long->Date
    new Date(long)
  • long->LocalDateTime
    LocalDateTime.ofEpochSecond(timestamp / 1000,
    	(int) (timestamp % 1000),
    	ZoneOffset.of(zoneId.getId()));
  • long->Timestamp
    new Timestamp(time)
  • long->Calendar
    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(time);
  • long->String
    format.format(new Date(time));
  • long->Instant
    Instant.ofEpochMilli(l)
  • Date->long
    date.getTime();
  • Date->Date
    =
  • Date->LocalDateTime
    LocalDateTime.ofInstant(date.toInstant(), zoneId);
  • Date->Timestamp
    Timestamp.from(date.toInstant());
  • Date->Calendar
    Calendar c = Calendar.getInstance();
    c.setTime(date);
  • Date->String
    format.format(date);
  • Date->Instant
    date.toInstant()
  • LocalDateTime->long
    ZonedDateTime zdt = localDateTime.atZone(zoneId);
    l = zdt.toEpochSecond() * 1000 + zdt.getNano();
  • LocalDateTime->Date
    Date.from(localDateTime.atZone(zoneId).toInstant());
  • LocalDateTime->LocalDateTime
    =
  • LocalDateTime->Timestamp
    Timestamp.from(localDateTime.atZone(zoneId).toInstant());
  • LocalDateTime->Calendar
    Calendar c = Calendar.getInstance();
    c.setTime(Date.from(localDateTime.atZone(zoneId).toInstant()));
  • LocalDateTime->String
    localDateTime.format(dateTimeFormatter)
  • LocalDateTime->Instant
    localDateTime.toInstant(ZoneOffset.of(zoneId.getId()));
  • Timestamp->long
    timestamp.getTime()
  • Timestamp->Date
    =
  • Timestamp->LocalDateTime
    LocalDateTime.ofInstant(timestamp.toInstant(), zoneId);
  • Timestamp->Timestamp
    =
  • Timestamp->Calendar
    Calendar c = Calendar.getInstance();
    c.setTime(timestamp);
  • Timestamp->String
    format.format(timestamp);
  • Timestamp->Instant
    timestamp.toInstant()
  • Calendar->long
    c.getTimeInMillis()
  • Calendar->Date
    c.getTime()
  • Calendar->LocalDateTime
    LocalDateTime.ofInstant(c.toInstant(), zoneId);
  • Calendar->Timestamp
    Timestamp.from(c.toInstant())
  • Calendar->Calendar
    =
  • Calendar->String
    simpleDateFormat.format(c.getTime());
  • Calendar->Instant
    c.toInstant()
  • String->long
    simpleDateFormat.parse(str).getTime();
  • String->Date
    simpleDateFormat.parse(str);
  • String->LocalDateTime
    LocalDateTime.parse("2018-01-26T17:34:41.000")
  • String->Timestamp
    Timestamp.valueOf("2018-01-26 17:34:41.000");
  • String->Calendar
  • String->String
    =
  • String->Instant
  • Instant->long
    instant.toEpochMilli()
  • Instant->Date
    Date.from(instant)
  • Instant->LocalDateTime
    LocalDateTime.ofInstant(instant, zoneId);
  • Instant->Timestamp
    Timestamp.from(instant)
  • Instant->Calendar
    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(instant.toEpochMilli());
  • Instant->String
     
  • Instant->Instant
    =