我有这个格式的日期:
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.p(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.p("Tue Mar 03 00:00:00 WIB 2015"));
System.out.println("Date in Indonesia: " + s); // 2015-03-03
本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处
评论列表(39条)