Parse a Date String to another format [duplicate] - java

This question already has answers here:
Java Date Time conversion to given timezone
(3 answers)
Java - Unparseable date
(3 answers)
Getting error java.text.ParseException: Unparseable date: (at offset 0) even if the Simple date format and string value are identical
(4 answers)
Closed 3 years ago.
I have a problem. I've tried to parse a String which contains a date into another DateFormat.
Code:
String datumString = "Mon, 27 Jan 2020 21:31:16 +0100";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
Date zeitstempel = null;
try {
zeitstempel = simpleDateFormat.parse(datumString);
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println(zeitstempel);
Error message:
java.text.ParseException: Unparseable date: "Mon, 27 Jan 2020 21:31:16 +0100"
at java.text.DateFormat.parse(DateFormat.java:366)
at de.puki.geopolitan.Main.<init>(Main.java:32)
at de.puki.geopolitan.Main.main(Main.java:73)
Please help. :)

The variable names show that you are German, most likely working on a system with German language and date formatting. By default this also affects SimpleDateFormat output and parsing (unless you specify the Locale to be used)!
Between the German and the English/US formatted date string there is a small but significant difference:
Locale.ENGLISH: "Mon, 27 Jan 2020 21:31:16 +0100";
Locale.GERMAN: "Mo., 27 Jan 2020 21:31:16 +0100";
The date string you parse however uses the English date format, therefore you have to explicitly set Locale.US or Locale.ENGLISHwhen constructing the SimpleDateFormat:
simpleDateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
Afterwards you can parse the sample date string in your question.

Related

java.text.ParseException: Unparseable date. "dd MMM yyyy HH:mm:ss" format [duplicate]

This question already has answers here:
Getting error java.text.ParseException: Unparseable date: (at offset 0) even if the Simple date format and string value are identical
(4 answers)
Closed 1 year ago.
I am getting a parsing error with the following code
String time = "24 Apr 2021 11:56:44";
Date timeOnLine = new SimpleDateFormat("dd MMM yyyy HH:mm:ss").parse(time);
Exception:
java.text.ParseException: Unparseable date: "24 Apr 2021 11:56:44"
I am not sure what the problem is since the pattern seems to correspond with the string correctly.
Any advice on how to solve would be greatly appreciated!
Try with:
String time = "24 Apr 2021 11:56:44";
Date timeOnLine = new SimpleDateFormat("dd MMM yyyy HH:mm:ss", Locale.US).parse(time);

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

Simple date format reading month incorrectly [duplicate]

This question already has answers here:
How to convert a String to a Date using SimpleDateFormat?
(10 answers)
Closed 7 years ago.
I am working with converting Date Strings to Epoch times then back again, but it seems like when I convert to Epoch the month is ignored when I parse the date String
String timeString = "15 Aug 2005 13:07:58.035";
Date dateTime = null;
SimpleDateFormat sdf = new SimpleDateFormat("DD MMM yyyy hh:mm:ss", Locale.US);
try {
dateTime = sdf.parse(timeString);
} catch (ParseException e) {
e.printStackTrace();
}
long convert = dateTime.getTime();
System.out.println(str);
System.out.println(convert);
Date epochToString = new Date(convert);
System.out.println(epochToString.toString());
Here is the output
15 Aug 2005 13:07:58.035
1105812478000
Sat Jan 15 13:07:58 EST 2005
When I calculated the epoch time by hand the date was indeed Jan 15 2005, so for some reason the MMM value is being ignored in my format.
Edit:
I should also note that I changed the format token to MM and tried a numerical representation in the string instead, but had the same exact results.
This is because DD means day in year. You want dd for day in month. See SimpleDateFormat Javadoc
I suggest you use HH for 24 hour time.

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

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

"Unparseable date" when trying to convert "16 Nov 2011 08:00" string to Date? [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?
How can I convert "16 Nov 2011 08:00" to a Date object? I tried SimpleDateFormat but does not works, I get
java.text.ParseException: Unparseable date: "16 Nov 2011 08:00"
or will I have to split the string enter slashes and then give it a try ?
You problem with the SimpleDateFormat was probably an incorrect format string.
Here is the correct way:
DateFormat formatter = new SimpleDateFormat( "dd MMM yyyy HH:mm" );
Date date = formatter.parse( "16 Nov 2011 08:00" );
You will need to use SimpleDateFormat to set a date format then use .parse(String s) to convert your String to a Date, here's an example:
DateFormat format = new SimpleDateFormat("dd MMM yyyy HH:mm");
String theDateString = "16 Nov 2011 08:00";
Date myDate = format.parse(theDateString);

Categories