如何将本地时区转换为utc和utc本地时区?

时间:2017-10-11 03:42:13

标签: java datetime utc datetime-conversion

我想将UTC时间存储在数据库(yyyy-mm-dddd-mm-yyyy

  1. 我想将本地时区转换为UTC并将其存储在DB
  2. 我尝试了不同的方法,但这种方式不正常。以下是我将本地转换为UTC的代码

    1. 从DB获取UTC时间并转换为本地时区
    2. 以下是我的代码:

      import java.text.DateFormat;
      import java.text.ParseException;
      import java.text.SimpleDateFormat;
      import java.util.Calendar;
      import java.util.Date;
      import java.util.TimeZone;
      
      public class TestDate {
      
          public static void main(String[] args) {                
              /*Date de = new Date();
              DateFormat converter1 = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
              System.err.println(converter1.format(de));
              Date der =*/
                      localToGMT();
      //          System.err.println("0000 : "+converter1.format(der));
              Date d=  new Date("09/10/2017 21:53:17");
              gmttoLocalDate(d);
          }
      
          public static Date localToGMT() {
      //      Date gmt = new Date(sdf.format(date));
              Date gmt = null;
              Date localTime =  new Date();
               //creating DateFormat for converting time from local timezone to GMT
               DateFormat converter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
      
               //getting GMT timezone, you can get any timezone e.g. UTC
               converter.setTimeZone(TimeZone.getTimeZone("GMT"));
               System.out.println("local time : " + localTime);;
               System.out.println("time in GMT : " + converter.format(localTime));
      
               try {
                  gmt =converter.parse((converter.format(localTime)));
              } catch (ParseException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
               /*Date date = new Date("2017/10/10 12:04:28");
               System.err.println(date);
                  SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
                  sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
              gmt = sdf.parse(sdf.format(date));*/
              System.err.println("converted to utc :" + gmt);
              return gmt;
          }
      
          public static Date gmttoLocalDate(Date date) {
              String timeZone = Calendar.getInstance().getTimeZone().getID();
              Date local = new Date(date.getTime() + TimeZone.getTimeZone(timeZone).getOffset(date.getTime()));
              System.err.println("converted to local timezone :" + local);
              return local;
          }
      }
      

1 个答案:

答案 0 :(得分:0)

本地到UTC

 public static Date localToUTC() {
    Date date = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
    Date gmt = new Date(sdf.format(date));
    return gmt;
    }

UTC到本地

public static Date utctoLocalDate(Date date) {

String timeZone = Calendar.getInstance().getTimeZone().getID();
Date local = new Date(date.getTime() + TimeZone.getTimeZone(timeZone).getOffset(date.getTime()));
return local

}