This question already has answers here:
Java string to date conversion
(17 answers)
Closed 7 years ago.
How do I parse the date string below into a Date object?
String target = "Thu Sep 28 20:29:30 JST 2000";
DateFormat df = new SimpleDateFormat("E MM dd kk:mm:ss z yyyy");
Date result = df.parse(target);
Throws exception...
java.text.ParseException: Unparseable date: "Thu Sep 28 20:29:30 JST 2000"
at java.text.DateFormat.parse(DateFormat.java:337)
The pattern is wrong. You have a 3-letter day abbreviation, so it must be EEE. You have a 3-letter month abbreviation, so it must be MMM. As those day and month abbreviations are locale sensitive, you'd like to explicitly specify the SimpleDateFormat locale to English as well, otherwise it will use the platform default locale which may not be English per se.
public static void main(String[] args) throws Exception {
String target = "Thu Sep 28 20:29:30 JST 2000";
DateFormat df = new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy", Locale.ENGLISH);
Date result = df.parse(target);
System.out.println(result);
}
This prints here
Thu Sep 28 07:29:30 BOT 2000
which is correct as per my timezone.
I would also reconsider if you wouldn't rather like to use HH instead of kk. Read the javadoc for details about valid patterns.
Here is a working example:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.util.Date;
public class j4496359 {
public static void main(String[] args) {
try {
String target = "Thu Sep 28 20:29:30 JST 2000";
DateFormat df = new SimpleDateFormat("EEE MMM dd kk:mm:ss zzz yyyy");
Date result = df.parse(target);
System.out.println(result);
} catch (ParseException pe) {
pe.printStackTrace();
}
}
}
Will print:
Thu Sep 28 13:29:30 CEST 2000
String target = "27-09-1991 20:29:30";
DateFormat df = new SimpleDateFormat("dd MM yyyy HH:mm:ss");
Date result = df.parse(target);
System.out.println(result);
This works fine?
new SimpleDateFormat("EEE MMM dd kk:mm:ss ZZZ yyyy");
and
new SimpleDateFormat("EEE MMM dd kk:mm:ss Z yyyy");
still runs. However, if your code throws an exception it is because your tool or jdk or any other reason. Because I got same error in my IDE but please check these http://ideone.com/Y2cRr (online ide) with ZZZ and with Z
output is : Thu Sep 28 11:29:30 GMT 2000
I had this issue, and I set the Locale to US, then it work.
static DateFormat visitTimeFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy",Locale.US);
for String "Sun Jul 08 00:06:30 UTC 2012"
A parse exception is a checked exception, so you must catch it with a try-catch when working with parsing Strings to Dates, as #miku suggested...
Related
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
I am trying to convert the following json string: "Mon Apr 04 00:00:00 CEST 2016" to a new date object by a simpleDateFormat. But i dont see why it wont work hope some one can help me.
String date = "Mon Apr 04 00:00:00 CEST 2016";
I get the following error:
(java.text.ParseException) java.text.ParseException: Unparseable
date: "Mon Apr 04 00:00:00 CEST 2016"
public Date parseDate(String date)
{
try
{
SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Date returnDate = formatter.parse(date);
return returnDate;
}
catch (ParseException e)
{
e.printStackTrace();
return null;
}
}
you need to parse with the locale:
SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
"EEE MMM ddHH:mm:ss z yyyy"
looks you forgot to put the space after dd:
"EEE MMM dd HH:mm:ss z yyyy"
Can anyone point out what seems to be the problem here?
try {
Date date = new SimpleDateFormat("Mon, 02 Nov 2015 15:13:00 EET").parse("EEE, dd MMM yyyy hh:mm:ss z");
} catch (ParseException e) {
e.printStackTrace();
}
and the stacktrace:
java.text.ParseException: Unparseable date: "Mon, 02 Nov 2015 15:13:00 EET" (at offset 26)
I'm suspecting something with the locale that I'm using but I can't be sure. Seems that "z" for timezone not working.
Edit:
Sorry the exception was funny earlier, I changed it but forgot to update here.
try {
Date date = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss z", Locale.US).parse("Mon, 02 Nov 2015 15:13:00 EET");
} catch (ParseException e) {
e.printStackTrace();
}
After looking at javadoc for SimpleDateFormat, you are using "hh" for hour, which is assumed to be a 12-hour time. Use HH for 24-hour time. Your example as 15 for the hour.
I think you're missing 'z' here.
Try:
Date date = new SimpleDateFormat("Mon, 02 Nov 2015 15:13:00 EET").
parse("EEE, dd MMM yyyy hh:mm:ss zzz")
Since your timezone is with three characters.
Such an exception can come from parsing a date with the wrong Locale. For example this date formatter :
SimpleDateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss z", Locale.US);
will successfully parse the example date :
Date date = df.parse("Mon, 02 Nov 2015 15:13:00 EET");
But the following will give the exception you are getting
SimpleDateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss z", Locale.FRENCH);
I would expect the Locale in Android to be chosen according to the language set by the user.
Consider the snippet:
String dateStr = "Mon Jan 32 00:00:00 IST 2015"; // 32 Jan 2015
DateFormat formatter = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
DateFormat ddMMyyyy = new SimpleDateFormat("dd.MM.yyyy");
System.out.println(ddMMyyyy.format(formatter.parse(dateStr)));
gives me the output as
01.02.2015 // Ist February 2015
I wish to prevent this to make the user aware on the UI that is an invalid date?
Any suggestions?
The option setLenient() of your SimpleDateFormat is what you are looking for.
After you set isLenient to false, it will only accept correctly formatted dates anymore, and throw a ParseException in other cases.
String dateStr = "Mon Jan 32 00:00:00 IST 2015"; // 32 Jan 2015
DateFormat formatter = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
formatter.setLenient(false);
DateFormat ddMMyyyy = new SimpleDateFormat("dd.MM.yyyy");
try {
System.out.println(ddMMyyyy.format(formatter.parse(dateStr)));
} catch (ParseException e) {
// Your date is invalid
}
You can use DateFormat.setLenient(boolean) to (from the Javadoc) with strict parsing, inputs must match this object's format.
DateFormat ddMMyyyy = new SimpleDateFormat("dd.MM.yyyy");
ddMMyyyy.setLenient(false);
Set the date formatter not to be lenient...
DateFormat formatter = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
formatter.setLenient(false);
I have date strings in this form Thu Aug 02 00:00:00 GMT+00:00 2012
I have tried to use this method to parse these String in a Date object
public Date fromStringToDate(String data) {
Date result;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy MM dd HH:mm:ss");
try {
result = sdf.parse(data);
return result;
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
But doesn't works and I get this error
java.text.ParseException: Unparseable date: "Thu Aug 02 00:00:00 GMT+00:00 2012"
I suppose that the problem is caused by a wrong SimpleDateFormat, but I don't know the right syntax to fix it.
You need to adjust the date format to the given string:
EEE MMM dd HH:mm:ss Z yyyy
Make sure use the correct placeholders, case sensitive, etc. Take a look to the Date and Time Patterns.
Sorry, I had a mistake with the 'z' pattern, 'Z' is:
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.US);
Take a look to Locale.US, it is important to apply because the months and and days are in english.
Use this date formatting:
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy")