简体   繁体   中英

How to convert date into gmt in java

DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'SSSX");
df.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println("Current date and time in GMT: " + df.format("2017-01-04"));

The above is my code and when I try to convert the date to gmt I get the error:

Exception in thread "main" java.lang.IllegalArgumentException: Cannot format given Object as a Date at java.text.DateFormat.format(DateFormat.java:310) at java.text.Format.format(Format.java:157) at Sample.main(Sample.java:24)

Please point out what am I doing wrong.

 DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'SSSX");
 df.setTimeZone(TimeZone.getTimeZone("GMT"));
 System.out.println("Current date and time in GMT: " + df.format(new Date()));

This above code is working fine but I need something that works for any date.

And when I try this code:

try {
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'SSSX");
    df.setTimeZone(TimeZone.getTimeZone("GMT"));
    System.out.println("Current date and time in GMT: " + df.parse("2017-01-04"));

} catch(Exception e) {
    e.printStackTrace();
}

I am getting Unparseable date: "2017-01-04" . Please tell me how to fix it

When you have a date object then you can format it as you want:

DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
format.setTimeZone(TimeZone.getTimeZone("GMT"));

//create a calendar representing your date.
Calendar cal = Calendar.getInstance();
cal.set(2017, 01, 04);

//format the date object to the pattern you want.
String DateToStr = format.format(cal.getTime());

System.out.println(DateToStr);

If you have your date as string you can also do:

String string = "2017-01-04";
DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
format1.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = null;
try {
    date = format1.parse(string);
} catch (Exception ex) {
    ex.printStackTrace();
}
System.out.println(date);

Try it..

String s = "2016-11-21 13:30:0.0";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss);
        Date d = null;
        try 
        {
            d = format.parse(s);
        } catch (ParseException e) 
        {
            e.printStackTrace();
        }

        SimpleDateFormat gmtFormat = new SimpleDateFormat("yyyy-MM-dd'T'SSSX");

        gmtFormat.setTimeZone(TimeZone.getTimeZone("GMT"));

        System.out.println(gmtFormat.format(d));

The below code is to help in understanding better - you can try uncommenting the commented lines and compare the results. The point of interest for you in this whole block is gdf.parse(gmtFormat)

    Date date = new Date();
    DateFormat idf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    idf.setTimeZone(TimeZone.getTimeZone("IST"));
    DateFormat gdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    gdf.setTimeZone(TimeZone.getTimeZone("GMT"));

    //String istFormat = idf.format(date);
    //System.out.println("IST: "+istFormat);

    String gmtFormat = gdf.format(date);
    System.out.println("GMT: "+ gmtFormat);
    try {
        //System.out.println("gmt parsed from istFormat: "+gdf.parse(istFormat));
        //System.out.println("ist parsed from istFormat: "+idf.parse(istFormat));

        System.out.println("gmt parsed from gmtFormat: "+gdf.parse(gmtFormat));
        System.out.println("ist parsed from gmtFormat: "+idf.parse(gmtFormat));
    } catch (ParseException e) {
        e.printStackTrace();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM