Convert from String to Date throws Unparseable date exception [duplicate] - java

This question already has answers here:
Java - Unparseable date
(3 answers)
Closed 9 years ago.
I want to convert a Date to a String but I have some problems. My code is this:
SimpleDateFormat formato = new SimpleDateFormat(
"EEE MMM dd HH:mm:ss z yyyy");
String hacer = "Fri Nov 01 10:30:02 PDT 2013";
Date test = null;
test = formato.parse( hacer);
System.out.println("prueba===>" + test);
But nothing something is wrong eclipse shows me this error:
Unparseable date: "Fri Nov 01 10:30:02 PDT 2013"
at java.text.DateFormat.parse(Unknown Source)
some help?

Probably your default locale doesn't support English months in MMM. For example in Poland MMM supports "styczeń" but not "Jan" or "January"
To change this In SimpleDateFormat you need to set locale which supports months written in English, for example
new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);

Related

How to convert time string to Date in Java? ParseException [duplicate]

This question already has answers here:
Java unparsable date SimpleDateFormat [duplicate]
(4 answers)
Closed 2 years ago.
The time string is like Tue Feb 09 10:23:31 CST 2021, how to convert to date object?
This is my code :
SimpleDateFormat dateFormat = new SimpleDateFormat("E M d hh:mm:ss z yyyy");
Date parsedDate = dateFormat.parse("Tue Feb 09 10:23:31 CST 2021");
System.out.println("===>"+parsedDate.getTime());
It throw the below exception:
java.text.ParseException: Unparseable date: "Tue Feb 09 10:23:31 CST 2021"
at java.text.DateFormat.parse(DateFormat.java:366)
Oh I find the root cause, the SimpleDateFormat is wrong.It should be "EEE MMM dd hh:mm:ss zzz yyyy"
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd hh:mm:ss zzz yyyy");
Date parsedDate = dateFormat.parse("Tue Feb 09 10:23:31 CST 2021");
System.out.println("===>"+parsedDate.getTime());
It work now.

How to convert a String AM/PM date to Timestamp in Java? [duplicate]

This question already has answers here:
Java - Unparseable date
(3 answers)
Closed 3 years ago.
I want to parse a date that includes AM/PM format from String to Timestamp.
Although when I try to parse this date:
Dec 31 2017 12:00AM
I get a
Exception in thread "main" java.text.ParseException: Unparseable date: "Dec 31 2017 12:00AM"
at java.text.DateFormat.parse(Unknown Source)
at test.DatteTest.main(DatteTest.java:16)
My code:
String endDate = "Dec 31 2017 12:00AM";
SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd yyyy hh:mmaa");
Date parsedDate = dateFormat.parse(endDate);
Timestamp timestamp = new Timestamp(parsedDate.getTime());
System.out.println(endDate);
System.out.println(timestamp);
Am I doing something wrong in the simpledateformat?
"MMM dd yyyy hh:mmaa"?
The input date contains a word in english. You have to give the locale :
SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd yyyy hh:mmaa", Locale.ENGLISH);

DateTimeParseException: Text could not be parsed at index 2 [duplicate]

This question already has answers here:
java DateTimeFormatterBuilder fails on testtime [duplicate]
(2 answers)
Java - Unparseable date
(3 answers)
Closed 4 years ago.
I am really confused now why the following snippet results in DateTimeParseException.
public static void main(String[] args) {
java.time.format.DateTimeFormatter dtf = java.time.format.DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss zzz");
String date = "Mon, 10 Sep 2018 23:57:09 UTC";
System.out.println(dtf.parse(date));
}
The following exception is thrown:
Exception in thread "main" java.time.format.DateTimeParseException: Text 'Mon, 10 Sep 2018 23:57:09 UTC' could not be parsed at index 2
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1777)
at com.sample.binding.bitronvideo.driver.BitronVideoRecordingDriver.main(BitronVideoRecordingDriver.java:448)
I would really appreciate further help.
Thanks,
Amit
I didn't get the exception. So Checking your profile I saw that your locale is in Germany so i tried this and got the exception.
java.time.format.DateTimeFormatter dtf =
java.time.format.DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss zzz",
Locale.GERMANY);
String date = "Mon, 10 Sep 2018 23:57:09 UTC";
System.out.println(dtf.parse(date));
And the shordays for German are :
Short weekdays So, Mo, Di, Mi, Do, Fr, Sa
Try with this code and I bet it will work
java.time.format.DateTimeFormatter dtf =
java.time.format.DateTimeFormatter.ofPattern("EE, dd MMM yyyy HH:mm:ss zzz");
String date = "Mo, 10 Sep 2018 23:57:09 UTC";
System.out.println(dtf.parse(date));
But for your String Date to work just use UK or US Locale by passing an argument
java.time.format.DateTimeFormatter dtf =
java.time.format.DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss zzz",
Locale.UK);
String date = "Mon, 10 Sep 2018 23:57:09 UTC";
System.out.println(dtf.parse(date));

Formatting a string to dd/MM/yyyy [duplicate]

This question already has answers here:
java.util.Date format conversion yyyy-mm-dd to mm-dd-yyyy
(8 answers)
Closed 7 years ago.
I have the string Thu Nov 12 00:00:00 GMT 2015 which is EEE MMM dd HH:mm:ss ZZZ yyyy.
I want to convert this string into a string with the format of dd/MM/yyyy, or in this case 12/11/2015.
SimpleDateFormat sFrom = new SimpleDateFormat("EEE MMM dd HH:mm:ss ZZZ yyyy");
SimpleDateFormat sTo = new SimpleDateFormat("dd/MM/yyyy");
String result = sTo.format(sFrom.parse("Thu Nov 12 00:00:00 GMT 2015"));

Java SimpleDateFormat parsing mystery [duplicate]

This question already has answers here:
Unparseable date
(4 answers)
Closed 7 years ago.
I'm unable to correctly parse a date.....
Here is the code :
String UpdateDateFormat = "Fri Mar 06 00:00:00 CET 2015";
SimpleDateFormat sdf2 = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy");
try{
Date UpdateDateFormated = sdf2.parse(UpdateDateFormat);
}
catch(ParseException ex){
System.out.println(ex.getMessage());
}
I get an exception, "Unparseable date..."
I saw this post : How to convert "Mon Jun 18 00:00:00 IST 2012" to 18/06/2012? but it's not working for me.
Any idea ?
Thanks for your help
SimpleDateFormat sdf2 = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);
Just needed to add the Locale.US.

Categories