This question already has answers here:
Converting ISO 8601-compliant String to java.util.Date
(31 answers)
Closed 8 years ago.
This is what I tried so far but is not working:
This is the format of the date return by the Drive api's json response
2014-04-29T17:58:02.437Z
final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz");
System.out.println(df.parse("2014-04-29T17:58:02.437Z"));
What is the correct way to convert it?
Quick suggestion for:
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(sdf.parse("2014-04-29T17:58:02.437Z"));
As you see, Z is simply set by GMT, while you was not parsing 437 milliseconds.
Works for me, just tried it out!
Related
This question already has answers here:
Java string to date conversion
(17 answers)
Closed 5 years ago.
I've a problem with date. I've a date as string in format: "2017-05-10 16:30"
I'd like to convert it to date looking the same as I wrote before.
Please help me, thanks in advance!
You can use LocalDateTime.parse to create a LocalDateTime object, but the second part of your question didn't really make sense. The date object itself doesn't have a format, so it can't "look like" anything. You decide what format to adopt when you convert it back to a string.
A simple search in google or stackoverflow might lead to answer but here you go.
Pre Java 8
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse("2017-05-10 16:30");
Java 8
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime ldt = LocalDateTime.parse("2017-05-10 16:30", dtf);
This question already has answers here:
Convert unix timestamp to date in java [duplicate]
(3 answers)
Closed 5 years ago.
I receive date and time from openweather api in such format "dt":1489602000", I don't know what that mean. I want to receive something like this "2014-07-23 09:00:00" How can I transform it using SimpleDateFormat
String transformedDate = new SimpleDateFormat("H:mm dd MMM yy").format(new Date(yourTime*1000));
Customize your date format like there
This question already has answers here:
Convert String to java.util.Date
(4 answers)
Closed 7 years ago.
I read more questions on the web but I dont' find a solution yet.
I have a String like "14/05/1994", exactly in this format.
I need to covert it into java.util.Date in the same format.
I tried:
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Date dataFrom = new Date();
dataFrom = df.format("14/05/1994");
But the result is: Sat May 14 00:00:00 CET 1994
It's possibile have as a result: 14/05/1994 not as a String, but as java.util.Date?
A java.util.Date doesn't have a format. It's just a date.
When you print it out, e.g. using toString(), it uses a default format, which is what you're seeing. But you have that date.
Date dataFrom = new Date();
dataFrom = df.format("14/05/1994");
I don't think that can be your code because DateFormat.format accepts a Date and returns a String, not the other way around. You might mean df.parse, which would get you the results you describe. But if you take your SimpleDateFormat and use its format method on the Date, then you should get back out 14/05/1994 as you want. A java.util.Date doesn't have a format, though.
This question already has answers here:
Converting ISO 8601-compliant String to java.util.Date
(31 answers)
Closed 7 years ago.
I have this weird date format that I have to parse.
2015-12-18T03:36:06.000+0000
I am currently mapping a regex to date formats so I can parse different dates. However, this format got me confused. Any help appreciated.
To parse a String into a Date in Java, you use a DateFormat object, and specify the format the date is in. There is no need to use a Regex, the Java library has a way to do this for you.
final DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
final Date d = df.parse("2015-12-18T03:36:06.000+0000"); // From your code above
System.out.println(d);
See the JavaDoc for SimpleDateFormat for more explanation as to what the symbols mean. This is actually a common format for dates called ISO 8601, I just took the pattern right from the documentation.
Watch out! These DateFormat objects are not threadsafe.
This question already has answers here:
java.util.Date format conversion yyyy-mm-dd to mm-dd-yyyy
(8 answers)
Closed 7 years ago.
Has java any function where I have a String with a date in this form : ddMMyy or dd.MM.yy
and I want to convert it in another format like: dd:MM:yy
I would suggest using two SimpleDateFormat objects for this.
Date date = new SimpleDateFormat("ddMMyy").parse( dateString);
String result = new SimpleDateFormat("dd:MM:yy").format( date );
For the format, see the documentation: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html