How to Parse Date in java [duplicate] - java

This question already has answers here:
How can I convert Date.toString back to Date?
(5 answers)
Closed 4 years ago.
Unparseable date: "Tue Jul 03 16:59:51 IST 2018" Exception
String date="Tue Jul 03 16:59:51 IST 2018";
i want to parse it.
My code is
SimpleDateFormat newformat=SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
Date d=newformat.parse(date);

You are using old and quite problematic classes, especially Date.
For your example, perhaps consider using LocalDateTime
String date = "Tue Jul 03 16:59:51 IST 2018";
DateTimeFormatter format = DateTimeFormatter
.ofPattern("EEE MMM dd HH:mm:ss z yyyy");
LocalDateTime dateTime = LocalDateTime.parse(date, format);

Since your pattern has to match the string you want to parse you need to adjust the pattern as following:
EEE MMM dd HH:mm:ss z yyyy
Infos gathered from the docs.
You should also use the new java.time API introduced with Java 8.
String s = "Tue Jul 03 16:59:51 IST 2018";
//Java 7 way
SimpleDateFormat newformat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Date d = newformat.parse(s);
//Java 8 way
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss z yyyy");
LocalDateTime dateTime = LocalDateTime.parse(s, formatter);

Related

Java unparsable date SimpleDateFormat [duplicate]

