2

See the below code

Date date1 = new Date(HttpDateParser.parse(dateString);
int offset = TimeZone.getTimeZone("GMT+5:30").getRawOffset();
date1.setTime(date1.getTime() + offset);
String pattern = "yyyy-MM-dd hh:mma";
SimpleDateFormat date = new SimpleDateFormat(pattern);
String dateNow = date.format(date1);

It converts fine to indian standard time in simulator. When i try to use in device, the time remain unchanged.

Richard
  • 8,920
  • 2
  • 18
  • 24
Hiren Gujarati
  • 1,039
  • 14
  • 32

2 Answers2

2

HttpDateParser.parse says in the docs:

Parses a date string and returns the number of milliseconds since January 1, 1970, 00:00:00 GMT.

If you want to display the time in the device's own timezone, you don't need to do all that timezone conversion, just use SimpleDateFormat directly:

long timeSinceEpoch = HttpDateParser.parse(dateString);
String pattern = "yyyy-MM-dd hh:mma";
SimpleDateFormat date = new SimpleDateFormat(pattern);
String dateNow = date.formatLocal(timeSinceEpoch);
Michael Donohue
  • 11,776
  • 5
  • 31
  • 44
  • Is the device timezone set to indian standard time? – Michael Donohue Jan 08 '11 at 05:22
  • Yes. Also for testing purpose, i am using GMT+5.50 for converting the time. It works fine in simulator but not work in device. I got the difference 5.30hrs between date & converted date in simulator. The code you provided, give the same date as source date. – Hiren Gujarati Jan 08 '11 at 05:29
0
 Parses a date string and returns the number of milliseconds since January 1, 1970, 00:00:00 GMT.

But what happen if i pass milliseconds to Date() function. It automatically gives date converting to user's local timezone. Cheers.

The below code have done everything fine.

 Date date1 = new Date(HttpDateParser.parse((String)kValue))

The date1 already converted to user's local timezone.

Hiren Gujarati
  • 1,039
  • 14
  • 32