SimpleDate Format parsing error - java

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]

Related

Parse DateTime with DateTimeFormatter

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

SimpleDateFormat is not able to parse the date with offset value like - 'GMT-0500'

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

Java simpleDateFormat does not parse date correctly for given Locale?

Here in my code I get the current time and than format it according to "UTC" timezone and than parsed it using the same Timezone hence converting it to a Date. The problem is that I get the date as "Wed Jul 20 13:04:51 GMT+05:00 2016" than
its formatted and parse as in the code below
Calendar calendar = Calendar.getInstance();
Date currentDate = calendar.getTime();
//gives time as "Wed Jul 20 13:04:51 GMT+05:00 2016"
dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
String dateText = dateFormat.format(currentDate);
//This formats dates as following "Wed, 20 Jul 2016 08:04:51 +0000"
Now uptil here things work fine, the time with timezone GMT+05:00 is converted to standard UTC with GMT+00:00 that shown by +0000 in the formatted date
Date setteledDate = dateFormat.parse(dateText);
Now Parsing the date as above gives the following date.
"Wed Jul 20 13:04:51 GMT+05:00 2016". The problem is this date is again in GMT+5
and the whole purpose of me getting current date formatting it and than parsing it is lost because the purpose was to get current date according to "UTC" (GMT+00:00).
Please help.
Replace Z to z in SimpleDateFormat format.
Calendar calendar = Calendar.getInstance();
Date currentDate = calendar.getTime();
//gives time as "Wed Jul 20 13:04:51 GMT+05:00 2016"
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
String dateText = dateFormat.format(currentDate);
Log.e("date",dateText);
//output Wed, 20 Jul 2016 09:01:20 UTC
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
dateText = dateFormat.format(currentDate);
Log.e("date",dateText);
//output Wed, 20 Jul 2016 09:02:04 GMT+00:00
//update
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
try {
Date date = dateFormat.parse("Wed Jul 20 13:04:51 GMT+05:00 2016");
Log.e("date",date.toString());
} catch (ParseException e) {
e.printStackTrace();
}

Cannot parse a json string to date

I am trying to convert the following json string: "Mon Apr 04 00:00:00 CEST 2016" to a new date object by a simpleDateFormat. But i dont see why it wont work hope some one can help me.
String date = "Mon Apr 04 00:00:00 CEST 2016";
I get the following error:
(java.text.ParseException) java.text.ParseException: Unparseable
date: "Mon Apr 04 00:00:00 CEST 2016"
public Date parseDate(String date)
{
try
{
SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Date returnDate = formatter.parse(date);
return returnDate;
}
catch (ParseException e)
{
e.printStackTrace();
return null;
}
}
you need to parse with the locale:
SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
"EEE MMM ddHH:mm:ss z yyyy"
looks you forgot to put the space after dd:
"EEE MMM dd HH:mm:ss z yyyy"

Date parse with Timezone - Android

The input date is Tue Jun 09 07:00:00 SAST 2015. I want to parse this to a date so I used
SimpleDateFormat inputFormatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
Date inputDate = inputFormatter.parse("Tue Jun 09 07:00:00 SAST 2015");
This throws a ParseException at offset 20

Categories

Resources