Conversion of local time zone to GMT in java

前端 未结 3 1699
青春惊慌失措
青春惊慌失措 2020-12-30 13:05

Am trying convert date which is in local time zone to GMT, i did some thing like this

SimpleDateFormat sdf = new SimpleDateFormat(\"MM/dd/yyyy HH:mm:ss\");         


        
相关标签:
3条回答
  • 2020-12-30 13:32

    After 'parse' you have to again format it before printing, e.g.:

    System.out.println("GMT 2"+sdf.format(gmtDate));
    

    EDIT: The reason is your gmtStrDate doesn't store any timezone information

    0 讨论(0)
  • 2020-12-30 13:40
    import java.util.*;
    import java.text.*;
    public class CurrentTimeToGMT{
      public static void main(String args[]){
        Date date = new Date();
        DateFormat gmtFormat = new SimpleDateFormat();
        TimeZone gmtTime = TimeZone.getTimeZone("GMT");
        gmtFormat.setTimeZone(gmtTime);
        System.out.println("Current Time: "+date);
        System.out.println("GMT Time: " + gmtFormat.format(date));
    
      }
    }
    

    Output

    Current Time: Wed Mar 10 11:21:18 IST 2010
    
    GMT Time: 3/10/10 5:51 AM
    
    0 讨论(0)
  • 2020-12-30 13:46

    You can do this using this function say you want to convert local time to GMT time and also this will be GMT+6 or GMT+5 then just use this function. This function will return answer.

    public String getCurrentDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy MM dd hh:mm a zzz");
        Date date = new Date();
        sdf.setTimeZone(TimeZone.getTimeZone("GMT+6:00"));
        return sdf.format(date);
     }
    
    0 讨论(0)
提交回复
热议问题