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,

Jaydip
  • 592
  • 5
  • 27
Jai
  • 1,974
  • 2
  • 22
  • 42
  • with standard timezones you mean replacing "GST" with "GMT" and still not working? – Nanoc Nov 03 '15 at 16:10
  • Hey its working fine above code for me . – M S Parmar Nov 03 '15 at 16:19
  • @Nanoc : Nope, It's still not working! – Jai Nov 03 '15 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 03 '15 at 16:48
  • Looks like somewhat device related issue – Nanoc Nov 03 '15 at 16:48
  • @Nanoc : I have tested on Nexus 5, Nexus 4, Samsung galaxy S3 and S4. On none of them it's working! So It might not be device related issue.I checked on version 5.1.1 and 4.4 as well – Jai Nov 03 '15 at 16:51
  • Then how it is possible that some people said that code works, i dont have time to test it myself but i would bet is a device language/timezone configuration not the device itself – Nanoc Nov 03 '15 at 16:53
  • @Nanoc : that's why I posted question :) Why its not permanent solution for everyone? – Jai Nov 03 '15 at 16:56
  • @Nanoc : Even see I have tested with same timezone which I'm having on my device.And the thing is we can't change it manually because this data is coming from twitter's API. So It would be different timezone for each user :) – Jai Nov 03 '15 at 16:57

3 Answers3

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.

Nilesh Pawar
  • 3,895
  • 3
  • 21
  • 19
  • 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 04 '15 at 03: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. – Nilesh Pawar Nov 04 '15 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 – Nilesh Pawar Nov 04 '15 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);
-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.

Jai
  • 1,974
  • 2
  • 22
  • 42
  • 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. – Nilesh Pawar Nov 17 '15 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 – Nilesh Pawar Nov 17 '15 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 '15 at 03: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 '15 at 03: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 '15 at 03:44
  • @NileshPawar : The original problem was android is unable to parse that format. So Format was the original issue. I mentioned that it could be anything like GST or EST or IST, it depends on user's location. That's why I made easy solution by removing and adding default timezone of users. Because timezone getting in API is equal to default timezone of user's location. The solution was given by you to replace it with GMT, for that I need to check each and every timezone with corresponding GMT value. :) – Jai Nov 18 '15 at 06:02
  • Not sure of the data flow in your case. If the date you are receiving from twitter API is generated from the same time zone as the device user, it will work. However if your app receives dates generated in some other timezone than the device user, its clearly going to fail. Example if twitter API sends you a Date for something that was generated in UAE and your app is running in Australia, its going to be displayed wrong. – Nilesh Pawar Nov 18 '15 at 06:07
  • @NileshPawar : But it will not be the case right? Because not in case of twitter only in any other SDKs as well when you will ask for data they will give you data with your timezone only. :) So atleast remove minus rating so this can be helpful to other guys :) – Jai Nov 18 '15 at 07:57