This question already has answers here:
How to get date from date time in java
(6 answers)
Why can't this SimpleDateFormat parse this date string?
(4 answers)
how to parse output of new Date().toString()
(4 answers)
Java - Unparseable date
(3 answers)
Closed 3 years ago.
I have a date that looks like that:
Sun Dec 29 00:24:09 CET 2019
I have a little utility method that parses a string date from a format to another:
public String formatDate(String date, String fromFormat, String toFormat) throws Exception {
SimpleDateFormat from = new SimpleDateFormat(fromFormat);
SimpleDateFormat to = new SimpleDateFormat(toFormat);
return to.format(from.parse(date));
}
However, with above date format, I do not find the correct date pattern to indicate to my method.
According to SimpleDateFormat patterns documentation, it should be (if I am not mistaken), the following (for Sun Dec 29 00:24:09 CET 2019):
"E M d HH:mm:ss z yyyy"
However, it throws the following exception:
java.text.ParseException: Unparseable date: "Sun Dec 29 00:24:09 CET 2019"
at java.text.DateFormat.parse(DateFormat.java:366)
at com.aptar.simulator.Utils.formatDate(Utils.java:60)
The method is called like this:
formatDate(exDate, "E M d HH:mm:ss z yyyy", "dd-MM-yyyy HH:mm:ss");
Where
exDate = "Sun Dec 29 00:24:09 CET 2019"
Try below solution -
formatDate("Sun Dec 29 00:24:09 CET 2019","EEE MMM d HH:mm:ss z yyyy","dd-MM-yyyy HH:mm:ss");
Format should be - "EEE MMM d HH:mm:ss z yyyy"
You should use EEE for Sun and MMM for Dec
hope this helps.
Date format should be
EEE MMM dd HH:mm:ss z yyyy
Your code works fine using this format.
using java.time API
LocalDate.parse(datestr, DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss z yyyy")).format("TO DATE PATTERN");
Further details at Using java.time package to format date
Please find the code snippet below to solve your problem. The issue was the letter codes were correct, but there was character count mismatch , hence causing the issue. E.g.:Sun has three chars, but you were using a single E in your formatter.
public class Examp167 {
public static String formatDate(String date, String fromFormat, String toFormat) throws Exception {
SimpleDateFormat from = new SimpleDateFormat(fromFormat);
SimpleDateFormat to = new SimpleDateFormat(toFormat);
return to.format(from.parse(date));
}
public static void main(String[] args) throws Exception{
String exDate = "Sun Dec 29 00:24:09 CET 2019";
System.out.println( formatDate(exDate, "EEE MMM dd HH:mm:ss zzz yyyy", "dd-MM-yyyy HH:mm:ss"));
}
}
Firs use DateTimeFormatter instead of an old outdated class, then you should set the Locale since the day and month names are in English and last the in format needs to be MMM instead of M for the month
public static String formatDate(String date, String fromFormat, String toFormat) throws Exception {
DateTimeFormatter inFormatter = DateTimeFormatter.ofPattern(fromFormat, Locale.US);
DateTimeFormatter outFormatter = DateTimeFormatter.ofPattern(toFormat, Locale.US);
return outFormatter.format(inFormatter.parse(date));
}
Example:
String exDate = "Sun Dec 29 00:24:09 CET 2019";
String out = formatDate(exDate, "E MMM d HH:mm:ss z yyyy", "dd-MM-yyyy HH:mm:ss");
System.out.println(out);
29-12-2019 00:24:09

How to parse date string EEE MMM dd to yyyy-MM-dd in java [duplicate]

This question already has answers here:
Change date format in a Java string
(22 answers)
how to parse output of new Date().toString()
(4 answers)
Closed 3 years ago.
I'm having the date in this pattern EEEEE MMMMM yyyy HH:mm:ss.SSSZ, I would like to convert this to yyyy-MM-dd format in java, I tried below approach but I'm getting this exception java.text.ParseException: Unparseable date:
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
String dateInString = "Sun Oct 01 00:00:00 EDT 2017";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
Can someone please help me to resolve this?
Thanks.
From java-8 you can use the ZonedDateTime with pattern of your input date which is EEE MMM dd HH:mm:ss zzz yyyy
String dateInString = "Sun Oct 01 00:00:00 EDT 2017";
ZonedDateTime time = ZonedDateTime.parse(dateInString,DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy"));
System.out.println(time.toLocalDate()); //2017-10-01
By default LocalDate is without a time-zone in the ISO-8601 calendar system, such as 2007-12-03.
You've defined your formatter as the concept 'date, month, year', and then you try to ask it to parse a string that isn't in this format at all. You need to make a formatter that can format Sun Oct 01 00:00:00 EDT 2017, and dd-MMM-yyyy obviously isn't it. The javadoc of SimpleDateFormat will tell you what combination of letters you need to use.
Once you've got that, it's easy: parse with this new formatter, then call .format with your old one (the dd-MMM-yyyy one).
You can't use the same formatter for both parsing and formatting. See this answer: https://stackoverflow.com/a/999191
You double-create the DateFormat one parse and once to format
DateFormat dfParse = new SimpleDateFormat("EEEEE MMMMM yyyy HH:mm:ss.SSSZ");
DateFormat dfFormat = new SimpleDateFormat("yyyy-MM-dd");
dfFormat.format(dfParse.parse("Sun Oct 01 00:00:00 EDT 2017"))

I have been trying to convert "Mon Jan 21 2019 20:06:48 GMT-0400 (EDT)" into (01/21/2019 20:06:48 ) this format In java [duplicate]

This question already has answers here:
Java Date Validator with Time Zone
(1 answer)
Change date format in a Java string
(22 answers)
Closed 3 years ago.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM dd yyyy HH:mm:ss");
OR
java.text.DateFormat dateFormat = new java.text.SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss");
I have the format of time in data bases tables and I have to convert them into yyyy-MM-dd HH:mm:ss or MM/dd/YYYY HH:mm:ss
Input : Mon Jan 21 2019 20:06:48 GMT-0400 (EDT)
Expected output : 01/21/2019 20:06:48
21 January, 2019 is a Monday not a Thursay, Java'll throws an error when parsing
You need an formatter to parse your string, and a formatter to print the date like you want
if your system use by default Locale.ENGLISH you can remove it) :
String input = "Mon Jan 21 2019 20:06:48 GMT-0400 (EDT)";
DateTimeFormatter inFormatter = DateTimeFormatter.ofPattern("EE MMM dd yyyy HH:mm:ss 'GMT'xx (z)");
DateTimeFormatter outFormatter = DateTimeFormatter.ofPattern("MM/dd/yy HH:mm:ss");
OffsetDateTime od = OffsetDateTime.parse(input, inFormatter);
System.out.println(od); // 2019-01-21T20:06:48-04:00
System.out.println(od.format(outFormatter)); // 01/21/19 20:06:48

Java date does not parse day [duplicate]

This question already has answers here:
Date format parse exception - "EEE MMM dd HH:mm:ss Z yyyy" [duplicate]
(3 answers)
format date from "MMM dd, yyyy HH:mm:ss a" to "MM.dd
(2 answers)
Closed 7 years ago.
I have a weird problem, when I try to parse this date: Tue Nov 03 10:50:16 CET 2015 using the java SimpleDateFormat, it throws an exception because of the "Tue" in there.
My code is:
String date = "Tue Nov 03 10:50:16 CET 2015";
Date parsedDate = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy").parse(date);
Which throws this exception: java.text.ParseException: Unparseable date: "Tue Nov 03 10:50:16 CET 2015"
I have tried debugging it, and this is what is boils down to:
String date = "Tue";
Date parsedDate = new SimpleDateFormat("EEE").parse(date);
Which throws the same type of exception. (I also tried it with a single 'E'). I think this is really strange, because the documentation tells me that this is how it should be used. Source: https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html
Solutions are more than welcome!
Thanks, Luca
Update: the point of the parsing is to parse MANIFEST.getMainAttributes().getValue("Created-On");
Thanks to cheffe I figured out the solution:
Date parsedDate = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH).parse(date);
Which worked! :)

