Parsing weird date format [duplicate] - java

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.

Related

Java parse Date from string [duplicate]

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);

Convert String date into java.util.Date in the dd/MM/yyyy format [duplicate]

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.

How to convert Google Drive's Date format in java? [duplicate]

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!

Convert epoch String to Java date [duplicate]

This question already has answers here:
Unix epoch time to Java Date object
(7 answers)
Closed 9 years ago.
I am getting an epoch String from my DB which looks something like this : 1391328000000
I am having a hard time trying to convert it to Java Date.
I tried the following :
private String buildDate(String dateString){
System.out.println("dateString " + dateString);
DateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
format.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
String formatted = format.format(Integer.parseInt(dateString));
return formatted;
}
I think you're overthinking about the DateFormat. If I want to simply obtain a Date instance, what I would try is the following:
Date d = new Date(Long.parseLong(dateString));
You need to turn it into a java.util.Date object in order for SimpleDateFormat to handle it. Also, a value like what you quoted needs to be parsed as a long, as it is too large for an int.
That is, change the line where you set formatted to be:
String formatted = format.format(new Date(Long.parseLong(dateString)));
As an aside, if the project you're working on can handle an extra external dependency, switch date/time handling over to the joda library. The stuff in java.util (that is, Date and Calendar) rapidly becomes painful and error-prone to work with.

How to convert date/time strings into Java Calendar objects? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to convert a date String to a Date or Calendar object?
I'm in a pinch here...
I have a series of date/time strings formatted like this:"9-29-2011 9:05 PM PDT"
and I need to convert this string into a java Calendar object.
Once I have this Calendar object it must represent exactly this
date and time (including the AM or PM). Please, what is the best way to accomplish this?
Use DateFormat to parse the String. Pass the Date to the Calendar.setTime() method.
Check out http://joda-time.sourceforge.net/ . Joda Time makes a lot of tricky date/time operations simple!
You will probably want to construct a custom formatter: http://joda-time.sourceforge.net/userguide.html#Input_and_Output
DateTimeFormat.forPattern("dd-MM-yyyy hh:mm a z"); should be close. You can then convert the Joda DateTime to Date or to a Calendar.

Categories

Resources