This question already has answers here:
java DateTimeFormatterBuilder fails on testtime [duplicate]
(2 answers)
Java - Unparseable date
(3 answers)
Closed 4 years ago.
Im having issue with formatting this string into ZonedDateTime object. I tried following oracle docs but I failed and I have no idea how it should be.
String date = "Sat Dec 01 20:56:28 CET 2018"
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss z uuuu");
ZonedDateTime dateTime = ZonedDateTime.parse(date, formatter);
I get
java.time.format.DateTimeParseException: Text 'Sat Dec 01 20:56:28 CET 2018' could not be parsed at index 0
Specify a Locale, to determine the human language used in translating name of month and such.
String date = "Sat Dec 01 20:56:28 CET 2018";
DateTimeFormatter formatter =
DateTimeFormatter.ofPattern(
"EEE MMM dd HH:mm:ss z uuuu" ,
Locale.US
)
;
ZonedDateTime dateTime = ZonedDateTime.parse(date, formatter);
System.out.println(dateTime);
Related
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)));
This question already has answers here:
java DateTimeFormatterBuilder fails on testtime [duplicate]
(2 answers)
Closed 2 years ago.
I have a pdf file where I want to know if the next line is a date, or just a string (there are two types of formats of the listing, and knowing if I've arrived at a date is important.) The trouble is, there appears to be no way to use date formatting to arrive at a date of 01 Apr 2020
LocalDate date = parseDate( "dd MMM yyyy", "01 Apr 2020" );
Throws ... Text '01 Apr 2020' could not be parsed at index 3
private static LocalDate parseDate( final String format, final String s ) {
final DateTimeFormatter df = DateTimeFormatter.ofPattern( format );
LocalDate ld; // Check if this was a legal LocalDate.
try {
ld = LocalDate.parse(s, df);
} catch (java.time.format.DateTimeParseException pe) {
System.out.println( pe.getMessage() );
ld = null; // This will signal an error
}
return ld;
}
Is there really no way to parse that format of date, like a bank uses in their pdf?
Replace
final DateTimeFormatter df = DateTimeFormatter.ofPattern( format );
With
final DateTimeFormatter df = DateTimeFormatter.ofPattern( format , Locale.US );
Hopefully, this will resolve your issue.
I believe you're using java8, You can do
LocalDate date = LocalDate.parse("01 Apr 2020", DateTimeFormatter.ofPattern("dd MMM yyyy", Locale.ROOT));
Edited: After pointed out that it doesn't work for all locales. Locale.ROOT should be used for neutral locale.
This question already has answers here:
Formatting LocalDate in Java: What's the pattern for "March 26 2020"
(2 answers)
Closed 1 year ago.
I'm trying to parse this string in the following way but I get an exception. Can anyone help me please?
String dateStr = "Thu 14 Feb 2019 15:05:48 +0200";
LocalDateTime datetime = LocalDateTime.parse(dateStr, DateTimeFormatter.ofPattern("EEE d MMM yyyy HH:mm:ss Z"));
Exception:
java.time.format.DateTimeParseException: Text 'Thu 14 Feb 2019 15:05:48 +0200' could not be parsed at index 0
String dateStr = "Thu 14 Feb 2019 15:05:48 +0200";
Locale bLocale = new Locale.Builder().setLanguage("en").setRegion("US").build();
LocalDateTime datetime = LocalDateTime.parse(dateStr, DateTimeFormatter.ofPattern("EEE d MMM yyyy HH:mm:ss Z", bLocale));
System.out.println(datetime);
You should create a locale as the parameter.
I'm not sure, but I think EEE only works if you specify locale. Anyway, it will work if you just ignore the day of the month.
LocalDateTime datetime = LocalDateTime.parse(
dateStr.substring(4), // skip "Thu "
DateTimeFormatter.ofPattern("d MMM yyyy HH:mm:ss Z"));
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));
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);