'How to convert ISOdate in String in JAVA
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;
}
Solution 1:[1]
try this:
new Date("2016-11-30T11:17:20.945Z")
Solution 2:[2]
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 "";
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ersin Gülbahar |
| Solution 2 | thilina Kj |
