扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
@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.ofInstant(zonedDateTime.toInstant(), 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.from(ZonedDateTime.now().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]
}
2、String转各种日期@Test // 特别经典,建议收藏
public void String_to_ZonedDateTime() {ZonedDateTime dateTime = ZonedDateTime.from(DateTimeFormatter.ISO_OFFSET_DATE_TIME.withZone(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("yyyy-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
}
3、各种日期转String@Test
public void LocalDate_to_String() {String str = DateTimeFormatter.ofPattern("yyyy-MM-dd").format(LocalDate.now());
log.info("str = " + str); //2021-11-06
}
@Test
public void LocalDateTime_to_String() {String str = LocalDateTime.now().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 = ZonedDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
log.info("str = " + str); //2021-11-06 22:13:54
}
4、其他各种日期类互转@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 = LocalDate.now().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.ofInstant(date.toInstant(), ZoneId.systemDefault());
log.info("time = {}", time); //2021-11-06T22:34:11.358
}
@Test
public void LocalDateTime_转_java_util_date() {Instant instant = LocalDateTime.now().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 = localDateTime.toLocalTime();
LocalDate localDate = localDateTime.toLocalDate();
log.info("localDate = {}, localTime = {}", localDate, localTime); //localDate = 2021-11-06, localTime = 22:38:06.205
}
@Test
public void LocalDate_和_LocalTime_转_LocalDateTime() {LocalDateTime localDateTime = LocalDateTime.of(LocalDate.now(), LocalTime.now());
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
}
5、错误示例与正例@Test
public void LocalDate_to_String_错误示例_LocalDate不包含时分秒不能使用HHmmss() {try {String text = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
} catch (UnsupportedTemporalTypeException e) {log.error("错误示例: 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) {log.error("LocalDateTime格式化必须包含时分秒HH:mm:ss, " + e.getMessage(), e);
}
}
@Test
public void LocalDateTime_to_String_正例_但是LocalDateTime可以使用格式yyyy_MM_dd() {String 日期string = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
log.info("LocalDateTime转String,日期string===" + 日期string);
}
更多内容:更多内容大家可以关注一下个人博客网,https://blog.xueqimiao.com/,内容更丰富喔。
为金堂县等地区用户提供了全套网页设计制作服务,及金堂县网站建设行业解决方案。主营业务为成都网站制作、网站建设、外贸网站建设、金堂县网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流