java 将 mongo 返回的日期转换为这种格式 2015-10-25
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27803876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting the date returned from mongo to this format 2015-10-25
提问by Hamed Minaee
I have a json file returned from mongo db as follow:
我有一个从 mongo db 返回的 json 文件,如下所示:
[
{
"_id": {
"$date": "2014-10-19T04:00:00.000Z"
},
"value": 29
},
{
"_id": {
"$date": "2014-10-20T04:00:00.000Z"
},
"value": 20
},
{
"_id": {
"$date": "2014-10-21T04:00:00.000Z"
},
"value": 21
}
]
Now I want to read the date in java in the following format: 2014/10/25
现在我想以以下格式读取java中的日期:2014/10/25
but when I use:
但是当我使用:
System.out.println("DAte is : "+result.get("_id").toString() );
the result is :
结果是:
DAte is : Sun Oct 19 01:00:00 ADT 2014
Then only thing that comes to my mind is to use substring and manually convert the date to 2014-10-25 but I am sure there would better way. Does any one have any idea?
然后我唯一想到的是使用子字符串并手动将日期转换为 2014-10-25 但我相信会有更好的方法。有谁有想法吗?
Update :
更新 :
Here is the answer :
这是答案:
converting date from one format to another does not work
thanks a lot for helping
非常感谢您的帮助
回答by thilina Kj
You can use this method to parse the mongo date format. But in here we are neglecting time zone. So use this if the time zone doesn't matter.
您可以使用此方法来解析 mongo 日期格式。但在这里我们忽略了时区。因此,如果时区无关紧要,请使用它。
private static String convertMongoDate(String val){
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
SimpleDateFormat outputFormat= new SimpleDateFormat("yyyy/MM/dd");
try {
String finalStr = outputFormat.format(inputFormat.parse(val));
System.out.println(finalStr);
return finalStr;
} catch (ParseException e) {
e.printStackTrace();
}
return "";
}
回答by wassgren
Why don't you use the DateTimeFormatter
? Then you can parse your dates this way:
你为什么不使用DateTimeFormatter
?然后你可以这样解析你的日期:
// The test string
String str = "Sun Oct 19 01:00:00 ADT 2014";
// Formatter for the input date
final DateTimeFormatter inputFormat =
DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy");
// The parsed date
final ZonedDateTime parsed = ZonedDateTime.parse(str, inputFormat);
// The output format(s). Specify the one you need
final DateTimeFormatter outputFormat1 = DateTimeFormatter.ofPattern("yyyy/MM/dd");
final DateTimeFormatter outputFormat2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
// Print
System.out.println(outputFormat1.format(parsed)); // -> 2014/10/19
System.out.println(outputFormat2.format(parsed)); // -> 2014-10-19
This articleprovides some good reading for how to parse and format dates. The class DateTimeFormatter
is available for Java 8, if you are using older versions of Java the class SimpleDateFormat
may be used instead (it uses a similar strategy for handling dates but is unfortunately not thread-safe).
这篇文章提供了一些关于如何解析和格式化日期的好读物。该类DateTimeFormatter
可用于 Java 8,如果您使用的是旧版本的 Java,SimpleDateFormat
则可以使用该类代替(它使用类似的策略来处理日期,但不幸的是不是线程安全的)。
Edit:Updated the formatter after input from the OP
编辑:从 OP 输入后更新格式化程序
回答by RE350
I would suggest you to do something like below.
我建议你做类似下面的事情。
SimpleDateFormat original = new SimpleDateFormat("yyyy-MM-ddThh:mm:sssZ");
SimpleDateFormat output= new SimpleDateFormat("yyyy/MM/dd");
String isoFormat = original.format(result.get("_id"));
Date d = original.parse(isoFormat);
String formattedTime = output.format(d);
System.out.println(formattedTime);