Unparseable Date:"Sat Oct 12 09:05:00 IST 2013" exception - java

I need to parse a string to date. But getting an unparseable exception.
Following is my code:
String str="Sat Oct 12 09:05:00 IST 2013";
SimpleDateFormat format = new SimpleDateFormat("EEE MM DD hh:mm:ss yyyy");
try {
format.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}

Your format has several issues:
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);
D denotes day in year, not day in month
You're missing the time zone
The format for month is incorrect
Since you're time is in 24-hour format, you need H instead of h
Refer to SimpleDateFormat documentation for information on Date and Time Patterns.

Your format for parsing that string is wrong. You need to use this format.
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy"); // Default Locale
MMM - is the proper month format in your case
z - you missed the pattern for timezone
d - represents the day in the month not D which represents day in the year.
And as suggested, you might want to use H instead of h as the hour seems to be in the 24-hour format.
Have a look at the docs to learn more about the Date and Time Patterns.

Related

Trouble formatting date in Java [duplicate]

This question already has answers here:
DateTimeParse Exception
(2 answers)
Closed 2 years ago.
I've tried several methods with Java Joda Time, Date Time with locale and commons-lang and can't get this date formatted.
Input
Mon Dec 28 15:18:16 UTC 2020
Output
Desired output format yyyy-MM-dd HH:mm:ss.SSS
When I use a format pattern like EEE MMM dd HH:mm:ss Z YYYY the date is off my a couple days and the timezone seems completely wrong.
Formatter:
private static final DateTimeFormatter DATE_TIME_FORMATTER =
DateTimeFormatter
.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")
.withLocale(Locale.US)
.withZone(ZoneId.systemDefault());
DateUtils.parseDate (Optional
.ofNullable(record)
.map(CustomerModel::getCustomerAudit)
.map(customerAudit::getCreated)
.map(auditItem::getDate).get ().toString (), "EEE MMM dd HH:mm:ss YYYY")
When debugging parsing issues, if possible, reverse the operation and generate the text you're supposed to be parsing, to verify the parsing rules, i.e. the date format string. This applies to date parsing, JAXB parsing, and any other (de)serializing operation that is bi-directional. It makes finding conversion rule issues a lot easier.
So, let us check the format string in the question, with the shown date value:
ZonedDateTime dateTime = ZonedDateTime.of(2020, 12, 28, 15, 18, 16, 0, ZoneOffset.UTC);
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss Z YYYY", Locale.US);
System.out.println(dateTime.format(fmt));
Output
Mon Dec 28 15:18:16 +0000 2021
Oops! That doesn't fit the expected output, aka the input we desire to parse:
Mon Dec 28 15:18:16 UTC 2020
So what went wrong?
The year is wrong because it's supposed to be uuuu (year), not YYYY (week-based-year).
The time zone is wrong because Z does support a text representation. Use VV or z instead.
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss z uuuu", Locale.US);
ZonedDateTime dateTime = ZonedDateTime.parse("Mon Dec 28 15:18:16 UTC 2020", fmt);
System.out.println(dateTime);
System.out.println(dateTime.format(DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSS")));
Output
2020-12-28T15:18:16Z[UTC]
2020-12-28 15:18:16.000
As you can see, it now parsed correctly.
The code in the question makes little sense:
It is formatting a Date value to text using toString(), just to attempt parsing that back.
It is using Optional for simple null-handling (which is discouraged), but then unconditionally calling get(), which means a null value will throw exception anyway.
The code should be:
record.getCustomerAudit().getCreated().getDate().toInstant()
This of course makes the entire question moot.
Works fine for me.
String s = "Mon Dec 28 15:18:16 UTC 2020";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss VV yyyy",
Locale.ENGLISH);
ZonedDateTime zdt = ZonedDateTime.parse(s, formatter);
formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS", Locale.ENGLISH);
System.out.println(zdt.format(formatter));
Output is
2020-12-28 15:18:16.000
Am I missing something?
Have you tried with SimpleDateFormat?
String dateString = "Mon Dec 28 15:18:16 UTC 2020";
SimpleDateFormat input = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.ENGLISH);
System.out.println(output.format(input.parse(dateString)));
With timezone:
String dateString = "Mon Dec 28 15:18:16 UTC 2020";
SimpleDateFormat input = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd z HH:mm:ss.SSS");
input.setTimeZone(TimeZone.getTimeZone("UTC"));
output.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println(output.format(input.parse(dateString)));

Talend - Transform "EEE MMM dd hh:mm:ss z yyyy" in "yyyy-MM-dd"

I receive a date from a file in this format: "EEE MMM dd hh:mm:ss z yyyy" and I'm trying to convert this value into Date "yyyy-MM-dd". For that I'm using:
TalendDate.parseDate("yyyy/MM/dd", TalendDate.formatDate("yyyy/MM/dd", TalendDate.parseDate("EEE MMM dd hh:mm:ss z yyyy",context.date)))
The context.date is defined here:
context.date = input_row.mtime_string;
But when I run my JavaRow component I get the following error:
Exception in component tJavaRow_1
"java.lang.RuntimeException: java.text.ParseException: Unparseable date: "Thu Aug 09 10:38:45 BST 2018"
How can I solve this?
Many Thanks!
You could achieve the format using the below code snippet -
System.out.println(input_row.newColumn);
SimpleDateFormat parserSDF = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy", Locale.ENGLISH);
Date date = parserSDF.parse(input_row.newColumn);
String dDate = null;
parserSDF = new SimpleDateFormat("yyyy-MM-dd");
dDate = parserSDF.format(date);
System.out.println(dDate);
Also, you need the below libraries to be imported(Advanced settings section of tJavaRow)-
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.Date;
I had directly passed the input value from file(in my scenario tFileInputDelimited) into tJavaRow as - input_row.newColumn and then used SimpleDateFormat class to both parse and format dates according to the formatting pattern.
Read more here.
If you want to convert the date into LocalDate then below code might help:
private LocalDate getLocalDate(String date){
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM dd hh:mm:ss z yyyy", Locale.getDefault());
return LocalDate.parse(date, formatter);
}
And once you get the LocalDate, you can transform it to any format. As the question expects yyyy-MM-dd then just call toString() on LocalDate object.
LocalDate curr = LocalDate.now();
System.out.println(curr.toString());
It will display the date like "2019-11-20".
Hope this helps to someone.

