-1

Recently, I have imported CSV file to MongoDB with the date field. The issue is with the date field, I try to change the date field from string to ISO format. currently, it show "2016-07-31T04:18:17.000Z". I don't know how to change this into ISO format using java. I tried this method

        String date1 = dr1.getString("created_at");
        DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy"); 

        Date Created_at =  df.parse(date1);
        dr1.append("created_at", Created_at);
        Brand_Mention(dr1,collection1,new ObjectId(dr1.get("_id").toString()));

Could anyone please help me to solve this!!!

prabhu
  • 103
  • 1
  • 3
  • 15
  • 2
    "2016-07-31T04:18:17.000Z" *is* ISO format... (or an ISO-8601 format, anyway). The second part of your question should be split into a separate question, as it's entirely separate from the formatting side. – Jon Skeet Oct 25 '16 at 14:00
  • @JonSkeet But I failed to query in mongodb in this format – prabhu Oct 25 '16 at 14:56
  • That doesn't make the question any clearer... and you still haven't removed the "split the timings" part, which really shouldn't be there. – Jon Skeet Oct 25 '16 at 14:56
  • It would help if you'd give an example of the *exact* ISO format you're trying to format to. – Jon Skeet Oct 25 '16 at 14:56
  • kindly review http://stackoverflow.com/questions/10942931/converting-string-to-date-in-mongodb – Digital Alchemist Oct 25 '16 at 14:58
  • @JonSkeet ISODate("2016-10-09T11:32:01Z") the format used in MongoDB. While I am trying to perform Aggregation pipeline it works in this (ISODate("2016-10-09T11:32:01Z") format but fails to work for "2016-07-31T04:18:17.000Z" – prabhu Oct 25 '16 at 15:04
  • Well of that, only the 2016-10-09T11:32:01Z part is the ISO format. The rest is Mongo-specific. But it sounds like you just need to drop the milliseconds and add the `ISODate("...")` wrapper. – Jon Skeet Oct 25 '16 at 15:53

1 Answers1

0

Look at java.time.Instant (javadoc) class.

It has method parse() to parse your string. And then you can use LocalTime.from() to convert instant time point to local time (that is hours and minutes and seconds without date or timezone).

Then you can compare local times to find needed time range.

AlexZam
  • 1,147
  • 7
  • 18
  • I have tried this method"" – prabhu Oct 25 '16 at 14:22
  • String date1 = dr1.getString("created_at"); DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy"); Date Created_at = df.parse(date1); dr1.append("created_at", Created_at); Brand_Mention(dr1,collection1,new ObjectId(dr1.get("_id").toString())); – prabhu Oct 25 '16 at 14:23
  • But as you see your string "2016-07-31T04:18:17.000Z" is in ISO format, not in "EEE MMM dd HH:mm:ss Z yyyy". So no need in SimpleDateFormat, use just Instant.parse(dr1.getString("created_at")). – AlexZam Oct 26 '16 at 14:28
  • Also I should note that in Java coding conventions variables and methods are named with small first letter and in camel case. "createdAt", not "Created_at", also "brandMention" instead of "Brand_Mention". – AlexZam Oct 26 '16 at 14:30
  • Thank you!!! I solved this using Javascript. – prabhu Oct 26 '16 at 15:55