0

There is the following info:

  • date ("2013-08-30 07:05:25")
  • timezone ("Europe/Moscow")

My app gets this info from some server, and I need to convert this date/time to current user's timezone. I know that I should use DateFormat and TimeZone APIs, but I don't understand how. Please, give me a piece of advice or some code. Thanks.

malcoauri
  • 11,904
  • 28
  • 82
  • 137
  • 1
    If you can include a new library, I highly recommend looking into joda-time it's a great library for date time manipulation. http://www.joda.org/joda-time/ Example http://joda-time.sourceforge.net/userguide.html#Changing_TimeZone – Ethan Aug 31 '13 at 13:54
  • see my answer, it will work for your time zone – Prateek Aug 31 '13 at 14:33

1 Answers1

0

Below code will work for you & refer this stack question for timezones-in-java :

Calendar cal = Calendar.getInstance();
cal.setTimeZone(TimeZone.getTimeZone("Europe/Moscow"));
cal.setTime(new Date());

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a");

Date resultdate = new Date(cal.getTimeInMillis());
sdf.setTimeZone(TimeZone.getTimeZone("Europe/Moscow"));

System.out.println("String date:"+sdf.format(resultdate));
try {
    System.out.println("Date:"+sdf2.parse(sdf.format(resultdate)));
} catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

OUTPUT

String date:2013-08-31 06:25:54 PM
Date:Sat Aug 31 18:25:54 IST 2013
Community
  • 1
  • 1
Prateek
  • 12,014
  • 12
  • 60
  • 81