Am trying to read date field from MongoDB in below format
Formate: YYYY-MM-dd HH:mm:ss.SSSSSS
2017-01-23-10.46.07.812000 - DB2
2017-01-23T16:46:07.812Z - Stored in MongoDB (While viewing from GUI tool)
Mon Jan 23 22:16:07 IST 2017 - Result/Reading from MongoDB
// Formatter for the input date
final DateTimeFormatter inputFormat = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy");
final ZonedDateTime dateFiledParsed = ZonedDateTime.parse(dateFiled.toString(), inputFormat);
final DateTimeFormatter outputFormat3 = DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm:ss.SSSSSS");
System.out.println(outputFormat3.format(publicationDateParsed));
Result: 2017-01-23 22:16:07.000000
In the result 2017-01-23 22:16:07.000000, instead of 000 it should be the 812 (Original value: 2017-01-23-10.46.07.812000)
Note: Using MongoDB Java driver 3.4.
Thank you in advance!
Bharathi
You can use Java's SimpleDateFormat to format the date accordingly. For example, assuming you inserted the date in MongoDB using the proper ISODate type:
> db.test.find()
{
"_id": ObjectId("597813a12dbe1d773beb11d2"),
"date": ISODate("2017-01-23T16:46:07.812Z")
}
This code prints the correct date:
Document doc = collection.find().first();
Date date = doc.getDate("date");
SimpleDateFormat formattedDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
formattedDate.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println(formattedDate.format(date));
Output is:
2017-01-23 16:46:07.812
In my case worked the next code, when passing a date to MongoDb:
SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(dateFormat.format(date));
When retrieving it:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println(dateFormat.format(date));
this 2 methods will match mongo's date format (util.Date in java)
public static String convertToString(Date date) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
dateFormat.setTimeZone(TimeZone.getTimeZone(zone));
return dateFormat.format(date);
}
public static Date convertToDate(String strDate) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
dateFormat.setTimeZone(TimeZone.getTimeZone(zone));
Date parsedDate = null;
try {
parsedDate = dateFormat.parse(strDate);
} catch (ParseException e) {
log.error(e.getMessage());
}
return parsedDate;
}
Related
I have this string: 2018-09-22 10:17:24.772000
I want to convert it to Date:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS");
String sdate = "2018-09-22 10:17:24.772000";
Date dateFrom = simpleDateFormat.parse(sdate);
but it shows: Sat Sep 22 10:17:24 GMT+03:30 2018
Here is what you should do instead, you are printing date object itself, you should print its format.
I will provide the code with old date api and new local date api :
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS");
String sdate = "2018-09-22 10:17:24.772000";
Date dateFrom = simpleDateFormat.parse(sdate);
System.out.println(dateFrom); // this is what you do
System.out.println(simpleDateFormat.format(dateFrom)); // this is what you should do
// below is from new java.time package
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSS");
System.out.println(LocalDateTime.parse(sdate, formatter).format(formatter));
output is :
Sat Sep 22 10:30:16 EET 2018
2018-09-22 10:30:16.000000
2018-09-22 10:17:24.772000
Hope This will help you
public class Utils {
public static void main(String[] args) {
String mytime="2018-09-22 10:17:24.772000";
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss.SSSSSS");
Date myDate = null;
try {
myDate = dateFormat.parse(mytime);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd");
String finalDate = timeFormat.format(myDate);
System.out.println(finalDate);
}
}
Looks to me like you have converted it to a Date. What is your desired result? I suspect what you are wanting to do is to create another Simple date format that shows your expected format and then use simpledateformat2.format(dateFrom)
I should also point out based on past experience that you should add a Locale to your simple date formats otherwise a device with a different language setting may not be able to execute this code
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS", Locale.US);
My time format is coming
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
2108-03-27T17:18:16.985+0530
input Date I have to convert it into other time format
Mar 27,2018 5:18 pm
is expected output can any please suggest me how to convert given time to other time format in java .
SimpleDateFormat sdf = new SimpleDateFormat("MMM d,yyyy h:mm a");
System.out.println(sdf.format(date));
If you have Java 8, use the java.time API:
DateTimeFormatter parser = new DateTimeFormatterBuilder()
.append(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
.appendPattern("XX")
.toFormatter();
OffsetDateTime odt = OffsetDateTime.parse("2108-03-27T17:18:16.985+0530", parser);
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("MMM dd,yyyy h:mm a", Locale.ENGLISH);
String formattedDate = fmt.format(odt); // Mar 27,2108 5:18 PM
SimpleDateFormat has lots of problems, many of them solved by java.time API, and you should prefer to use those.
For older versions of Java, there's a nice backport, with the same classes and functionality.
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy hh:mm a");
System.out.println(sdf.format(new Date()));
//Mar 27, 2018 06:28 PM
for more
What are the date formats available in SimpleDateFormat class?
You can convert type any-to-any of date format.
You just need to pass String format to SimpleDateFormate.
Use this method as static and call it from anywhere by passing inputDate
Simplified Date convert method:
public static String getFormattedDate(String inputDate)
{
String outputFormattedDate = "";
try
{
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z");
Date inputFormatDate = inputFormat.parse(inputDate);
SimpleDateFormat outputFormat = new SimpleDateFormat("MMM dd,yyyy h:mm a");
outputFormattedDate = outputFormat.format(inputFormatDate);
}
catch (Exception ex)
{
outputFormattedDate = inputDate;
ex.printStackTrace();
}
return outputFormattedDate;
}
Hope it will help you.
String startDateStr = "2017-02-03"
DateFormat formatter = new SimpleDateFormat("yyyy-MM-DD",Locale.US);
Date date = (Date)formatter.parse(startDateStr);
2017-02-03 date is parsed to Tue Jan 03 00:00:00 GMT+05:45 2017
Did I
miss something?
Update
I needed a string to be converted to a date object
while maintaining the same format.
The reason for this is I want to make use of public boolean after(Date when) method
This will work ^_^
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd");
DateFormat outputFormat = new SimpleDateFormat("dd MMM yyyy");
String startDateStr ="2017-02-03";
Date date = null;
try {
date = inputFormat.parse(startDateStr);
String startDateStrNewFormat = outputFormat.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
Little explanation of your output :
D is Day in year (1-365)
d is day in month (1-31)
Check the document
Use SimpleDateFormat type for fomatter. You are creating DateFormat object but using SimpleDateFormat.
String startDateStr = "2017-02-03"
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd",Locale.US);
Date date = (Date)formatter.parse(startDateStr);
Yes you missed something. You used DD instead of dd in your yyyy-MM-DD format string. Here is how you do it:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = sdf.format(new Date());
I have a date time (which is a string) in the following format: 2/19/2015 5:25:35 p.m, and I wanted to turn it in the following Date Format: Thu Feb 19 5:25:35 p.m. CET 2015 I tried the following code:
String sDatePrecedenteExecution = "19/02/2015 17:30:29";
SimpleDateFormat format = new SimpleDateFormat ("ddd d mmm yyyy HH: mm: ss");
Date date = format.parse (sDatePrecedenteExecution)
but I got the following error:
java.text.ParseException: unparseable Date: "2/19/2015 5:30:29 p.m."
Has java.text.DateFormat.parse (DateFormat.java:337)
You are currently using the "output" format to read your incoming date string (2/19/2015 5:25:35 p.m), which is why you see the error.
You need to specify a second format for parsing your incoming date string, and use that format to parse instead. It should look like this:
SimpleDateFormat inFormat = new SimpleDateFormat ("dd/MM/yyyy HH:mm:ss")
Date date = inFormat.parse(sDatePrecedenteExecution)
Note that you also have a bug in your output format - m means minutes, and you want MMM, which is months. Have a look at the docs.
Your SimpleDateFormat doesn't match the format which you are entering. They should reflect the same.
Try this code
String parseDate = ""19/02/2015 17:30:29";
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date parsedDate = dateFormat.parse(parseDate);
You need to change your code something like...
String sDatePrecedenteExecution = "19/02/2015 17:30:29";
SimpleDateFormat format = new SimpleDateFormat ("dd/mm/yyyy HH:mm:ss");
try {
Date date = format.parse (sDatePrecedenteExecution);
System.out.println(date);
format = new SimpleDateFormat ("ddd d mmm yyyy HH: mm: ss");
String str = format.format(date);
System.out.println(str);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Try this pattern:
SimpleDateFormat format = new SimpleDateFormat ("dd mm yyyy HH:mm:ss");
You went wrong when you made: "ddd d mmm yyyy HH: mm: ss"
Use this pattern "dd/M/yyyy HH:mm:ss" instead & read the documentation
SimpleDateFormat format = new SimpleDateFormat ("dd/M/yyyy HH:mm:ss");
String sDatePrecedenteExecution = "19/02/2015 17:30:29";
try{date =format.parse (sDatePrecedenteExecution);
}catch(Exception ex){//deal with it here}
System.out.println(date.toString()); //Thu Feb 19 17:30:29 UTC 2015
I'm trying to convert a String that represents a date stored in SQLITE.
The date was stored into sqlite as follows:
Date date;
date.toString();
According with Java documentation, toString() method:
Returns a string representation of this Date. The formatting is
equivalent to using a SimpleDateFormat with the format string "EEE MMM
dd HH:mm:ss zzz yyyy", which looks something like "Tue Jun 22 13:07:00
PDT 1999". The current default time zone and locale are used. If you
need control over the time zone or locale, use SimpleDateFormat
instead.
Until here, it's fine but, when I try to get the String and convert it to date again, Java throws an exception.
The String comes from sqlite:
Mon Jan 20 18:26:25 BRT 2014
So, I do:
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
Date date= sdf.parse("Mon Jan 20 18:26:25 BRT 2014");
What I'm doing wrong?
Thanks.
try this code
String dateString = "here your date";
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date convertedDate = new Date();
try {
convertedDate = dateFormat.parse(dateString);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(convertedDate);
Try this:
String w = "Mon Jan 20 18:26:25 BRT 2014";
SimpleDateFormat pre = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
try{
Date date = pre.parse(w);
System.out.println(sdf.format(date));
}catch(Exception e){
e.printStackTrace();
}
Output:
20/01/2014
Formatter for storing and restoring data value in format dd/MM/yyyy
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
Storing data
String dataAsString = simpleDateFormat.format(date); // 20/01/2014
Restoring data
Date data = simpleDateFormat.parse(dataAsString);