I am using the below code to parse a date, and to format it how I want; the code is working fine for other dates such as this one:
Thu, 04 Sep 2014 19:32:00 GMT
But it is throwing the above error message on a date such as this:
Fri, 05 Sep 2014 06:01:00 +0100
Can anyone explain why?
Thanks.
The code:
//Only get the 'important' parts.
pubDateFormat = pubDate.substring(0, 23);
//Format it how I want.
dateFormatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm");
//Here is where it is breaking.
Date date = dateFormatter.parse(pubDateFormat);
//Format it once again to how I want.
dateFormatter = new SimpleDateFormat("dd/MM/yyyy");
//get the current date and format it the same.
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
currDate = sdf.format(new Date());
currDate = currDate.replace(" ", "");
rssDate = dateFormatter.format(date).replace(" ", "");
This is the log:
09-05 11:30:07.153: E/DATE(7665): Fri, 05 Sep 2014 06:01
09-05 11:30:07.153: E/ERROR(7665): java.text.ParseException: Unparseable date: "
09-05 11:30:07.153: E/ERROR(7665): Fri, 05 Sep 2014 06:01" (at offset 0)
You have to add the locale:
SimpleDateFormat dateFormatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm", Locale.ENGLISH);
Try this
DateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm", Locale.US);
Related
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
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();
}
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]
I want to parse this date "Mon Mar 09 2015 00:00:00 GMT+0530 (India Standard Time)" in java SimpleDateFormat class as "MM-dd-yyyy", is this a valid date format? If yes, how can I do that?
String inputDate = "Wed Sep 17 2014 00:00:00 GMT+0530 (India Standard Time)";
//SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss"); -- Not working
SimpleDateFormat sdf = new SimpleDateFormat("E MMM dd yyyy HH:mm:ss z"); // Not Working
Date date = sdf.parse(inputDate);
sdf = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(sdf.format(date));
Tried following piece of code:
String inputDate = "Wed Sep 17 2014 00:00:00 GMT+0530 (India Standard Time)";
SimpleDateFormat sdf = new SimpleDateFormat("E MMM dd yyyy HH:mm:ss 'GMT'z");
Date date = sdf.parse(inputDate);
sdf = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(sdf.format(date));
Output:
2014-09-17
Try by using this format "E MMM dd yyyy HH:mm:ss z" with SimpleDateFormat class.
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();
}