Search code examples
javaandroiddatabasetimezonew3c

Convert W3C TimeZone to device TimeZone android


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


Solution

  • 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;
        }