Jdk8最全⽇期转换类,String转各种⽇期,各种⽇期转String java有⾃带的⽇期格式化类,并且定义了各种⽇期格式常量:
DateTimeFormatter.ISO_OFFSET_DATE_TIME 2011-12-03
DateTimeFormatter.ISO_DATE 2011-12-03
DateTimeFormatter.ISO_LOCAL_DATE 2011-12-03
DateTimeFormatter.ISO_LOCAL_TIME 10:15 or 10:15:30
DateTimeFormatter.ISO_LOCAL_DATE_TIME 2011-12-03T10:15:30
DateTimeFormatter.ISO_OFFSET_DATE 2011-12-03+01:00
DateTimeFormatter.ISO_OFFSET_DATE_TIME 2011-12-03T10:15:30+01:00 (推荐应⽤系统使⽤此格式)DateTimeFormatter.ISO_OFFSET_TIME 10:15+01:00 or 10:15:30+01:00
DateTimeFormatter.ISO_TIME 10:15, 10:15:30 or 10:15:30+01:00
DateTimeFormatter.ISO_ZONED_DATE_TIME 2011-12-03T10:15:30+01:00[Europe/Paris]
string转date的方法DateTimeFormatter.ISO_DATE_TIME 2011-12-03T10:15:30, 2011-12-03T10:15:30+01:00 or 2011-12-
03T10:15:30+01:00[Europe/Paris]
DateTimeFormatter.ISO_ORDINAL_DATE 2012-337 (DAY_OF_YEAR)
DateTimeFormatter.ISO_WEEK_DATE 2012-W48-6 (WEEK_OF_WEEK_BASED_YEAR) (DAY_OF_WEEK) DateTimeFormatter.ISO_INSTANT 2011-12-03T10:15:30Z
DateTimeFormatter.BASIC_ISO_DATE 20111203
DateTimeFormatter.RFC_1123_DATE_TIME Tue, 3 Jun 2008 11:05:30 GMT
ZonedDateTime⽇期类与其他各种⽇期类互转
@Test
public void LocalDateTime_转_ZonedDateTime() { // LocalDateTime 转 ZonedDateTime
ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(
LocalDateTime.of(2021, 5, 30, 7, 0, 0)
.atZone(ZoneId.systemDefault()).toInstant(),
ZoneId.systemDefault()
);
LocalDateTime time = LocalDateTime.Instant(), ZoneId.systemDefault());
log.info("time = {}", time); //2021-05-30T07:00
}
@Test
public void java_util_date_转_ZonedDateTime() {
ZonedDateTime time = ZonedDateTime.ofInstant(
new java.util.Date().toInstant(),
ZoneId.systemDefault()
);
log.info("time = {}", time); //2021-11-06T22:23:00.076+08:00[Asia/Shanghai]
}
@Test
public void ZonedDateTime_转_java_util_date() {
Date date = Date.w().toInstant());
log.info("date = {}", date); //Sat Nov 06 22:45:15 CST 2021
}
@Test
public void java_sql_date_转_ZonedDateTime() {
ZonedDateTime time = ZonedDateTime.ofInstant(
new java.sql.Date(System.currentTimeMillis()).toLocalDate().atStartOfDay().toInstant(ZoneOffset.UTC),
ZoneId.systemDefault()
);
log.info("time = {}", time); //2021-11-06T08:00+08:00[Asia/Shanghai]
}
其他各种⽇期类互转
@Test
public void util_date_转_LocalDate() {
LocalDate date = LocalDateTime.ofInstant(new java.util.Date().toInstant(), ZoneId.systemDefault())
.toLocalDate();
log.info("date = {}", date); //2021-11-06
}
@Test
public void LocalDate_转_java_util_date() {
Instant instant = w().atStartOfDay().atZone(ZoneId.systemDefault()).toInstant();
java.util.Date date = java.util.Date.from(instant);
log.info("date = {}", date); //Sat Nov 06 00:00:00 CST 2021
}
@Test
public void util_date_互转_LocalDateTime() {
java.util.Date date = new java.util.Date();
LocalDateTime time = LocalDateTime.Instant(), ZoneId.systemDefault());
log.info("time = {}", time); //2021-11-06T22:34:11.358
}
@Test
public void LocalDateTime_转_java_util_date() {
Instant instant = w().atZone(ZoneId.systemDefault()).toInstant();
java.util.Date date = java.util.Date.from(instant);
log.info("date = {}", date); //Sat Nov 06 22:34:22 CST 2021
}
@Test
public void java_util_date_转_java_sql_Timestamp() {
LocalDateTime localDateTime = LocalDateTime.ofInstant(new java.util.Date().toInstant(), ZoneId.systemDefault());
java.sql.Timestamp timestamp = Timestamp.valueOf(localDateTime);
log.info("timestamp = " + timestamp); //2021-11-06 22:35:17.553
}
@Test
public void java_sql_Timestamp_转_java_util_date() {
LocalDateTime time = new java.sql.Timestamp(System.currentTimeMillis()).toLocalDateTime();
log.info("time = " + time); //2021-11-06T22:35:57.063
}
@Test
public void java_util_date_转_LocalDate_和_LocalTime() {
LocalDateTime localDateTime = LocalDateTime.ofInstant(new java.util.Date().toInstant(), ZoneId.systemDefault());
LocalTime localTime = LocalTime();
LocalDate localDate = LocalDate();
log.info("localDate = {}, localTime = {}", localDate, localTime); //localDate = 2021-11-06, localTime = 22:38:06.205
}
@Test
public void LocalDate_和_LocalTime_转_LocalDateTime() {
LocalDateTime localDateTime = LocalDateTime.w(), w());
Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
java.util.Date date = java.util.Date.from(instant);
log.info("date = {}", date); //Sat Nov 06 22:40:09 CST 2021
}
@Test
public void LocalTime_to_util_date() {
// 只有时分秒的LocalTime建议不要转需要年⽉⽇的date
}
String转各种⽇期
@Test // 特别经典,建议收藏
public void String_to_ZonedDateTime() {
ZonedDateTime dateTime = ZonedDateTime.from(DateTimeFormatter.ISO_OFFSET_DATE_TIME.w
ithZone(ZoneId.systemDefault()) .parse("2011-05-24T14:15:30+08:00"));
log.info("time = " + dateTime); //2011-05-24T14:15:30+08:00[Asia/Shanghai]
}
@Test
public void String_to_LocalDate() {
LocalDate date = LocalDate.parse("2020-04-13", DateTimeFormatter.ofPattern("yyyy-MM-dd"));
log.info("date = " + date); //2020-04-13
}
@Test
public void String_to_LocalDateTime() {
LocalDateTime time = LocalDateTime.parse("2020-04-13 18:49:30", DateTimeFormatter.ofPattern("y
yyy-MM-dd HH:mm:ss")); log.info("time = " + time); //2020-04-13T18:49:30
}
@Test
public void String_to_java_util_Date() {
Instant instant = LocalDate.parse("2020-04-13", DateTimeFormatter.ofPattern("yyyy-MM-dd"))
.atStartOfDay()
.atZone(ZoneId.systemDefault())
.toInstant();
java.util.Date date = java.util.Date.from(instant);
log.info("date = " + date); // Mon Apr 13 00:00:00 CST 2020
}
@Test
public void String_to_java_sql_Date() {
LocalDate localDate = LocalDate.parse("2020-04-20", DateTimeFormatter.ofPattern("yyyy-MM-dd"));
java.sql.Date date = java.sql.Date.valueOf(localDate);
log.info("date = " + date); //2020-04-20
}
@Test
public void String_to_java_sql_Timestamp() { // String 转 java.sql.Date
LocalDateTime localDateTime = LocalDateTime.parse("2020-04-20 19:20:00", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
java.sql.Timestamp timestamp = Timestamp.valueOf(localDateTime);
log.info("timestamp = " + timestamp); //2020-04-20 19:20:00.0
}
各种⽇期转String
@Test
public void LocalDate_to_String() {
String str = DateTimeFormatter.ofPattern("yyyy-MM-dd").w());
log.info("str = " + str); //2021-11-06
}
@Test
public void LocalDateTime_to_String() {
String str = w().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
log.info("str = " + str); // 2021-11-06 21:51:31
}
@Test
public void java_util_Date_to_String() {
String str = LocalDateTime.ofInstant(new java.util.Date().toInstant(), ZoneId.systemDefault())
.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
log.info("str = " + str); //2021-11-06
}
@Test
public void java_sql_Date_to_String() {
String str = new java.sql.Date(System.currentTimeMillis()).toLocalDate()
.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
log.info("str = " + str); //2021-11-06
}
@Test
public void java_sql_Timestamp_to_String() {
String str = new java.sql.Timestamp(System.currentTimeMillis()).toLocalDateTime()
.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
log.info("str = " + str); //2021-11-06 21:56:03
}
@Test
public void ZonedDateTime_to_String() {
String str = w().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
log.info("str = " + str); //2021-11-06 22:13:54
}
错误⽰例与正例
@Test
public void LocalDate_to_String_错误⽰例_LocalDate不包含时分秒不能使⽤HHmmss() {
try {
String text = w().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
} catch (UnsupportedTemporalTypeException e) {
<("错误⽰例: LocalDate不包含时分秒不能使⽤HH:mm:ss, 应该⽤LocalDateTime, " + e.getMessage(), e); }
}
@Test
public void String_to_LocalDateTime_错误⽰例_LocalDateTime格式化必须包含时分秒HHmmss() {
try {
LocalDateTime parsedDate = LocalDateTime.parse("2020-04-13", DateTimeFormatter.ofPattern("yyyy-MM-dd")); } catch (DateTimeException e) {
<("LocalDateTime格式化必须包含时分秒HH:mm:ss, " + e.getMessage(), e);
}
}
@Test
public void LocalDateTime_to_String_正例_但是LocalDateTime可以使⽤格式yyyy_MM_dd() {
String ⽇期string = w().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
log.info("LocalDateTime转String,⽇期string===" + ⽇期string);
}
end
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论