Converting the date returned from mongo to this format 2015-10-25

送分小仙女□ 提交于 2019-12-12 02:55:28

问题


I have a json file returned from mongo db as follow:

[
{
    "_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

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?

Update :

Here is the answer :

converting date from one format to another does not work

thanks a lot for helping


回答1:


Why don't you use the DateTimeFormatter? Then you can parse your dates this way:

// 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 article provides 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).

Edit: Updated the formatter after input from the OP




回答2:


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.

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 "";
}



回答3:


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);


来源:https://stackoverflow.com/questions/27803876/converting-the-date-returned-from-mongo-to-this-format-2015-10-25

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!