Cannot parse a json string to date - java

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"

Related

SimpleDate Format parsing error

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]

Java simple date formatter for string to date

Can anyone point out what seems to be the problem here?
try {
Date date = new SimpleDateFormat("Mon, 02 Nov 2015 15:13:00 EET").parse("EEE, dd MMM yyyy hh:mm:ss z");
} catch (ParseException e) {
e.printStackTrace();
}
and the stacktrace:
java.text.ParseException: Unparseable date: "Mon, 02 Nov 2015 15:13:00 EET" (at offset 26)
I'm suspecting something with the locale that I'm using but I can't be sure. Seems that "z" for timezone not working.
Edit:
Sorry the exception was funny earlier, I changed it but forgot to update here.
try {
Date date = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss z", Locale.US).parse("Mon, 02 Nov 2015 15:13:00 EET");
} catch (ParseException e) {
e.printStackTrace();
}
After looking at javadoc for SimpleDateFormat, you are using "hh" for hour, which is assumed to be a 12-hour time. Use HH for 24-hour time. Your example as 15 for the hour.
I think you're missing 'z' here.
Try:
Date date = new SimpleDateFormat("Mon, 02 Nov 2015 15:13:00 EET").
parse("EEE, dd MMM yyyy hh:mm:ss zzz")
Since your timezone is with three characters.
Such an exception can come from parsing a date with the wrong Locale. For example this date formatter :
SimpleDateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss z", Locale.US);
will successfully parse the example date :
Date date = df.parse("Mon, 02 Nov 2015 15:13:00 EET");
But the following will give the exception you are getting
SimpleDateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss z", Locale.FRENCH);
I would expect the Locale in Android to be chosen according to the language set by the user.

How to parse the string "Mon Oct 22 03:00:26 +0000 2012" [duplicate]

This question already has answers here:
How to parse a date? [duplicate]
(5 answers)
Closed 9 years ago.
How to parse the following string to date
Mon Oct 22 03:00:26 +0000 2012
I tried MMM dd HH:mm:ss yyyy, but it is not working. I know I am missing something but couldn't find out that.
String b="Mon Oct 22 03:00:26 +0000 2012";
DateFormat a = new SimpleDateFormat("MMM dd HH:mm:ss yyyy");
Date d=(Date)a.parse(b)
I'd recommend EEE MMM dd HH:mm:ss Z yyyy instead of HH:mm:ss yyyy.
Edit:
Specifically, your code would be:
String b="Mon Oct 22 03:00:26 +0000 2012";
DateFormat a = new SimpleDateFormat("MMM dd HH:mm:ss Z yyyy");
Date d=(Date)a.parse(b)
Edit after comment:
String b="Mon Oct 22 03:00:26 +0000 2012";
DateFormat a = new SimpleDateFormat("MMM dd HH:mm:ss Z yyyy", Locale.getDefault());
Date d=(Date)a.parse(b)
Try to do something like this:
SimpleDateFormat formatter = new SimpleDateFormat("EEEE, MMM dd, yyyy HH:mm:ss a");
String dateInString = "Friday, Jun 7, 2013 12:10:56 PM";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
Ref: How to Convert String to Date in Java

Java date format convert on Android

I have date String as below:
Fri, 19 Jul 2013 01:30:22 GMT
Thu, 12 Sep 2013 19:19:18 GMT
And I want to convert to below format:
2013/07/19 01:30:22
2013/09/12 19:19:18
At first, I use below code to parse date:
DateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
Date date = dateFormat.parse("Fri, 23 Aug 2013 01:10:35 GMT");
But it show below exception on line Date date = dateFormat.parse("Fri, 23 Aug 2013 01:10:35 GMT");:
java.text.ParseException: Unparseable date: "Fri, 23 Aug 2013 01:10:35 GMT" (at offset 0)
How can I modify it?
try this
DateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
your default language is probably not English and does not accept Fri and Aug
Use this code
String dateString = "Fri, 19 Jul 2013 01:30:22 GMT";
SimpleDateFormat simple = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date;
try {
date = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z")
.parse(dateString);
Log.e("result", "Date:" + simple.format(date));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try this
String DateStr="Fri, 19 Jul 2013 01:30:22 GMT";
SimpleDateFormat sim=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date d = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z").parse(DateStr);
System.out.println(sim.format(d));
You can make like this.
String newDate = date.getYear()+"/"+date.getMonth()+"/"+date.getDay() and ect.
or you can write your own convert method
String myConvert(Date date)
{
return date.getYear()+"/"+date.getMonth()+"/"+date.getDay()+ect.
}
You can try this
DateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
Date date = dateFormat.parse("Fri, 23 Aug 2013 01:10:35 GMT");
String formattedString=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").
format(date).toString();
System.out.println(formattedString);
Try
String dateStr = "Fri, 19 Jul 2013 01:30:22 GMT";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z").parse(dateStr);
System.out.println(sdf.format(date));
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
date.parse("Fri, 19 Jul 2013 01:30:22 GMT");
String newDate = simpleDateFormat.format(date);
Try with "format" :
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
String date = "";
try {
date = dateFormat.format("Fri, 23 Aug 2013 01:10:35 GMT");
} catch(Exception e){
e.printStackTrace();
}

java.text.ParseException: Unparseable date: "Wed Jan 11 00:00:00 CET 2012"

I have the next problem with this date:
java.text.ParseException: Unparseable date: "Wed Jan 11 00:00:00 CET 2012"
I have this:
DateFormat formatter ;
Date dateIn=null;
formatter = new SimpleDateFormat( "EEE MMM dd HH:mm:ss yyyy" );
try {
dateIn = (Date)formatter.parse(dateI);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
What I'm doing bad?. Thanks
Use timezone and also a locale
SimpleDateFormat( "EEE MMM dd HH:mm:ss z yyyy", Locale.US);
to reflect English language in the input string (days and month names).
You need to add z in your format string for including timezone. Try this:
SimpleDateFormat( "EEE MMM dd HH:mm:ss z yyyy" );

Categories

Resources