二、JDK8日期转换

1、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.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/,内容更丰富喔。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小薛博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值