具体理论网上有很多介绍了,本地不再提及; 本文全使用例子; 执行一下对比一下结果在理解是会有很大的收获。
ZonedDateTime、OffsetDateTime、LocalDateTime、instant、Date、timestamp之间如何相互转化
public static final LocalDateTime LOCAL_DATE_TIME = LocalDateTime.of(2019, 7, 7, 20, 18, 18, 888);
public static final ZonedDateTime ZONED_DATE_TIME = ZonedDateTime.of(2019, 7, 7, 20, 18, 18, 888, ZoneId.of("Asia/Tokyo"));
public static final OffsetDateTime OFFSET_DATE_TIME = OffsetDateTime.of(2019, 7, 7, 20, 18, 18, 888, ZoneOffset.ofHours(9));
protected static final Date DATE = new Date(1562501898000L);
public void instantBuild() {
System.out.println(Instant.now());
System.out.println("----------");
System.out.println(Instant.ofEpochMilli(1562501898000L));
System.out.println(Instant.ofEpochMilli(1562501898888L));
System.out.println(Instant.ofEpochSecond(1562501898));
System.out.println(Instant.ofEpochSecond(1562501898,888));
System.out.println("----------");
System.out.println(ZONED_DATE_TIME.toInstant());
System.out.println(OFFSET_DATE_TIME.toInstant());
System.out.println(LOCAL_DATE_TIME.toInstant(ZoneOffset.ofHours(8)));
System.out.println(LOCAL_DATE_TIME.toInstant(ZoneOffset.UTC));
System.out.println("----------");
System.out.println(DATE.toInstant());
System.out.println("----------");
System.out.println(Instant.parse("2019-07-07T20:18:18.000000888Z"));
System.out.println(Instant.parse("2019-07-07T20:18:18Z"));
System.out.println("----------");
}
public void dateBuild() throws ParseException {
System.out.println(new Date(1562501898000L));
System.out.println(new Date());
Date date1 = new Timestamp(1562501898000L);
System.out.println(date1);
System.out.println(new Date(new Timestamp(1562501898000L).getTime()));
System.out.println(Calendar.getInstance().getTime());
System.out.println("----------");
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("格式化器用的时区是:" + dateFormat.getTimeZone().getID() + ":" + dateFormat.parse("2019-07-07 20:18:18"));
dateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Tokyo"));
System.out.println("格式化器用的时区是:" + dateFormat.getTimeZone().getID() + ":" + dateFormat.parse("2019-07-07 20:18:18"));
System.out.println(DateUtils.parseDate("2019-07-07 20:18:18 +0800","yyyy-MM-dd HH:mm:ss Z"));
System.out.println(DateUtils.parseDate("2019-07-07 20:18:18.888","yyyy-MM-dd HH:mm:ss.SSS"));
System.out.println(DateUtils.parseDate("2019-07-07 20:18:18","yyyy-MM-dd HH:mm:ss"));
System.out.println(DateUtils.parseDate("2019-07-07 20:18","yyyy-MM-dd HH:mm"));
System.out.println(DateUtils.parseDate("2019/07/07 20:18:18 +0800","yyyy/MM/dd HH:mm:ss Z"));
System.out.println(DateUtils.parseDate("2019/07/07 20:18:18.888","yyyy/MM/dd HH:mm:ss.SSS"));
System.out.println(DateUtils.parseDate("2019/07/07 20:18:18","yyyy/MM/dd HH:mm:ss"));
System.out.println(DateUtils.parseDate("2019/07/07 20:18","yyyy/MM/dd HH:mm"));
System.out.println(DateUtils.parseDate("20190707201818 +0800","yyyyMMddHHmmss Z"));
System.out.println(DateUtils.parseDate("20190707201818888","yyyyMMddHHmmsSSS"));
System.out.println(DateUtils.parseDate("20190707201818","yyyyMMddHHmmss"));
System.out.println(DateUtils.parseDate("201907072018","yyyyMMddHHmm"));
System.out.println("----------");
System.out.println(DateUtils.parseDate("2019-07-32 20:18:18 +0800","yyyy-MM-dd HH:mm:ss Z"));
System.out.println(DateUtils.parseDateStrictly("2019-07-07 20:18:18 +0800","yyyy-MM-dd HH:mm:ss Z"));
System.out.println("----------");
System.out.println(Date.from(Instant.ofEpochMilli(1562501898888L)));
System.out.println(Date.from(Instant.ofEpochSecond(1562501898,888)));
System.out.println(Date.from(Instant.ofEpochSecond(1562501898)));
System.out.println(Date.from(Instant.parse("2019-07-07T20:18:18.000000888Z")));
System.out.println(Date.from(Instant.parse("2019-07-07T20:18:18Z")));
System.out.println(Date.from(ZONED_DATE_TIME.toInstant()));
System.out.println(Date.from(OFFSET_DATE_TIME.toInstant()));
System.out.println(Date.from(LOCAL_DATE_TIME.toInstant(ZoneOffset.UTC)));
System.out.println(Date.from(LOCAL_DATE_TIME.toInstant(ZoneOffset.ofHours(8))));
}
public void timestampBuild() {
System.out.println(new Timestamp(1562501898000L));
System.out.println(new Timestamp(new Date(1562501898000L).getTime()));
System.out.println("----------");
System.out.println(Timestamp.from(Instant.ofEpochMilli(1562501898888L)));
System.out.println(Timestamp.from(Instant.ofEpochSecond(1562501898,888)));
System.out.println(Timestamp.from(Instant.ofEpochSecond(1562501898)));
System.out.println(Timestamp.from(Instant.parse("2019-07-07T20:18:18.000000888Z")));
System.out.println(Timestamp.from(Instant.parse("2019-07-07T20:18:18Z")));
System.out.println("----------");
System.out.println(Timestamp.from(ZONED_DATE_TIME.toInstant()));
System.out.println(Timestamp.from(OFFSET_DATE_TIME.toInstant()));
System.out.println(Timestamp.from(LOCAL_DATE_TIME.toInstant(ZoneOffset.ofHours(8))));
System.out.println(Timestamp.from(LOCAL_DATE_TIME.toInstant(ZoneOffset.UTC)));
System.out.println("----------");
System.out.println(Timestamp.valueOf(LOCAL_DATE_TIME));
System.out.println(ZONED_DATE_TIME);
System.out.println(ZONED_DATE_TIME.toLocalDateTime());
System.out.println(Timestamp.valueOf(ZONED_DATE_TIME.toLocalDateTime()));
}
public void localDateBuild() {
System.out.println(LocalDate.now());
System.out.println(LocalDate.of(2019, 7, 7));
System.out.println(LocalDate.of(2019, Month.JULY, 7));
System.out.println(LOCAL_DATE_TIME.toLocalDate());
System.out.println(OFFSET_DATE_TIME.toLocalDate());
System.out.println(ZONED_DATE_TIME.toLocalDate());
}
public void localTimeBuild() {
System.out.println(LocalTime.now());
System.out.println(LocalTime.of(20, 18));
System.out.println(LocalTime.of(20, 18, 18));
System.out.println(LocalTime.of(20, 18, 18, 888));
System.out.println(LOCAL_DATE_TIME.toLocalTime());
System.out.println(OFFSET_DATE_TIME.toLocalTime());
System.out.println(ZONED_DATE_TIME.toLocalTime());
}
public void localDateTimeBuild() {
System.out.println(LocalDateTime.now());
System.out.println("----------1");
System.out.println(LocalDateTime.of(2019, 7, 7, 20, 18, 18, 888));
System.out.println(LocalDateTime.of(2019, 7, 7, 20, 18, 18));
System.out.println(LocalDateTime.of(2019, 7, 7, 20, 18));
System.out.println(LocalDateTime.of(2019, Month.JULY, 7, 20, 18, 18, 888));
System.out.println(LocalDateTime.of(2019, Month.JULY, 7, 20, 18, 18));
System.out.println(LocalDateTime.of(2019, Month.JULY, 7, 20, 18));
System.out.println(LocalDateTime.of(LocalDate.of(2019, 7, 7), LocalTime.of(20, 18,18,888)));
System.out.println(LocalDateTime.of(LocalDate.of(2019, 7, 7), LocalTime.of(20, 18,18)));
System.out.println(LocalDateTime.of(LocalDate.of(2019, 7, 7), LocalTime.of(20, 18)));
System.out.println("----------2");
System.out.println(LocalDateTime.ofEpochSecond(1562501898,888888888, ZoneOffset.UTC));
System.out.println(LocalDateTime.ofEpochSecond(1562501898,888888888, ZoneOffset.ofHours(8)));
System.out.println(LocalDateTime.ofInstant(Instant.parse("2019-07-07T20:18:18.000000888Z"), ZoneId.of("UTC")));
System.out.println(LocalDateTime.ofInstant(Instant.parse("2019-07-07T20:18:18.000000888Z"), ZoneId.of("+08:00")));
System.out.println(DATE.toInstant().atZone(ZoneId.of("+09:00")).toLocalDateTime());
System.out.println(OFFSET_DATE_TIME.toLocalDateTime());
System.out.println(ZONED_DATE_TIME.toLocalDateTime());
System.out.println("----------3");
System.out.println(LocalDateTime.parse("2022-04-03T18:00"));
DateTimeFormatter df = DateTimeFormatter.ofPattern("第Q季度 yyyy-MM-dd HH:mm:ss", Locale.JAPAN);
System.out.println(LocalDateTime.parse("第3季度 2019-07-07 20:18:18",df));
DateTimeFormatter df1 = DateTimeFormatter.ofPattern("第Q季度 yyyy-MM-dd HH:mm:ss", Locale.CHINA);
System.out.println(LocalDateTime.parse("第3季度 2019-07-07 20:18:18",df1));
System.out.println(LocalDateTime.parse("2019-07-07T20:18:18.000000888"));
System.out.println(LocalDateTime.parse("2019-07-07T20:18:18"));
System.out.println(LocalDateTime.parse("2019-07-07T20:18"));
}
public void offsetDateTimeBuild() {
System.out.println(OffsetDateTime.now());
System.out.println(OffsetDateTime.now(ZoneId.of("Asia/Tokyo")));
System.out.println(OffsetDateTime.now(Clock.systemUTC()));
System.out.println("----------1");
System.out.println(OffsetDateTime.of(2019, 7, 7, 20, 18, 18, 888, ZoneOffset.ofHours(9)));
System.out.println(OffsetDateTime.of(LocalDate.of(2019, 7, 7),LocalTime.of(20, 18, 18, 888),ZoneOffset.ofHours(9)));
System.out.println(OffsetDateTime.of(LOCAL_DATE_TIME,ZoneOffset.ofHours(9)));
System.out.println(OffsetDateTime.ofInstant(LOCAL_DATE_TIME.toInstant(ZoneOffset.ofHours(9)), ZoneOffset.ofHours(9)));
System.out.println("----------2");
System.out.println(LOCAL_DATE_TIME.atOffset(ZoneOffset.ofHours(9)));
System.out.println(ZONED_DATE_TIME.toOffsetDateTime());
System.out.println(OffsetDateTime.parse("2019-07-07T20:18:18.000000888+09:00"));
System.out.println(OffsetDateTime.parse("2019-07-07T20:18:18+09:00"));
System.out.println(OffsetDateTime.parse("2019-07-07T20:18+09:00"));
}
public void zonedDateTimeBuild() {
System.out.println(ZonedDateTime.now());
System.out.println(ZonedDateTime.now(ZoneId.of("Asia/Tokyo")));
System.out.println(ZonedDateTime.now(Clock.systemUTC()));
System.out.println("----------2");
System.out.println(ZonedDateTime.of(2019, 7, 7, 20, 18, 18, 888, ZoneId.of("Asia/Tokyo")));
System.out.println(ZonedDateTime.of(LocalDate.of(2019, 7, 7),LocalTime.of(20, 18, 18, 888),ZoneId.of("Asia/Tokyo")));
System.out.println(ZonedDateTime.of(LOCAL_DATE_TIME, ZoneId.of("Asia/Tokyo")));
System.out.println("--------3");
System.out.println(ZonedDateTime.ofInstant(LOCAL_DATE_TIME.toInstant(ZoneOffset.ofHours(9)), ZoneOffset.ofHours(9)));
System.out.println(ZonedDateTime.ofInstant(LOCAL_DATE_TIME.toInstant(ZoneOffset.ofHours(9)), ZoneId.of("Asia/Tokyo")));
System.out.println(ZonedDateTime.ofInstant(LOCAL_DATE_TIME.toInstant(ZoneOffset.ofHours(9)), ZoneId.of("Asia/Shanghai")));
System.out.println(ZonedDateTime.ofInstant(LOCAL_DATE_TIME, ZoneOffset.ofHours(9), ZoneOffset.ofHours(9)));
System.out.println(ZonedDateTime.ofInstant(LOCAL_DATE_TIME, ZoneOffset.UTC, ZoneOffset.ofHours(9)));
System.out.println("--------4");
System.out.println(OFFSET_DATE_TIME.toZonedDateTime());
System.out.println(OFFSET_DATE_TIME.atZoneSameInstant(ZoneId.of("Asia/Tokyo")));
System.out.println(OFFSET_DATE_TIME.atZoneSameInstant(ZoneId.of("Asia/Shanghai")));
System.out.println(OFFSET_DATE_TIME.atZoneSimilarLocal(ZoneId.of("Asia/Tokyo")));
System.out.println(OFFSET_DATE_TIME.atZoneSimilarLocal(ZoneId.of("Asia/Shanghai")));
System.out.println("--------5");
System.out.println(LOCAL_DATE_TIME.atZone(ZoneId.of("Asia/Tokyo")));
System.out.println(DATE.toInstant().atZone(ZonedDateTime.now().getZone()));
System.out.println(DATE.toInstant().atZone(ZonedDateTime.now().getOffset()));
System.out.println("----------6");
System.out.println(ZonedDateTime.parse("2019-07-07T20:18:18.000000888+09:00[Asia/Tokyo]"));
System.out.println(ZonedDateTime.parse("2019-07-07T20:18:18+09:00[Asia/Tokyo]"));
System.out.println(ZonedDateTime.parse("2019-07-07T20:18+09:00[Asia/Tokyo]"));
}
把日期转换为String格式
public static final LocalDateTime LOCAL_DATE_TIME = LocalDateTime.of(2019, 7, 7, 20, 18, 18, 888);
public static final ZonedDateTime ZONED_DATE_TIME = ZonedDateTime.of(2019, 7, 7, 20, 18, 18, 888, ZoneId.of("Asia/Tokyo"));
public static final OffsetDateTime OFFSET_DATE_TIME = OffsetDateTime.of(2019, 7, 7, 20, 18, 18, 888, ZoneOffset.ofHours(9));
protected static final Date DATE = new Date(1562501898888L);
public void timestampToString() {
Timestamp timestamp = new Timestamp(156250189888L);
System.out.println(timestamp.toString());
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(df.format(timestamp));
}
public void dateToString() {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("这是北京时间:" + df.format(DATE));
df.setTimeZone(TimeZone.getTimeZone("Asia/Tokyo"));
System.out.println("这是东京时间:" + df.format(DATE));
}
public void ISO_DATE_TIME() {
Assert.assertEquals("2019-07-07T20:18:18.000000888+09:00[Asia/Tokyo]", ZONED_DATE_TIME.format(DateTimeFormatter.ISO_DATE_TIME));
Assert.assertEquals("2019-07-07T20:18:18.000000888+09:00", OFFSET_DATE_TIME.format(DateTimeFormatter.ISO_DATE_TIME));
Assert.assertEquals("2019-07-07T20:18:18.000000888", LOCAL_DATE_TIME.format(DateTimeFormatter.ISO_DATE_TIME));
}
public void ISO_DATE() {
Assert.assertEquals("2019-07-07+09:00", ZONED_DATE_TIME.format(DateTimeFormatter.ISO_DATE));
Assert.assertEquals("2019-07-07+09:00", OFFSET_DATE_TIME.format(DateTimeFormatter.ISO_DATE));
Assert.assertEquals("2019-07-07", LOCAL_DATE_TIME.format(DateTimeFormatter.ISO_DATE));
}
public void ISO_TIME() {
Assert.assertEquals("20:18:18.000000888+09:00", ZONED_DATE_TIME.format(DateTimeFormatter.ISO_TIME));
Assert.assertEquals("20:18:18.000000888+09:00", OFFSET_DATE_TIME.format(DateTimeFormatter.ISO_TIME));
Assert.assertEquals("20:18:18.000000888", LOCAL_DATE_TIME.format(DateTimeFormatter.ISO_TIME));
}
public void ISO_LOCAL_DATE_TIME() {
Assert.assertEquals("2019-07-07T20:18:18.000000888", DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(ZONED_DATE_TIME));
Assert.assertEquals("2019-07-07T20:18:18.000000888", DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(OFFSET_DATE_TIME));
Assert.assertEquals("2019-07-07T20:18:18.000000888", DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(LOCAL_DATE_TIME));
}
public void ISO_LOCAL_DATE() {
Assert.assertEquals("2019-07-07", DateTimeFormatter.ISO_LOCAL_DATE.format(ZONED_DATE_TIME));
Assert.assertEquals("2019-07-07", DateTimeFormatter.ISO_LOCAL_DATE.format(OFFSET_DATE_TIME));
Assert.assertEquals("2019-07-07", DateTimeFormatter.ISO_LOCAL_DATE.format(LOCAL_DATE_TIME));
}
public void ISO_LOCAL_TIME() {
Assert.assertEquals("20:18:18.000000888", DateTimeFormatter.ISO_LOCAL_TIME.format(ZONED_DATE_TIME));
Assert.assertEquals("20:18:18.000000888", DateTimeFormatter.ISO_LOCAL_TIME.format(OFFSET_DATE_TIME));
Assert.assertEquals("20:18:18.000000888", DateTimeFormatter.ISO_LOCAL_TIME.format(LOCAL_DATE_TIME));
}
public void ISO_OFFSET_DATE_TIME() {
Assert.assertEquals("2019-07-07T20:18:18.000000888+09:00",DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(ZONED_DATE_TIME));
Assert.assertEquals("2019-07-07T20:18:18.000000888+09:00",DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(OFFSET_DATE_TIME));
}
public void ISO_OFFSET_DATE() {
Assert.assertEquals("2019-07-07+09:00",DateTimeFormatter.ISO_OFFSET_DATE.format(ZONED_DATE_TIME));
Assert.assertEquals("2019-07-07+09:00",DateTimeFormatter.ISO_OFFSET_DATE.format(OFFSET_DATE_TIME));
}
public void ISO_OFFSET_TIME() {
Assert.assertEquals("20:18:18.000000888+09:00",DateTimeFormatter.ISO_OFFSET_TIME.format(ZONED_DATE_TIME));
Assert.assertEquals("20:18:18.000000888+09:00",DateTimeFormatter.ISO_OFFSET_TIME.format(OFFSET_DATE_TIME));
}
public void ISO_ZONED_DATE_TIME() {
Assert.assertEquals("2019-07-07T20:18:18.000000888+09:00[Asia/Tokyo]", DateTimeFormatter.ISO_ZONED_DATE_TIME.format(ZONED_DATE_TIME));
Assert.assertEquals("2019-07-07T20:18:18.000000888+09:00", DateTimeFormatter.ISO_ZONED_DATE_TIME.format(OFFSET_DATE_TIME));
}
public void ISO_INSTANT() {
Assert.assertEquals("2019-07-07T11:18:18.000000888Z", ZONED_DATE_TIME.format(DateTimeFormatter.ISO_INSTANT));
Assert.assertEquals("2019-07-07T11:18:18.000000888Z", OFFSET_DATE_TIME.format(DateTimeFormatter.ISO_INSTANT));
}
public void ISO_ORDINAL_DATE() {
Assert.assertEquals("2019-188+09:00", ZONED_DATE_TIME.format(DateTimeFormatter.ISO_ORDINAL_DATE));
Assert.assertEquals("2019-188+09:00", OFFSET_DATE_TIME.format(DateTimeFormatter.ISO_ORDINAL_DATE));
Assert.assertEquals("2019-188", LOCAL_DATE_TIME.format(DateTimeFormatter.ISO_ORDINAL_DATE));
}
public void ISO_WEEK_DATE() {
Assert.assertEquals("2019-W27-7+09:00",ZONED_DATE_TIME.format(DateTimeFormatter.ISO_WEEK_DATE));
Assert.assertEquals("2019-W27-7+09:00",OFFSET_DATE_TIME.format(DateTimeFormatter.ISO_WEEK_DATE));
Assert.assertEquals("2019-W27-7",LOCAL_DATE_TIME.format(DateTimeFormatter.ISO_WEEK_DATE));
}
public void ofLocalizedDateTime() {
Assert.assertEquals("2019年7月7日星期日 日本标准时间 下午8:18:18",DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL).withZone(ZoneId.of("Asia/Tokyo")).format(LOCAL_DATE_TIME));
Assert.assertEquals("2019年7月7日 JST 下午8:18:18",DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG).withZone(ZoneId.of("Asia/Tokyo")).format(LOCAL_DATE_TIME));
Assert.assertEquals("2019年7月7日 下午8:18:18",DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM).withZone(ZoneId.of("Asia/Tokyo")).format(LOCAL_DATE_TIME));
Assert.assertEquals("2019/7/7 下午8:18",DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).withZone(ZoneId.of("Asia/Tokyo")).format(LOCAL_DATE_TIME));
Assert.assertEquals("2019年7月7日星期日",DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL).format(LOCAL_DATE_TIME));
Assert.assertEquals("2019年7月7日",DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG).format(LOCAL_DATE_TIME));
Assert.assertEquals("2019年7月7日",DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM).format(LOCAL_DATE_TIME));
Assert.assertEquals("2019/7/7",DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).format(LOCAL_DATE_TIME));
Assert.assertEquals("2019年7月7日星期日 日本标准时间 下午8:18:18",DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL).withZone(ZoneId.of("Asia/Tokyo")).format(ZONED_DATE_TIME));
Assert.assertEquals("2019年7月7日 JST 下午8:18:18",DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG).withZone(ZoneId.of("Asia/Tokyo")).format(ZONED_DATE_TIME));
Assert.assertEquals("2019年7月7日 下午8:18:18",DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM).withZone(ZoneId.of("Asia/Tokyo")).format(ZONED_DATE_TIME));
Assert.assertEquals("2019/7/7 下午8:18",DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).withZone(ZoneId.of("Asia/Tokyo")).format(ZONED_DATE_TIME));
Assert.assertEquals("2019年7月7日星期日",DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL).format(ZONED_DATE_TIME));
Assert.assertEquals("2019年7月7日",DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG).format(ZONED_DATE_TIME));
Assert.assertEquals("2019年7月7日",DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM).format(ZONED_DATE_TIME));
Assert.assertEquals("2019/7/7",DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).format(ZONED_DATE_TIME));
Assert.assertEquals("2019年7月7日星期日 日本标准时间 下午8:18:18",DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL).withZone(ZoneId.of("Asia/Tokyo")).format(OFFSET_DATE_TIME));
Assert.assertEquals("2019年7月7日 JST 下午8:18:18",DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG).withZone(ZoneId.of("Asia/Tokyo")).format(OFFSET_DATE_TIME));
Assert.assertEquals("2019年7月7日 下午8:18:18",DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM).withZone(ZoneId.of("Asia/Tokyo")).format(OFFSET_DATE_TIME));
Assert.assertEquals("2019/7/7 下午8:18",DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).withZone(ZoneId.of("Asia/Tokyo")).format(OFFSET_DATE_TIME));
Assert.assertEquals("2019年7月7日星期日",DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL).format(OFFSET_DATE_TIME));
Assert.assertEquals("2019年7月7日",DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG).format(OFFSET_DATE_TIME));
Assert.assertEquals("2019年7月7日",DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM).format(OFFSET_DATE_TIME));
Assert.assertEquals("2019/7/7",DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).format(OFFSET_DATE_TIME));
}
public void dateToCustomString() {
LocalDateTime localDateTimeExcept = LocalDateTime.of(2019, 7, 7, 20, 18, 18);
DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("第Q季度 yyyy-MM-dd HH:mm:ss", Locale.CHINA);
Assert.assertEquals("第3季度 2019-07-07 20:18:18",formatter1.format(LOCAL_DATE_TIME));
Assert.assertEquals("第3季度 2019-07-07 20:18:18",formatter1.format(ZONED_DATE_TIME));
Assert.assertEquals("第3季度 2019-07-07 20:18:18",formatter1.format(OFFSET_DATE_TIME));
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("第Q季度 yyyy-MM-dd HH:mm:ss", Locale.JAPAN);
Assert.assertEquals("第3季度 2019-07-07 20:18:18",formatter.format(LOCAL_DATE_TIME));
Assert.assertEquals("第3季度 2019-07-07 20:18:18",formatter.format(ZONED_DATE_TIME));
Assert.assertEquals("第3季度 2019-07-07 20:18:18",formatter.format(OFFSET_DATE_TIME));
Assert.assertEquals(localDateTimeExcept,LocalDateTime.parse("第3季度 2019-07-07 20:18:18", formatter));
Assert.assertEquals(localDateTimeExcept,LocalDateTime.parse("第3季度 2019-07-07 20:18:18", formatter1));
}
常用应用场景
把当前时区日期转换到零时区日期
方式1
Date date = new Date();
LocalDateTime localDateTime = date.toInstant().atZone(ZoneId.of("UTC")).toLocalDateTime();
Timestamp timestamp = Timestamp.valueOf(localDateTime);
new Date(timestamp.getTime());
方式2
Date date = new Date();
Date.from(localDateTime.toInstant(ZonedDateTime.now().getOffset()));
把零时区日期转换到当前时区日期
方式1
Date date = new Date();
LocalDateTime localDateTime = date.toInstant().atZone(ZonedDateTime.now().getZone()).toLocalDateTime();
ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(localDateTime.toInstant(ZoneOffset.UTC), ZoneId.systemDefault());
Date.from(zonedDateTime.toLocalDateTime().toInstant(ZoneOffset.ofHours(8)))
DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(zonedDateTime)
方式2
Date date = new Date();
LocalDateTime localDateTime2 = date.toInstant().atOffset(ZonedDateTime.now().getOffset()).toLocalDateTime();
OffsetDateTime offsetDateTime = OffsetDateTime.ofInstant(localDateTime.toInstant(ZoneOffset.UTC), ZoneId.systemDefault());
Date.from(offsetDateTime.toLocalDateTime().toInstant(ZoneOffset.ofHours(8)))
DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(offsetDateTime)
|