1

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);
Razib
  • 10,965
  • 11
  • 53
  • 80
farshad
  • 1,339
  • 2
  • 9
  • 14
  • It may be too late, but don't store dates as formatted strings. Store them as numbers, and always UTC (GMT). If you are simply trying to parse a formatted date, and didn't have control over the storage format, then nevermind. – Sky Kelsey Oct 13 '16 at 18:57
  • check this link, may be helpful - http://stackoverflow.com/questions/10153286/java-gmt-to-local-time-conversion – Razib Oct 13 '16 at 19:00

2 Answers2

1

Here are a couple of functions that will do it:

public static Date toLocale(Date date) {
    int GMTOffset = (int) TimeUnit.HOURS.convert(calendar.getTimeZone().getRawOffset(),
            TimeUnit.MILLISECONDS);
    return addHours(date, GMTOffset);
}


public static Date addHours(Date date, int hours) {
    calendar.setTime(date);
    calendar.add(Calendar.HOUR_OF_DAY, hours);
    return calendar.getTime();
}
1

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);
nandsito
  • 3,782
  • 2
  • 19
  • 26