1

What I have

I have a date string from server which is in w3c date format 2016-02-13T09:53:49.871Z

My problem

When I post the message to the server , it shows 5 hrs before instead of just now

What I want

I wanted the server TimeZone to be converted to the device TimeZone , so that irrespective of the region , the user will see his local time format

karthik kolanji
  • 2,044
  • 5
  • 20
  • 56

2 Answers2

1

I solved it , If any one needs can refer this code

public String convertW3CTODeviceTimeZone(String strDate) throws Exception {
        SimpleDateFormat simpleDateFormatW3C = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);
        simpleDateFormatW3C.setTimeZone(TimeZone.getTimeZone("GMT"));
        Date dateServer = simpleDateFormatW3C.parse(strDate);

        TimeZone deviceTimeZone = TimeZone.getDefault();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        simpleDateFormat.setTimeZone(deviceTimeZone);

        String formattedDate = simpleDateFormat.format(dateServer);
        return formattedDate;
    }
karthik kolanji
  • 2,044
  • 5
  • 20
  • 56
  • 3
    "W3C" is not a valid zone identifier. You are just lucky that in this case, Android will silently fall back to GMT. So use "GMT" instead (for correct interpretation of trailing Z in your input). – Meno Hochschild Mar 07 '16 at 12:03
  • 2016-08-26T00:00:00+00:00 how to convert this? having "+00:00" – Prasad Aug 26 '16 at 12:54
0

Hope this blogspot and link what you expecting:

http://javagrip.blogspot.in/2012/09/date-from-one-timezone-to-another-in-java.html

How to convert UTC timestamp to device local time in android

Community
  • 1
  • 1
swatz
  • 55
  • 11