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));
Related
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.util.Date format conversion yyyy-mm-dd to mm-dd-yyyy
(8 answers)
Closed 7 years ago.
I have the string Thu Nov 12 00:00:00 GMT 2015 which is EEE MMM dd HH:mm:ss ZZZ yyyy.
I want to convert this string into a string with the format of dd/MM/yyyy, or in this case 12/11/2015.
SimpleDateFormat sFrom = new SimpleDateFormat("EEE MMM dd HH:mm:ss ZZZ yyyy");
SimpleDateFormat sTo = new SimpleDateFormat("dd/MM/yyyy");
String result = sTo.format(sFrom.parse("Thu Nov 12 00:00:00 GMT 2015"));
This question already has answers here:
Unparseable date
(4 answers)
Closed 7 years ago.
I'm unable to correctly parse a date.....
Here is the code :
String UpdateDateFormat = "Fri Mar 06 00:00:00 CET 2015";
SimpleDateFormat sdf2 = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy");
try{
Date UpdateDateFormated = sdf2.parse(UpdateDateFormat);
}
catch(ParseException ex){
System.out.println(ex.getMessage());
}
I get an exception, "Unparseable date..."
I saw this post : How to convert "Mon Jun 18 00:00:00 IST 2012" to 18/06/2012? but it's not working for me.
Any idea ?
Thanks for your help
SimpleDateFormat sdf2 = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);
Just needed to add the Locale.US.
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);
This question already has answers here:
Java string to date conversion
(17 answers)
Closed 9 years ago.
I am have this String from a Date object: Mon Mar 25 00:00:00 IST 2013
What is the String representation of the date format?
The code below should work fine.
public static void main(String[] args) throws Exception {
String target = "Mon Mar 25 00:00:00 IST 2013";
DateFormat df = new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy", Locale.ENGLISH);
Date result = df.parse(target);
System.out.println(result);
}
Read the javadoc for the valid patterns.
String yourDate= "Mon Mar 25 00:00:00 IST 2013" ;
DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Date jDate = new Date(df.parse(yourDate).getTime());
System.out.println(jDate);