farshad farshad - 1 month ago 7
Android Question

how to convert GMT to Local date time in android

I have a time like this : Thu, 27 Oct 2016 18:17:47 GMT

I have tried the code bellow but didn't work


String inputText = "Thu, 27 Oct 2016 18:17:47 GMT";
SimpleDateFormat inputFormat = new SimpleDateFormat
("EEE MMM dd HH:mm:ss 'GMT' yyyy", Locale.US);
inputFormat.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));

SimpleDateFormat outputFormat =
new SimpleDateFormat("MMM dd, yyyy h:mm a");
// Adjust locale and zone appropriately
Date date = inputFormat.parse(inputText);
String outputText = outputFormat.format(date);
System.out.println(outputText);

Answer

Your input date and input format don't match each other. Try this input format instead:

SimpleDateFormat inputFormat = new SimpleDateFormat
    ("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US);
Comments