GMT Timezone not detected by SimpleFormat in Android
I have GMT posts date coming from server as
2015-05-14 12:27:35
I am using the following code to convert it to text diff.
Calendar systemCal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
try {
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = (Date) formatter.parse(dateString);
/***********************************/
CharSequence myDateString = DateUtils.getRelativeTimeSpanString(date.getTime(), systemCal.getTimeInMillis(), DateUtils.MINUTE_IN_MILLIS);
return myDateString.toString().replace("minutes", "min");
Now I am getting the problem in
Date date = (Date) formatter.parse(dateString);
As a result
Thu May 14 05:27:35 GMT+05:00 2015
Now my question is, why is it using my device's default timezone when I have already set the GMT timezone and how to parse it using the GMT timezone?
+3
source to share
1 answer
You are setting GMT for systemCal. Use the same timezone when you are using the created date. It uses the default timezome and that makes the difference.
Calendar systemCal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = (Date) formatter.parse("2015-02-03 10:11:12");
System.out.println(date); // => Tue Feb 03 05:11:12 EST 2015
System.out.println(formatter.format(date)); // => 2015-02-03 10:11:12
+1
source to share