is SimpleDateFormat different in Java & Android?

I use below code in Java and works perfect!
SimpleDateFormat format = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z");
Date date = format.parse("Sun, 11 May 2014 23:11:51 +0430");
but in Android I got exception !
java.text.ParseException: Unparseable date: "Sun, 11 May 2014 23:11:51 +0430" (at offset 0)
what's wrong ?!
The problem is that the code will execute correctly if the default locale is english, otherwise will throw an exception. You can solve it adding the correct Locale.
//Locale locale = new Locale("en-US");
Locale locale = Locale.US;
SimpleDateFormat format = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z", locale);
Date date = format.parse("Sun, 11 May 2014 23:11:51 +0430");
Probably the android device has a different language setting. Consider using a constant Locale as RC stated in the comment, in that case you wouldn't need the extra variable, use the constant directly in the constructor.
SimpleDateFormat format = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z", Locale.US);
Date date = format.parse("Sun, 11 May 2014 23:11:51 +0430");
If the Locale on your device is German for example your code executes if you parse this date:
SimpleDateFormat format = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z", Locale.GERMAN);
Date date = format.parse("So, 11 Mai 2014 23:11:51 +0430");

SimpleDateFormat and parseException

I am developing a Web application into GWT and I am using the Object DatePicker. This object retrieves the date in a defined format which I am translating into a String such as:
Wed May 14 2014 00:00
For me it is useful to use this date as String for some operations. However, for one of them I need the Timestamp object. For that reason, I am making use of the SimpleDateFormat object in the following way:
SimpleDateFormat sdf = new SimpleDateFormat("E MMM dd yyyy HH:mm");
Timestamp tDateIni = new Timestamp(sdf.parse(sDateIni).getTime());
Yet, when I run the remote debug I get a ParseException. Do you know what could the mistake be? I think I am using in a bad format the SimpleDateFormat object in the part "E MMM", but I am not sure. Thanks a lot in advance!
If you want to parse the date at client side in GWT then try with DateTimeFormat
DateTimeFormat dateTimeFormat=DateTimeFormat.getFormat("E MMM dd yyyy HH:mm");
Date date=dateTimeFormat.parse("Wed May 14 2014 00:00");
If you want to parse the date at server side then pass the time in milliseconds as long value instead of date string from client side and form the date back at server side using new Date(timeInMills)
Your date format uses the day of the week format that requires "EEE" instead of "E". This is causing the exception when the program is trying to read in your date string. It is expecting one letter for the day of the week.
http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Change this from
SimpleDateFormat sdf = new SimpleDateFormat("E MMM dd yyyy HH:mm");
to
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd yyyy HH:mm");
It should be EEE instead of E to represent Weekdays like Wed
Below code, perfectly works (TESTED)
public static void main(String[] args) {
String s = "Wed May 14 2014 00:00";
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd yyyy HH:mm");
try {
Timestamp tDateIni = new Timestamp(sdf.parse(s).getTime());
System.out.println(tDateIni.getTime());
} catch (ParseException ex) {
System.out.println("Parse Error");
}
}
I have added the Locale object in the SimpleDateFormat object and now it works. Thank you for all your help and your comments!!!

DATETIME convert

can anyone help me with converting DATETIME funtction in Java.
I retrieve the date time format from SMS headers in this format "Fri May 18 09:22:39 FJT 2012" .I want to convert it to this format "2012-05-18 09:51:42.39".Can anyone help.
Use a SimpleDateFormat for that purpose. Although I am not sure that the timezone "FJT" is known in Java. So you will maybe have to do some tricks for that.
As #Guillaume suggested, use SimpleDateFormat. Here's an example:
public String convert() throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
Date output = format.parse("Fri May 18 09:22:39 FJT 2012");
Calendar outputCal = Calendar.getInstance(format.getTimeZone());
outputCal.setTime(output);
return String.format("%04d-%02d-%02d %02d:%02d:%02d",outputCal.get(Calendar.YEAR), outputCal.get(Calendar.MONTH)+1, outputCal.get(Calendar.DAY_OF_MONTH), outputCal.get(Calendar.HOUR_OF_DAY), outputCal.get(Calendar.MINUTE), outputCal.get(Calendar.SECOND));
}
Everything is hardcoded - you have to add parameters as necessary
import java.text.SimpleDateFormat;
...
String d = "Fri May 18 09:22:39 FJT 2012";
SimpleDateFormat inputFormat = new SimpleDateFormat("EEE MMM d HH:mm:ss ZZZ yyyy");
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SS");
outputFormat.setTimeZone(TimeZone.getTimeZone("GMT")); // set yr time zone for output
Date date = inputFormat.parse(d);
System.out.println(outputFormat.format(date));

Categories