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
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:
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:
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")));
This question already has answers here:
How to convert a date in this format (Tue Jul 13 00:00:00 CEST 2010) to a Java Date (The string comes from an alfresco property)
(5 answers)
Closed 7 years ago.
i have this string output 'Wed Apr 01 09:50:31 CEST 2015' can anyone tell me what pattern is that . i need it in order to use it in another function.
#Test
public void test() throws ParseException {
String input = "Wed Apr 01 09:50:31 CEST 2015";
String format = "DD MM hh:mm:ss YYYY"; // i have a wrong format
SimpleDateFormat df = new SimpleDateFormat(format);
Date date = df.parse(input);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int week = cal.get(Calendar.WEEK_OF_YEAR);
System.out.println(week);
}
any help to figure out the correct format of date pattern in my case.
Solution to your problem is to override default Date locale using
SimpleDateFormat(String pattern, Locale locale) constructor:
DateFormat dateFormat = new SimpleDateFormat(
"EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
dateFormat.parse("Tue Jul 13 00:00:00 CEST 2011");
System.out.println(dateFormat.format(new Date()));
Solution copy of How to convert a date in this format (Tue Jul 13 00:00:00 CEST 2010) to a Java Date (The string comes from an alfresco property)
I would like to extract date and year from the following string and convert it to a Data Object in Java.
Mon Jul 07 19:18:26 CEST 2014
How can I extract only date and year (in this case, 2014-07-07) from the text in a sophisticated way?
SimpleDateFormat s = new SimpleDateFormat("dd/MMM/yyyy");
String dateInString = "Mon Jul 07 19:18:26 CEST 2014";
SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date = s.parse(dateInString.split(" ")[2]+"/"+dateInString.split(" ")[1]+"/"+dateInString.split(" ")[5]);
System.out.println(new SimpleDateFormat("YYYY-MM-dd").format(date));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
This should work for you, I splitted your String, then put it to a date format and then formatted it the way you wanted it to be, assumed you wanted the months as the second parameter after the year, if thats not the case you can simply change the 'MM' to 'dd' and the 'dd' to 'MM'.
For Java 7 or below, use a SimpleDateFormat for parsing and formatting:
Locale dateLocale = Locale.US;
SimpleDateFormat inFormat = new SimpleDateFormat("E MMM dd HH:mm:ss z yyyy", dateLocale);
Date date = inFormat.parse("Mon Jul 07 19:18:26 CEST 2014");
SimpleDateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd", dateLocale);
System.out.println(outFormat.format(date));
Since Java 8, you can use DateTimeFormatter:
Locale dateLocale = Locale.US;
DateTimeFormatter inFormatter = DateTimeFormatter.ofPattern("E MMM dd HH:mm:ss z yyyy", dateLocale);
TemporalAccessor date = inFormatter.parse("Mon Jul 07 19:18:26 CEST 2014");
DateTimeFormatter outFormatter = DateTimeFormatter.ISO_LOCAL_DATE;
System.out.println(outFormatter.format(date));