0

I am getting UTC time from server for eg - "2016-01-04T06:27:23.92". I want to convert it to Jan 4,2016 and time in Local formate. I am using following code but its not working -

Date localTime = new Date(commentDate);
String format = "yyyy/MM/dd HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date gmtTime = new Date(sdf.format(localTime));
Date fromGmt = new Date(gmtTime.getTime() + TimeZone.getDefault().getOffset(localTime.getTime()));
commentDate = commentDate.substring(0, commentDate.lastIndexOf("."));
camelCaseCoder
  • 1,447
  • 19
  • 32
unflagged.destination
  • 1,576
  • 3
  • 19
  • 38

1 Answers1

0

try this

try {
     //commentDate = "2016-01-04T06:27:23.92"
     Date date1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse(commentDate);

     Date fromGmt = new Date(date1.getTime() + TimeZone.getDefault().getOffset(date1.getTime()));
     Log.e("Date",new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(fromGmt)); // print 2016/01/04 11:57:23 (0530 hour deviation)
} catch (ParseException e) {
     e.printStackTrace();
}

You can change your date format according to your requirement.

Kunu
  • 5,078
  • 6
  • 33
  • 61