String could not be parsed at index 0 [duplicate] - java

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

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

Format string to java8 ZonedDateTime [duplicate]

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

I can't convert this String to Date [duplicate]

This question already has answers here:
Java - Unparseable date
(3 answers)
Closed 4 years ago.
I want convert "Thu Jan 18 00:00:00 CET 2018" to "2018-01-18" -> yyyy-mm-dd.
But I get error -> java.text.ParseException: Unparseable date: "Thu Jan 18 00:00:00 CET 2018".
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:
stringValue = String.valueOf(cell.getDateCellValue());
DateFormat formatter = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
Date date = (Date)formatter.parse(stringValue);
System.out.println(date);
break;
case Cell.CELL_TYPE_STRING:...
break;
}
Maybe is why I have cell.getCellType() and Cell.CELL_TYPE_NUMERIC deprecated ?
Seems your Locale is not in this format, try Locale.US, and you need use x for timezone, try:
String input = "Thu Jan 18 00:00:00 CET 2018";
DateFormat parser = new SimpleDateFormat("E MMM dd HH:mm:ss x yyyy", Locale.US);
Date date = parser.parse(input); // parse String to Date
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(formatter.format(date)); // format Date to String
Or you can use ZonedDateTime since java8:
String input = "Thu Jan 18 00:00:00 CET 2018";
ZonedDateTime zonedDateTime = ZonedDateTime.parse(input,
DateTimeFormatter.ofPattern("E MMM dd HH:mm:ss z yyyy", Locale.US));
System.out.println(DateTimeFormatter.ISO_DATE.format(zonedDateTime.toLocalDate()));
If you don't specify a locale, then JVM will use the default locale of the system. If that also happens to be US English (or some other language where the abbreviation for January is "Jan"), then the results will be the same.
Please take a look at this thread : https://coderanch.com/t/491599/java/SimpleDateFormat-locale

Converting date object to yyyy-MM-dd hh:mm:ss [duplicate]

This question already has answers here:
want current date and time in "dd/MM/yyyy HH:mm:ss.SS" format
(11 answers)
SimpleDateFormatter.parse giving output in different format than specified
(1 answer)
Closed 4 years ago.
I am trying to convert this date object Fri Sep 21 08:00:00 SGT 2018 to this format yyyy-MM-dd hh:mm:ss date object.
Date dt = sd1.parse(startTime);
logger.info(dt);
logger.info(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(dt.toString()));
The first log statement works, it returns me Fri Sep 21 08:00:00 SGT 2018 but the second log statement does not work.
It throws me an error
unparseable date Fri Sep 21 08:00:00 SGT 2018
What am i doing wrong here? My end goal is to get the date object in yyyy-MM-dd hh:mm:ss format.
First you have to parse the date properly with :
new SimpleDateFormat("EEE MMM dd hh:mm:ss zzz yyyy").parse("Fri Sep 21 08:00:00 SGT 2018");
So this should work:
Date newDate = new SimpleDateFormat("EEE MMM dd hh:mm:ss zzz yyyy").parse("Fri Sep 21 08:00:00 SGT 2018");
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss.SS");
String strDate = sdf.format(newDate.getTime());
System.out.println(strDate);
But I suggest you to go with LocalDateTime
LocalDateTime ldt = LocalDateTime.parse("Fri Sep 21 08:00:00 SGT 2018", DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy"));
System.out.println(ldt.format(DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss.SS")));

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

Categories