问题描述
我的时间是-> 2020年9月15日11:10:25
我正在使用此代码将其格式化为日语
timeFormatStr = "YYYY MMMMMMMMMM DD HH:mm:ss z";
SimpleDateFormat sd = new SimpleDateFormat(timeFormatStr,locale);
timeStr = sdf.format(new Date(time));
timeStr看起来像这样(看起来不正确)。
2020 9月259 23:10:25 UTC
您知道格式字符串应该是什么吗?我检查了语言环境是-ja_JP.eucjp
谢谢
解决方法
YYYY MMMMMMMMMM DD HH:mm:ss z
不是日语如何设置日期和时间的格式。您应该使用DateTimeFormatter
,然后调用ofLocalizedDateTime
和withLocale
。这将产生一个格式化程序,该格式化程序以本地日语格式生成字符串。
String formatted = DateTimeFormatter
.ofLocalizedDateTime(FormatStyle.FULL) // choose a style here
.withLocale(Locale.JAPANESE)
.format(new Date(time).toInstant().atZone(ZoneOffset.UTC)); // choose a timezone here
System.out.println(formatted); // 1970年1月1日木曜日 0時00分00秒 Z
您实际上不应再使用Date
。您应该直接给DateTimeFormatter
一个ZonedDateTime
。