0

I am using MongoDB to save my data and in database i can see the date value like this

ISODate("2016-11-30T11:17:20.945Z")

but when i parse it in front-end it become like

createdOn : 1480582463992

I want to convert "ISODate("2016-11-30T11:17:20.945Z")" it in JAVA and not in js in such a way so that i can get string date value.

Thanks in advance

==============================================================

here is my java code

@Override
public List<Prescription> getcus(
        String id, String cid) {
    List<Prescription> listrescription = null;
    listrescription  = this.patientDBService.getPatientLastThreePrescription(id, cid);

    Prescription prre = new Prescription();
    for(Prescription i : listrescription){ 
        //Date dates = new Date();
        //i.getCreatedOn(); // getting the data from mongo like 1480582463992

    //no clue what to do here to get ISO date as in string
     }
    return listrescription;
}
Aman
  • 806
  • 2
  • 12
  • 38
  • http://stackoverflow.com/questions/3914404/how-to-get-current-moment-in-iso-8601-format – cralfaro Dec 01 '16 at 10:39
  • http://stackoverflow.com/a/18217193/1746118 – Naman Dec 01 '16 at 10:45
  • Possible duplicate of [Converting ISO 8601-compliant String to java.util.Date](http://stackoverflow.com/questions/2201925/converting-iso-8601-compliant-string-to-java-util-date) – Naman Dec 01 '16 at 10:45
  • @nullpointer my question is just opposite of what you have suggested – Aman Dec 01 '16 at 11:03
  • In your question edit I cannot see any connection to MongoDB... So, as far as I understand your question, you query a document from Mongo and have problems getting the saved date value, is that right? If so, show your document (completely) and the code that you use to retrieve the document and the date value from that doc. – mtj Dec 01 '16 at 11:10
  • @mtj the ques is not about mongo but mongo default save the data in ISO format. one who are using mongo can answer this that is why i tagged it with mongo... yes i am getting the data from mongo but is iso format. and since mongo save the date in ISO so it also fetching it in ISO – Aman Dec 01 '16 at 11:18
  • 2
    @adasdasd No, mongo does *not* store a specific format. A date saved in mongo is stored in an internal date representation and only *displayed* in that format, if you use the mongo shell. Each language specific mongo driver will translate this into the specific date-type for the target language (at least the java driver does). – mtj Dec 01 '16 at 11:20
  • @mtj sorry my bad you are right i am saving the data in @DateTimeFormat(iso = ISO.DATE_TIME) protected Date createdOn; @DateTimeFormat(iso = ISO.DATE_TIME) protected Date updatedOn; – Aman Dec 01 '16 at 11:21
  • but this is not the question that how i am saving... the question is now i can i covert ISO in string – Aman Dec 01 '16 at 11:22
  • Read the Date *object* via the mongo driver, convert the date using the methods found here: http://stackoverflow.com/questions/3914404/how-to-get-current-moment-in-iso-8601-format – mtj Dec 01 '16 at 11:35
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/129529/discussion-between-adasdasd-and-mtj). – Aman Dec 01 '16 at 11:41
  • @mtj java 6 or earier version doen't support Z as in different values but 0:00 and also i am still clueless what to do -_- – Aman Dec 01 '16 at 11:57

2 Answers2

0

try this:

 new Date("2016-11-30T11:17:20.945Z")
CompEng
  • 7,161
  • 16
  • 68
  • 122
  • i want to do it with java – Aman Dec 01 '16 at 10:41
  • this will conflict with ISO date format on mongo because new date has its own format... editing the question with java code. can you check it out> – Aman Dec 01 '16 at 10:47
  • new Date with an ISO-Date string? This is neither related to the question nor a legal constructor parameter (apart from the fact that the Date(String) constructor is deprecated.) – mtj Dec 01 '16 at 11:08
0

If you don't care about the time zone you can use this method to parse the date format like "2017-06-19T05:27:26.000Z"

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 "";
}
thilina Kj
  • 1,300
  • 13
  • 25