我真的很感激任何人都可以帮忙。 我在字符串中获取日期(2014-11-12T00:00:00Z),现在我想转换为格式(2014-11-12T00:00:00-04:00)。Java字符串日期转换问题
How can we do that in java?
我真的很感激任何人都可以帮忙。 我在字符串中获取日期(2014-11-12T00:00:00Z),现在我想转换为格式(2014-11-12T00:00:00-04:00)。Java字符串日期转换问题
How can we do that in java?
你可以尝试使用JodaTime
String str = "2014-11-12T00:00:00Z";
String pattern = "yyyy-MM-dd'T'HH:mm:ssZ";
DateTimeFormatter dtf = DateTimeFormat.forPattern(pattern);
DateTime dateTime = dtf.parseDateTime(str);
System.out.println(dateTime); //2014-11-12T00:00:00-04:00
编辑:
String s = "2014-11-12T00:00:00Z";
String pattern = "yyyy-MM-dd'T'HH:mm:ssZ";
Date date = new SimpleDateFormat(pattern, Locale.ENGLISH).parse(s);
DateTime d = new DateTime(date.getTime());
System.out.println(d);
使用乔达时库,或类似java.time包内置于Java 8中。
使用proper name指定所需的时区。避免3或4个字母代码,因为它们既不标准也不唯一。
您的字符串格式符合ISO 8601标准。在Joda-Time和java.time中默认使用该标准来解析和生成字符串。
Joda-Time中的示例2.7。
DateTimeZone zone = DateTimeZone.forID("America/Martinique");
DateTime dateTime = new DateTime(yourInputString, zone);
String output = DateTime.toString();
我得到以下例外,当我在线程“主” java.lang.IllegalArgumentException异常以上suggestion.Exception使用:无效的格式:“2014-11-12T00:00:00Z”在“Z格式有误“ \t在org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:673) \t在com.guthyrenker.customizationoffer.transformer.Test.main(Test.java:31) – user3157090 2015-04-03 12:48:45
@ user3157090: - 以上例外是因为某些时区缩写不明确,分析程序无法知道哪个时区的含义。 'z'时区名称是常规(非标准化)和含糊 – 2015-04-03 12:51:20
应该怎么做才能克服这个错误? – user3157090 2015-04-03 12:52:11