Date format parse exception - "EEE MMM dd HH:mm:ss Z yyyy" [duplicate]

This question already has answers here:
How to convert date in to yyyy-MM-dd Format?
(6 answers)
How can I convert Date.toString back to Date?
(5 answers)
Java - Unparseable date
(3 answers)
Closed 5 years ago.
I got problem with date parse example date:
SimpleDateFormat parserSDF=new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy", Locale.getDefault());
parserSDF.parse("Wed Oct 16 00:00:00 CEST 2013");
got exception
Exacly I want parse this format date to yyyy-MM-dd
I try:
SimpleDateFormat parserSDF = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
Date date = parserSDF.parse("Wed Oct 16 00:00:00 CEST 2013");
take :
java.text.ParseException: Unparseable date: "Wed Oct 16 00:00:00 CEST 2013"
OK I change to and works :
SimpleDateFormat parserSDF = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy", Locale.ENGLISH);
Date date = parserSDF.parse("Wed Oct 16 00:00:00 CEST 2013");
I'm going to assume that Locale.getDefault() for you is pl-PL since you seem to be in Poland.
English words in date strings therefore cause an unparseable date.
An appropriate Polish date String would be something like
"Wt paź 16 00:00:00 -0500 2013"
Otherwise, change your Locale to Locale.ENGLISH so that the SimpleDateFormat object can parse String dates with English words.
Instead of using Locale.default that you and others often don't know which default, you can decide by using locale.ENGLISH because I see your string date is format in English. If you are at other countries, the format will be different.
Here is my example code:
public static void main(String[] args) {
try {
SimpleDateFormat parserSDF = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.ENGLISH);
Date date = parserSDF.parse("Wed Oct 16 00:00:00 CEST 2013");
System.out.println("date: " + date.toString());
} catch (ParseException ex) {
ex.printStackTrace();
}
}
The result will be : date: Wed Oct 16 05:00:00 ICT 2013. Or you can decide which part of this date to be printed, by using its fields.
Hope this help :)
I think the original Exception is due to Z in your format.
Per documentation:
Z Time zone RFC 822 time zone -0800
most likely you meant to use lower case z

Categories

Resources