日期格式问题从java [英] The date format issue from java
问题描述
我从一个joynet云api服务器获取日期格式:
2012-11-20T10:26:04 + 00 :00
但是,我不知道处理最后一个细分 +00:00 ,除了+00:00之外,我已格式化格式。
SimpleDateFormat fmt = new SimpleDateFormat(yyyy-MM -dd'T'HH:mm:ss);
pre>
日期date = fmt.parse(2012-11-20T10:26:04);
感谢@Abu
我将其重写为删除:,
SimpleDateFormat fmt = new SimpleDateFormat(yyyy-MM-dd'T'HH:mm:ssZ );
String input =2012-11-20T10:25:58 + 00:00;
String s1 = input.split(T)[0];
String s2 = input.split(T)[1];
String sep = null;
if(s2.contains(+)){
sep =+ ;
}
if(s2.contains( - )){
sep = - ;
}
String s3 = s2.split( \\+ sep)[0];
String s4 = s2.split(\\+ sep)[1] .re place(:,);
String cleanDate = s1 +T+ s3 + sep + s4;
Date date = fmt.parse(cleanDate);
System.out.println(date);
解决方案删除
code>如果你不使用java 7
并使用这个:
SimpleDateFormat fmt = new SimpleDateFormat(yyyy-MM-dd'T'HH:mm:ssZ);
请参阅javadoc for Java 6中的SimpleDateFormat
和
如果您使用Java 7,则直接使用:
SimpleDateFormat fmt = new SimpleDateFormat(yyyy-MM-dd'THH:mm:ssX);
请参阅javadoc for Java 7中的SimpleDateFormat
I get the date format from a joynet cloud api server:
2012-11-20T10:26:04+00:00"
However, I have no idea to handle the last segment +00:00, I have made the format except for +00:00
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); Date date = fmt.parse("2012-11-20T10:26:04");
Thanks for @Abu
I rewrite it to remove ":",
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"); String input = "2012-11-20T10:25:58+00:00"; String s1 = input.split("T")[0]; String s2 = input.split("T")[1]; String sep = null; if (s2.contains("+")) { sep = "+"; } if (s2.contains("-")) { sep = "-"; } String s3 = s2.split("\\" + sep)[0]; String s4 = s2.split("\\" + sep)[1].replace(":", ""); String cleanDate = s1 + "T" + s3 + sep + s4; Date date = fmt.parse(cleanDate); System.out.println(date);
解决方案Remove that
:
inside the time zone part if you are not using java 7and use this :
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
See javadoc for SimpleDateFormat in Java 6
And
If you are using Java 7 then directly use this :
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
See javadoc for SimpleDateFormat in Java 7
这篇关于日期格式问题从java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!