I need to parse a date that I receive in a String with the following format: "Mon, 07 Nov 2022 21:00:00 +0100"
I have to dump the date to an object of type LocalDateTime and I use the following code:
String fecha = "Mon, 07 Nov 2022 21:00:00 +0100";
DateTimeFormatter formato = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss XXXX");
LocalDateTime fechaHora = LocalDateTime.parse(fecha, formato);
but I get a DateTimeParseException. I can't find the error. Can you help me?
Thank you
There is a pre-defined format for that: RFC_1123_DATE_TIME
String fecha = "Mon, 07 Nov 2022 21:00:00 +0100";
DateTimeFormatter formato = DateTimeFormatter.RFC_1123_DATE_TIME;
LocalDateTime fechaHora = LocalDateTime.parse(fecha, formato);
Your code is working fine in my local.
However if you want to convert a string to date you can also use SimpleDateFormat
For Ex:
String sDate1="Mon, 07 Nov 2022 21:00:00 +0100";
Date date1=new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ssZ").parse(sDate1);
System.out.println(date1);
Related
I am trying to parse this (and many similar) dateString - "Wed Aug 26 2020 11:03:30 GMT-0500"
Looking at the SimpleDateFormat documentation, I was assuming that a pattern like this should work:
String dateFormat = "EEE MMM d yyyy HH:mm:ss z";
However, it doesn't. But the following format is able to parse
String dateFormat = "EEE MMM d yyyy HH:mm:ss 'GMT'z";
But when I print the parsed date, I get the time with an hour added and offset reduced by an hour - Wed Aug 26 12:03:30 GMT-04:00 2020
What can I do to prevent this offset change?
Here is the sample code:
String dateStr = "Wed Aug 26 2020 11:03:30 GMT-0500";
String dateFormat = "EEE MMM d yyyy HH:mm:ss 'GMT'z";
Date date = new SimpleDateFormat(dateFormat).parse(dateStr);
System.out.println("Original Date String : "+dateStr);
System.out.println("Original Date Object : "+date);
Output:
Original Date String : Wed Aug 26 2020 11:03:30 GMT-0500
Original Date Object : Wed Aug 26 12:03:30 GMT-04:00 2020
Use java.time.OffsetDateTime here because there is no zone in that String, just an offset and the classes you are using are outdated for good reasons... Get rid of java.util.Date and java.text.SimpleDateFormat.
See this example:
public static void main(String[] args) {
// provide the String to be parsed
String dateStr = "Wed Aug 26 2020 11:03:30 GMT-0500";
// provide a matching pattern
String dateFormat = "EEE MMM d yyyy HH:mm:ss 'GMT'Z";
// create a formatter with this pattern and a suitable locale for unit names
DateTimeFormatter dtf = DateTimeFormatter.ofPattern(dateFormat, Locale.ENGLISH);
// parse the String to an OffsetDateTime using the formatter
OffsetDateTime odt = OffsetDateTime.parse(dateStr, dtf);
// print the result in the default format
System.out.println("Default/ISO format:\t" + odt);
// and print it in your custom format
System.out.println("Custom format:\t\t" + odt.format(dtf));
}
Output:
Default/ISO format: 2020-08-26T11:03:30-05:00
Custom format: Wed Aug 26 2020 11:03:30 GMT-0500
i'm have string dates like Wed Aug 17 17:22:51 IST 2016 to parse to ISOdate. so I have tried following code to do that.
String tdate = "Wed Aug 17 17:22:51 IST 2016";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Date pubDate = sdf.parse(tdate);
but it gave me:
java.text.ParseException: Unparseable date: "Wed Aug 17 17:22:51 IST 2016"
try parsing with
SimpleDateFormat sdf = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
hope it helped
I'm getting this error:
java.text.ParseException: Unparseable date: "Fri Apr 08 2016 00:00:00 GMT+0530 (IST)"
I have used this SimpleDateFormat can any one suggest me a correct one?
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss z (Z)");
If you are feeding date as "Fri Apr 08 2016 00:00:00 GMT+0530 (IST)". Then it's wrong. Please remove GMT. All time zones are calculated from GMT only.
Try passing date as "Fri Apr 08 2016 00:00:00 +0530 (IST)". It will work.
The correct parse-able date string should be:
Fri Apr 08 2016 00:00:00 IST (+0530)
This little snippet should clear the confusion. It's the reverse of what you're doing:
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss z (Z)");
String strDate = format.format(new Date());
System.out.println(strDate);
Output is: Fri Apr 08 2016 17:26:34 IST (+0530)
You can try the pattern EEE MMM dd yyyy HH:mm:ss 'GMT'Z (z)
1) Using Java 1.6 :
System.out.println(fromStringToDate("Fri Apr 08 2016 00:00:00 GMT+0530 (IST)", "EEE MMM dd yyyy HH:mm:ss 'GMT'Z (z)"));
Output (in my system) : Fri Apr 08 00:00:00 IST 2016
Refer this link for timezone values Java TimeZone List
public static Date fromStringToDate(String myPotentialDate,String pattern) throws Exception{
// DateFormat myDateFormat = new SimpleDateFormat(pattern);
String countryCode = "US";
String languageCode = "en";
String timeZone = "Asia/Kolkata";
DateFormat myDateFormat = getDateFormat(pattern,countryCode,languageCode,timeZone);
// We set the Leniant to false
myDateFormat.setLenient(false);
try {
return myDateFormat.parse(myPotentialDate);
}
catch (ParseException e) {
// Unparsable date
throw new Exception("Unparsable date '"+myPotentialDate+"' with pattern '"+pattern+"'. Due to '"+e+"'",e);
}
}
private static DateFormat getDateFormat(String pattern,String countryCode,String languageCode,String timeZoneId){
// We build the Local
Locale myLocale = new Locale(languageCode,countryCode);
// We build the DateFormat with the Local and the pattern
DateFormat myDateFormat = new SimpleDateFormat(pattern,myLocale);
// We set the TimeZone to the correct one
myDateFormat.setTimeZone(TimeZone.getTimeZone(timeZoneId));
// We set the Leniant to false
myDateFormat.setLenient(false);
return myDateFormat;
}
2)Using Java 1.8 Java8 Date time API
String countryCode = "US";
String languageCode = "en";
String timeZoneId = "Asia/Kolkata";
LocalDateTime dt = LocalDateTime.parse("Fri Apr 08 2016 00:00:00 GMT+0530 (IST)",
DateTimeFormatter.ofPattern("EEE MMM dd yyyy HH:mm:ss 'GMT'Z (z)").withLocale(new Locale(languageCode,countryCode)));
ZoneId zoneId= ZoneId.of(timeZoneId);
ZonedDateTime zdt= ZonedDateTime.of(dt, zoneId);
System.out.println(zdt);
Output:
2016-04-08T00:00+05:30[Asia/Kolkata]
This is the string that I have:
Sat, Nov 02, 2013 at 5:10 pm
I'm trying to parse it into a date time using this formatter:
DateFormat formatter = new SimpleDateFormat("EEE, MMM dd, YYYY 'at' K:mm a");
However, this is what it returns when I use it to parse the date string:
Sat Jan 05 17:10:00 CST 2013
I assume I'm getting the formatter wrong, but I can't figure out where.
Capital YYYY is the format for something called the "week year". You want the lowercase yyyy for the actual year.
DateFormat formatter = new SimpleDateFormat("EEE, MMM dd, yyyy 'at' K:mm a");
With this change I output the parsed date and get:
Sat Nov 02 17:10:00 PDT 2013
(I'm in the Pacific time zone.)
I want to convert : Thu Feb 02 00:00:00 WET 2012 to 02/02/2012 (with date type not string) using JAVA.
I did
String date = "Thu Feb 02 00:00:00 WET 2012";
SimpleDateFormat formatnow = new SimpleDateFormat("EEE MMM dd HH:mm:ss ZZZ yyyy", Locale.ENGLISH);
SimpleDateFormat formatneeded=new SimpleDateFormat("YYYY-MM-dd");
Date date1 = formatnow.parse(date);
String date2 = formatneeded.format(date1);
Date date3= formatneeded.parse(date2);
System.out.println(date3);
And I'm having : Thu Feb 02 00:00:00 WET 2012.
Can anyone tell me where is the problem ??
The Date object does not hold any information about the display format you want. So parsing a date from a formatted date string is not going to 'remember' any formatting.
System.out.println(date3) will print value of date3 using java's toString method.
You have the formatted date string in date2. So System.out.println(date2) should give you the right value.