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
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]
How do I parse this date format:
"Tue Dec 16 07:01:31 CET 2014"
I tried the following:
DateFormat dateformat = DateFormat.getDateTimeInstance(DateFormat.FULL,
DateFormat.FULL, Locale.CANADA);
But that doesn't seem to be correct. Do you recognize this format and know how I need to configure DateFormat?
Try this;
SimpleDateFormat parserSDF=new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);
Date d = parserSDF.parse("Tue Dec 16 07:01:31 CET 2014");
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.)