SimpleDateFormat 中的印度尼西亚时间
Indonesian Times in SimpleDateFormat
我有一个这种格式的约会:
Tue Mar 03 00:00:00 WIB 2015
如何格式化成:
2015-03-03
我目前尝试的:
val today_date = new LocalDate()
val formatIncoming = new java.text.SimpleDateFormat("EEE MMM dd HH:mm:ss z YYYY")
val formatOutgoing = new java.text.SimpleDateFormat("yyyy-MM-dd")
formatOutgoing.format(formatIncoming.parse(data.cheque_date.toString))
//output
2014-12-30
有什么解决办法吗?
首先要注意: 类名 LocalDate
看起来像 Joda-Time 但你必须使用 SimpleDateFormat
因为 Joda-Time 无法解析时区名称或缩写。
其次:您的解析模式错误,使用 YYYY 而不是 yyyy(Y 是星期几的年份,而不是正常的日历年)。使用 Y 会导致错误的日期输出。
第三: 必须将语言环境指定为英语,因为您的输入中有英文标签(特别是如果您的默认语言环境不是英语 - 否则会出现异常).
第四: 模式字母 z 是正确的,将处理像 "WIB" 这样的时区名称。再次说明:在这里指定语言环境很重要。在给出的另一个答案中使用 "Z" 通常表示时区偏移,但允许根据 Javadoc 解析时区名称。因此,为了清楚起见,我建议 "z"(正如您所做的那样)。
SimpleDateFormat formatIncoming =
new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
SimpleDateFormat formatOutgoing = new SimpleDateFormat("yyyy-MM-dd");
TimeZone tz = TimeZone.getTimeZone("Asia/Jakarta");
System.out.println(tz.getDisplayName(false, TimeZone.SHORT, Locale.ENGLISH)); // WIB
formatOutgoing.setTimeZone(tz);
String s = formatOutgoing.format(formatIncoming.parse("Tue Mar 03 00:00:00 WIB 2015"));
System.out.println("Date in Indonesia: " + s); // 2015-03-03
我有一个这种格式的约会:
Tue Mar 03 00:00:00 WIB 2015
如何格式化成:
2015-03-03
我目前尝试的:
val today_date = new LocalDate()
val formatIncoming = new java.text.SimpleDateFormat("EEE MMM dd HH:mm:ss z YYYY")
val formatOutgoing = new java.text.SimpleDateFormat("yyyy-MM-dd")
formatOutgoing.format(formatIncoming.parse(data.cheque_date.toString))
//output
2014-12-30
有什么解决办法吗?
首先要注意: 类名 LocalDate
看起来像 Joda-Time 但你必须使用 SimpleDateFormat
因为 Joda-Time 无法解析时区名称或缩写。
其次:您的解析模式错误,使用 YYYY 而不是 yyyy(Y 是星期几的年份,而不是正常的日历年)。使用 Y 会导致错误的日期输出。
第三: 必须将语言环境指定为英语,因为您的输入中有英文标签(特别是如果您的默认语言环境不是英语 - 否则会出现异常).
第四: 模式字母 z 是正确的,将处理像 "WIB" 这样的时区名称。再次说明:在这里指定语言环境很重要。在给出的另一个答案中使用 "Z" 通常表示时区偏移,但允许根据 Javadoc 解析时区名称。因此,为了清楚起见,我建议 "z"(正如您所做的那样)。
SimpleDateFormat formatIncoming =
new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
SimpleDateFormat formatOutgoing = new SimpleDateFormat("yyyy-MM-dd");
TimeZone tz = TimeZone.getTimeZone("Asia/Jakarta");
System.out.println(tz.getDisplayName(false, TimeZone.SHORT, Locale.ENGLISH)); // WIB
formatOutgoing.setTimeZone(tz);
String s = formatOutgoing.format(formatIncoming.parse("Tue Mar 03 00:00:00 WIB 2015"));
System.out.println("Date in Indonesia: " + s); // 2015-03-03