0

I know It's very known and easy solution for the issue that I'm going to describe here. I'm trying to parse following date format but getting parse exception only even if passing right format to parse that String.

try{
  String mDateFormat = "Tue Nov 03 13:46:28 GST 2015";
  SimpleDateFormat mFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
  Date mFormattedDate = mFormat.parse(mDateFormat);

  }catch(ParseException e){
     e.printStackTrace();
}

Timezone included with this String is throwing crash only. If I will remove it, It's able to parse it properly. So Something is wrong with timezone format but Even I tried the standards declared by this link : SimpleDateFormat By Developer Site

Anyone suggest me what's wrong here?

Thanks,

9
  • with standard timezones you mean replacing "GST" with "GMT" and still not working?
    – Nanoc
    Nov 3, 2015 at 16:10
  • Hey its working fine above code for me .
    – M S Parmar
    Nov 3, 2015 at 16:19
  • @Nanoc : Nope, It's still not working!
    – Jai
    Nov 3, 2015 at 16:47
  • @MiteshParmar : May I know on which version of android are you testing? and on which specific device is it working?
    – Jai
    Nov 3, 2015 at 16:48
  • Looks like somewhat device related issue
    – Nanoc
    Nov 3, 2015 at 16:48

3 Answers 3

0

From all the experiments I did so far, it seems that the Android DateFormat parser implementation on which you are seeing a problem does not understand the GST zone. The simplest solution would be to use the formatter explicitly by replacing the GST to to GMT+4 as:

String mDateFormat = "Tue Nov 03 13:46:28 GMT+4 2015";

With this date string, Android seems to be returning the same conversion as the Desktop equivalent.

P.S (removing the TimeZone from the formatter wont be a correct solution since the conversions wont be accurate. Since you are getting this date from the Twitter API, you may need to filter the un-acceptible strings before formating on Android you can use:

mDateFormat.replace("GST", "GMT+4") 

before applyeing the DateFormat to the data.

3
  • Hi, Thanks for your answer. So did you mean for each timzone like PST, IST. GST whatever I will get, I need to replace that all by "GMT+4"? Will it be accurate in case of different different Timezone? And In these case I need to check each and every timezone to replace it right? because this GST will not fixed if user will use application from different country!
    – Jai
    Nov 4, 2015 at 3:27
  • No. PST is GMT-8 and IST is GMT+5:30 What I meant was, currently it seems that Android is not recognizing the GST. Its not even recognizing IST. So you will need to additionally do: mDateFormat.replace("IST", "GMT+5:30") The idea is when you receive any un-recognized timezone , you may need to replace it with the equivalent GMT+/- Number. You can write a small hello world app and find out which of the all possible timezone are not recognized by Android (PST is recognized) and then write a string replacement code for them. Nov 4, 2015 at 15:50
  • You can get GMT vs other timzeones list and GMT offset from timeanddate.com/time/zones Note that the website has listed the time offsets compared to UTC. But they can be applied to GMT as well since there is no time difference between UTC and GMT Nov 4, 2015 at 16:12
-1

if you are using english date then change the code to

SimpleDateFormat parserSDF = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy", Locale.ENGLISH);
3
  • This one still gives the exception on Android he is getting. @Jai's original code runs on desktop correctly thought. Nov 3, 2015 at 16:35
  • it worked for me when same exception was raised in my code before. Nov 3, 2015 at 17:33
  • But it throws exception now . clueless!! Nov 3, 2015 at 17:34
-1

What I did as easy solution :

1) Separate Timezone as String from the below format and replace with "" :

String mDateFormat = "Tue Nov 03 13:46:28 GST 2015";
String mTimeZone = mDateFormat.substring(20,23);
String mActualDate = mDateFormat.replace(mTimeZone + " ", "");

2) Get the default Timezone and Set the same one while applying SimpleDateFormat

final String TWITTER = "EEE MMM dd HH:mm:ss yyyy";
SimpleDateFormat mSf = new SimpleDateFormat(TWITTER, Locale.ENGLISH);
mSf.setTimeZone(TimeZone.getDefault());
Date mNewDate = sf.parse(mActualDate);

3) Get the updated date with same Timezone. No need to convert with GMT stuff.

8
  • The reason why this is wrong is you are removing the original Timezone before attempting the conversion. This new code gives wrong result if you compare the output of your original code running on a desktop vs your new code running on Android. Following is the sample results: Date used for Test: "Tue Nov 03 13:46:28 GST 2015"; Orginal Code from your question on Desktop converts this to: Tue Nov 03 04:46:28 EST 2015. Your new code Convesrts this to : Tue Nov 03 13:46:28 EST 201.Notice the difference in time 4 vs 13.If the code was correct the results should match. Nov 17, 2015 at 15:56
  • To give a bit of an analogy of why the original Timezone is important to be retained before date conversion is as follows. Assume you need to convert currency say from $1 to Yens. With your new method You are removing the $ (its like the currency zone) , then inserting the Locale currency. So by your method $1 = 1Yen which is not correct. Basically by removing the $ you are modifying the question from "What is $1 converted to Yen" to "What is 1 Yen Converted to Yen" which gives flawed results Nov 17, 2015 at 17:01
  • @NileshPawar : Dude, I', removing original timezone but adding same timezone if you will check with setTimeZone method I used.
    – Jai
    Nov 18, 2015 at 3:37
  • @NileshPawar : And your desktop is may have EST timing . That's why it's converting to EST. but it's wrong validation that you are checking. Because GST was the hardcod string for me because I'm in GST timezone. If you will read any data from twitter api, You will get EST in resutl not GST, So my process will simply remove it first and then it will add your EST timezone of your desktop again. So kindly understand the process :)
    – Jai
    Nov 18, 2015 at 3:40
  • @NileshPawar : For your test case, You should use Nov 03 13:46:28 EST 2015 instead of GST. Then you will have exact result. And that it what the actual use case we should try. Of course it will not work if you will give input with GST and will add EST (as default timezone of your system.) :)
    – Jai
    Nov 18, 2015 at 3:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.