在java中将未知日期格式的字符串表示形式转换为Date [英] converting string representation of unknown date-format to Date in java
问题描述
- 2015-10-14T16:41:42.000Z
- 2015-10-14T19:01:53.100 + 01:00
- 2015-10-14 05:20:29
或网站可能用于描述元标记日期的任何有效格式(因此格式将为官方,而不是异想天开,但是一组可能性并不小)。
我可以用joda-time来解决这个问题吗?
更新
>我想我找到一个等同于我正在寻找的Javascript
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects /日期
但是我需要一个 Java 的答案。
这不是一个真正的java解决方案,但是如果你发现了一个Javascript,那么你可以使用java中的Javascript解决方案,使用 ScriptEngine 。
只是一点点肮脏...:)
这里是一个示例代码:
code> public static void main(String [] args)throws ScriptException,ParseException {
脚本EngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName(js);
String [] dateStrings = new String [] {
2015-10-14T16:41:42.000Z,
2015-10-14T19:01:53.100 + 01:00 ,
2015-10-14 05:20:29};
SimpleDateFormat sdf = new SimpleDateFormat(yyyy-MM-dd'THH:mm:ss.SSSXXX);
for(String d:dateStrings){
String script =new Date('+ d +');
对象eval = engine.eval(script);
Date parsed = sdf.parse(eval.toString()。replace([Date,).replace(],));
System.out.println(eval + - >+解析);
}
}
打印出:
[Date 2015-10-14T16:41:42.000Z] - > Wed Oct 14 18:41:42 CEST 2015
[Date 2015-10-14T18:01:53.100Z] - > Wed Oct 14 20:01:53 CEST 2015
[Date 2015-10-14T03:20:29.000Z] - > Wed Oct 14 05:20:29 CEST 2015
eval.toString()部分可以明显改善。作为区域设置...
I have a string that represents a date. I do not know the date format of the string. But for example only, it may be any of
- 2015-10-14T16:41:42.000Z
- 2015-10-14T19:01:53.100+01:00
- 2015-10-14 05:20:29
or any valid format that a website may use to describe date in a meta tag (so the format will be official, as opposed to whimsical, but the set of possibilities is not small).
Can I use joda-time to solve this issue? How about java.util.Date
or anything else?
update
I think I find a Javascript equivalent of what I am looking for
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
But I need an answer for Java.
well this is not a real "java" solution, but if you have found a Javascript one, than you can use the Javascript solution in java using the ScriptEngine.
just a little quick and dirty... :)
here is a sample code:
public static void main(String[] args) throws ScriptException, ParseException {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
String[] dateStrings = new String[] {
"2015-10-14T16:41:42.000Z",
"2015-10-14T19:01:53.100+01:00",
"2015-10-14 05:20:29" };
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
for (String d : dateStrings) {
String script = "new Date('" + d + "')";
Object eval = engine.eval(script);
Date parsed = sdf.parse(eval.toString().replace("[Date ", "").replace("]", ""));
System.out.println(eval + " -> " + parsed);
}
}
that prints out:
[Date 2015-10-14T16:41:42.000Z] -> Wed Oct 14 18:41:42 CEST 2015
[Date 2015-10-14T18:01:53.100Z] -> Wed Oct 14 20:01:53 CEST 2015
[Date 2015-10-14T03:20:29.000Z] -> Wed Oct 14 05:20:29 CEST 2015
The eval.toString() part can be improved obviously. as the locale settings...
这篇关于在java中将未知日期格式的字符串表示形式转换为Date的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!