Search code examples
javadatetimeutcdatetime-conversion

How do I convert local time zone to utc and utc local time zone?


I want to store UTC time in DB (yyyy-mm-dd or dd-mm-yyyy)

  1. I want to convert local time zone to UTC and store it in DB

I tried in different ways but that is not working properly. Below is my code for convert local to UTC

  1. get the UTC time from DB and convert to local timezone

Below is my code for both:

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;
    }
}

Solution

  • Local to 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 to local

    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
    

    }