Java Data Type How to - SimpleDateFormat format: Thu Jun 06 2015 00:00:00 GMT+0530 (India Standard Time)








Question

We would like to know how to simpleDateFormat format: Thu Jun 06 2015 00:00:00 GMT+0530 (India Standard Time).

Answer

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
/*from   www  .  j  a  v  a 2s. c  om*/
public class Main {
  public static void main(String[] args) throws Exception {
    String input = "Thu Jun 06 2015 00:00:00 GMT+0530 (India Standard Time)";
    DateFormat inputFormat = new SimpleDateFormat(
        "E MMM dd yyyy HH:mm:ss 'GMT'z", Locale.ENGLISH);
    Date date = inputFormat.parse(input);

    DateFormat outputFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss",
        Locale.ENGLISH);
    outputFormat.setTimeZone(TimeZone.getTimeZone("UTC"));

    String output = outputFormat.format(date);
    System.out.println(output);
  }
}

The code above generates the following result.