Java 日期时间转换

JSR310 时间类型转换

1
2
3
4
5
6
7
final java.util.Date date = new java.util.Date();
final Timestamp timestamp = new Timestamp(date.getTime());
final Calendar calendar = Calendar.getInstance();

final Instant instant = Instant.now();
final LocalDateTime localDateTime = LocalDateTime.now();
final ZonedDateTime zonedDateTime = ZonedDateTime.now();

转 Instant

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// java.util.Date 转 Instant
Instant dateInstant = date.toInstant();

// Timestamp 转 Instant
Instant timestampInstant = timestamp.toInstant();

// Calendar 转 Instant
Instant calendarInstant = calendar.toInstant();

// LocalDateTime 转 Instant
1. Instant localDateTimeInstant = localDateTime.toInstant(ZoneOffset.of(ZoneId.systemDefault().getId()));
2. Instant localDateTimeInstant = localDateTime.atZone(ZoneId.systemDefault()).toInstant()
第一种使用中报错:Invalid ID for ZoneOffset, invalid format: Asia/Shanghai

// ZonedDateTime 转 Instant
Instant zonedDateTimeInstant = zonedDateTime.toInstant();

转 LocalDateTime

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// java.util.Date 转 LocalDateTime
LocalDateTime dateLocalDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());

// Timestamp 转 LocalDateTime
LocalDateTime timestampLocalDateTime = timestamp.toLocalDateTime();

// Calendar 转 LocalDateTime
LocalDateTime calendarLocalDateTime = LocalDateTime.ofInstant(calendar.toInstant(), ZoneOffset.systemDefault());

// Instant 转 LocalDateTime
LocalDateTime instantLocalDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());

// ZonedDateTime 转 LocalDateTime
LocalDateTime zonedDateTimeLocalDateTime = zonedDateTime.toLocalDateTime();

// 日期字符串转 LocalDateTime
LocalDateTime localDateTime = LocalDate.parse(dateStr).atStartOfDay();

// 日期时间字符串转 ZonedDateTime
LocalDateTime localDateTime = LocalDateTime.parse(dateTimeStr, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

转 ZonedDateTime

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// java.util.Date 转 ZonedDateTime
ZonedDateTime dateZonedDateTime = ZonedDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());

// Timestamp 转 ZonedDateTime
ZonedDateTime timestampZonedDateTime = ZonedDateTime.ofInstant(timestamp.toInstant(), ZoneId.systemDefault());

// Calendar 转 ZonedDateTime
ZonedDateTime calendarZonedDateTime = ZonedDateTime.ofInstant(calendar.toInstant(), ZoneId.systemDefault());

// Instant 转 ZonedDateTime
ZonedDateTime instantZonedDateTime = instant.atZone(ZoneId.systemDefault());

// LocalDateTime 转 ZonedDateTime
ZonedDateTime localDateTimeZonedDateTime = localDateTime.atZone(ZoneId.systemDefault());

// 日期字符串转 ZonedDateTime
ZonedDateTime zonedDateTime = LocalDate.parse(dateStr, DateTimeFormatter.ofPattern("yyyy-MM-dd")).atStartOfDay(ZoneId.systemDefault())

// 日期时间字符串转 ZonedDateTime
ZonedDateTime zonedDateTime = LocalDateTime.parse(dateTimeStr, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")).atZone(ZoneId.systemDefault())

转 java.util.Date

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Timestamp 转 Date
Date timestampDate = new Date(timestamp.getTime());

// Calendar 转 Date
Date calendarDate = calendar.getTime();

// Instant 转 Date
Date instantDate = Date.from(instant);

// LocalDateTime 转 Date
Date localDateTimeDate = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());

// ZonedDateTime 转 Date
Date zonedDateTimeDate = Date.from(zonedDateTime.toInstant());

转 Timestamp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// java.util.Date 转 Timestamp
Timestamp dateTimestamp = new Timestamp(date.getTime());

// Calendar 转 Timestamp
Timestamp calendarTimestamp = new Timestamp(calendar.getTimeInMillis());

// Instant 转 Timestamp
Timestamp instantTimestamp = Timestamp.from(instant);

// LocalDateTime 转 Timestamp
Timestamp localDateTimeTimestamp = Timestamp.valueOf(localDateTime);

// ZonedDateTime 转 Timestamp
Timestamp zonedDateTimeTimestamp = Timestamp.from(zonedDateTime.toInstant());

转 Calendar

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// java.util.Date 转 Calendar
Calendar dateCalendar = Calendar.getInstance();
dateCalendar.setTime(date);

// Timestamp 转 Calendar
Calendar timestampCalendar = Calendar.getInstance();
timestampCalendar.setTimeInMillis(timestamp.getTime());

// Instant 转 Calendar
Calendar instantCalendar = GregorianCalendar.from(ZonedDateTime.ofInstant(instant, ZoneId.systemDefault()));

// LocalDateTime 转 Calendar
Calendar localDateTimeCalendar = GregorianCalendar.from(ZonedDateTime.of(localDateTime, ZoneId.systemDefault()));

// ZonedDateTime 转 Calendar
Calendar zonedDateTimeInstantCalendar = GregorianCalendar.from(zonedDateTime);

转载自 JSR310 时间类型转换

FAQ:

1
2017-08-16 11:08:49.729  WARN 28128 --- [  XNIO-2 task-1] .m.m.a.ExceptionHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.time.ZonedDateTime'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.RequestParam @org.springframework.format.annotation.DateTimeFormat java.time.ZonedDateTime] for value '02/17/2011'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [02/17/2011].

接收的数据只有 date,目标类型是 date 和 time。

分享到:
Disqus 加载中...

如果长时间无法加载,请针对 disq.us | disquscdn.com | disqus.com 启用代理