Neigaard
Neigaard

Reputation: 4050

Convert timestamp from milliseconds to Date or Calendar in local time zone

I get a string with a millisecond and a timezone like this "1353913216000+0000", so a UTC time basically. How can I convert this to my local time? I do not think i can use a DateFormat, there is no millisecond pattern.

Upvotes: 0

Views: 648

Answers (2)

schippi
schippi

Reputation: 1059

you can use the class Calendar

    Calendar c= Calendar.getInstance(TimeZone.getTimeZone("GMT+2"));
    c.setTime(new Date(System.currentTimeMillis()));


    System.out.println(new Date(c.getTimeInMillis()+TimeZone.getTimeZone("GMT+2").getOffset(c.getTimeInMillis())));

Upvotes: 1

John B
John B

Reputation: 32959

If you can't use one of the built-in parsers, you could do the following:

  1. parse out the number up to the + and convert to a long.
  2. create a Date instance using this number
  3. Use a date format that ends with +zone to convert the date to a string
  4. replace the time zone with the one from the input
  5. parse the new string with the same date format

Upvotes: 0

Related Questions