前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >罗列日期的各种String格式

罗列日期的各种String格式

原创
作者头像
张紫娃
修改2024-04-22 08:08:08
2260
修改2024-04-22 08:08:08
举报

前提

代码语言:javascript
复制
LocalDateTime LOCAL_DATE_TIME = LocalDateTime.of(2019, 7, 7, 20, 18, 18, 888);
ZonedDateTime ZONED_DATE_TIME = ZonedDateTime.of(2019, 7, 7, 20, 18, 18, 888, ZoneId.of("Asia/Tokyo"));
OffsetDateTime OFFSET_DATE_TIME = OffsetDateTime.of(2019, 7, 7, 20, 18, 18, 888, ZoneOffset.ofHours(9));

Timestamp to string

代码语言:javascript
复制
Timestamp timestamp = new Timestamp(1562501898888L);
Assert.assertEquals("2019-07-07 20:18:18.888", timestamp.toString());  【当前时区】

DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Assert.assertEquals("2019-07-07 20:18:18", df.format(timestamp));      【当前时区】

Date to string

代码语言:javascript
复制
Date DATE = new Date(1562501898000L); // Sun Jul 07 20:18:18 CST 2019 【当前时区】

DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Assert.assertEquals("2019-07-07 20:18:18", df.format(DATE));          【当前时区】

df.setTimeZone(TimeZone.getTimeZone("Asia/Tokyo"));
Assert.assertEquals("2019-07-07 21:18:18", df.format(DATE));          【指定时区】 

// 本地时间,已经@Deprecated
System.out.println(DATE.toLocaleString()); // 2019年7月7日 下午8:18:18 【当前时区】

// GTM时间(格林威治这个时候的时间),已经@Deprecated
System.out.println(DATE.toGMTString());// 7 Jul 2019 12:18:18 GMT     【0时区】

ISO_DATE_TIME、ISO_DATE、ISO_TIME

代码语言:javascript
复制
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));

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));

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));

ISO_OFFSET_DATE_TIME、ISO_OFFSET_DATE、ISO_OFFSET_TIME

代码语言:javascript
复制
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));
//DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(LOCAL_DATE_TIME);//java.time.temporal.UnsupportedTemporalTypeException: Unsupported field:

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));
//DateTimeFormatter.ISO_OFFSET_DATE.format(LOCAL_DATE_TIME);//java.time.temporal.UnsupportedTemporalTypeException: Unsupported field:

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));
//DateTimeFormatter.ISO_OFFSET_TIME.format(LOCAL_DATE_TIME);//java.time.temporal.UnsupportedTemporalTypeException: Unsupported field:

ISO_ZONED_DATE_TIME

代码语言:javascript
复制
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));
//DateTimeFormatter.ISO_ZONED_DATE_TIME.format(LOCAL_DATE_TIME);//java.time.temporal.UnsupportedTemporalTypeException: Unsupported field:

//DateTimeFormatter.ISO_ZONED_DATE.format(ZonedDateTime.now());// 不存在ISO_ZONED_DATE
//DateTimeFormatter.ISO_ZONED_TIME.format(ZonedDateTime.now());// 不存在ISO_ZONED_TIME

ISO_INSTANT 【会算0时区】

代码语言:javascript
复制
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));
//LOCAL_DATE_TIME.format(DateTimeFormatter.ISO_INSTANT);//java.time.temporal.UnsupportedTemporalTypeException: Unsupported field:

ISO_ORDINAL_DATE (DAY_OF_YEAR)

代码语言:javascript
复制
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));

ISO_WEEK_DATE (DAY_OF_WEEK)

代码语言:javascript
复制
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));

FormatStyle

代码语言:javascript
复制
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日星期日 日本标准时间 下午8:18:18", DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL).withZone(ZoneId.of("Asia/Tokyo")).format(OFFSET_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(LOCAL_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日 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(LOCAL_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: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(LOCAL_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 下午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(LOCAL_DATE_TIME));
Assert.assertEquals("2019年7月7日星期日", DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL).format(ZONED_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(LOCAL_DATE_TIME));
Assert.assertEquals("2019年7月7日", DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG).format(OFFSET_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(LOCAL_DATE_TIME));
Assert.assertEquals("2019年7月7日", DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM).format(OFFSET_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(LOCAL_DATE_TIME));
Assert.assertEquals("2019/7/7", DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).format(OFFSET_DATE_TIME));
Assert.assertEquals("2019/7/7", DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).format(ZONED_DATE_TIME));
t

to custom string

代码语言:javascript
复制
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));

LocalDateTime localDateTimeExcept = LocalDateTime.of(2019, 7, 7, 20, 18, 18);
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));

//Assert.assertEquals(localDateTime1,ZonedDateTime.parse("第3季度 2019-07-07 20:18:18", formatter));java.time.format.DateTimeParseException
//Assert.assertEquals(localDateTime1,ZonedDateTime.parse("第3季度 2019-07-07 20:18:18", formatter1));java.time.format.DateTimeParseException

//Assert.assertEquals(localDateTime1,OffsetDateTime.parse("第3季度 2019-07-07 20:18:18", formatter));java.time.format.DateTimeParseException
//Assert.assertEquals(localDateTime1,OffsetDateTime.parse("第3季度 2019-07-07 20:18:18", formatter1));java.time.format.DateTimeParseException

特殊”的匹配类型

代码语言:javascript
复制
Date DATE = new Date(1562501898000L); // Sun Jul 07 20:18:18 CST 2019

DateFormat dfChina = new SimpleDateFormat("G GG GGGGG E EE EEEEE a aa aaaaa", Locale.CHINA);
System.out.println(dfChina.format(DATE)); // 公元 公元 公元 周日 周日 星期日 下午 下午 下午
System.out.println(dfChina.parse("公元 公元 公元 周日 周日 星期日 下午 下午 下午")); // Sun Jan 04 12:00:00 CST 1970

DateFormat dfUs = new SimpleDateFormat("G GG GGGGG E EE EEEEE a aa aaaaa", Locale.US);
System.out.println(dfUs.format(DATE)); // AD AD AD Sun Sun Sunday PM PM PM
System.out.println(dfUs.parse("AD AD AD Sun Sun Sunday PM PM PM")); // Sun Jan 04 12:00:00 CST 1970

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前提
  • Timestamp to string
  • Date to string
  • ISO_DATE_TIME、ISO_DATE、ISO_TIME
  • ISO_OFFSET_DATE_TIME、ISO_OFFSET_DATE、ISO_OFFSET_TIME
  • ISO_ZONED_DATE_TIME
  • ISO_INSTANT 【会算0时区】
  • ISO_ORDINAL_DATE (DAY_OF_YEAR)
  • ISO_WEEK_DATE (DAY_OF_WEEK)
  • FormatStyle
  • to custom string
  • 特殊”的匹配类型
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档