java – Convert Local time to UTC and vice versa-ThrowExceptions

Exception or error:

I’m working on Android application, and I want to convert local time (device time) into UTC and save it in database. After retrieving it from database I have to convert it again and display in the device’s time zone. Can anyone suggest how to do this in Java?

How to solve:

I converted local time to GMT/UTC and vice versa using these two methods and this works fine without any problem for me.

public static Date localToGMT() {
    Date date = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    Date gmt = new Date(sdf.format(date));
    return gmt;

pass the GMT/UTC date which you want to convert into device local time to this method:

public static Date gmttoLocalDate(Date date) {

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


A simplified and condensed version of the accepted answer:

public static Date dateFromUTC(Date date){
    return new Date(date.getTime() + Calendar.getInstance().getTimeZone().getOffset(date.getTime()));

public static Date dateToUTC(Date date){
    return new Date(date.getTime() - Calendar.getInstance().getTimeZone().getOffset(date.getTime()));


Try this:
//convert to UTC to Local format

 public Date getUTCToLocalDate(String date) {
            Date inputDate = new Date();
            if (date != null && !date.isEmpty()) {
                @SuppressLint("SimpleDateFormat") SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
                try {
                    inputDate = simpleDateFormat.parse(date);
                } catch (ParseException e) {
            return inputDate;

//convert Local Date to UTC

public String getLocalToUTCDate(Date date) {
    Calendar calendar = Calendar.getInstance();
    Date time = calendar.getTime();
    @SuppressLint("SimpleDateFormat") SimpleDateFormat outputFmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    return outputFmt.format(time);


you may try something like this to insert into DB:

    SimpleDateFormat f = new SimpleDateFormat("h:mm a E zz");
    System.out.println(f.format(new Date()));
    String dd = f.format(new Date());

This opt from ur comment:


1:43 PM Mon UTC

For this, -> convert it again and display in the device’s time


String dd = f.format(new Date());

        Date date = null;
        DateFormat sdf = new SimpleDateFormat("h:mm a E zz");
        try {
            date = sdf.parse(dd);
        }catch (Exception e){



7:30 PM Mon GMT+05:30

U may display like this.



will get you the timezone and

Calendar c = Calendar.getInstance();
int seconds = c.get(Calendar.SECOND)

will get you the time in UTC in seconds. Of course you can change the value to get it in another unit.



The modern approach uses the java.time;classes that years ago supplanted the terrible date-time classes such as Date and Calendar.

Capture the current moment as seen in UTC.

OffsetDateTime odt = ZoneOffset.UTC ) ;

Store in a database using a driver complaint with JDBC 4.2 or later.

myPreparedStatement( … , odt ) ;

Retrieve from database.

OffsetDateTime odt = myResultSet.getObject( … , OffsetDateTime.class ) ;

Adjust into a time zone.

ZoneId z = ZoneId.systemDefault() ;
ZonedDateTime zdt = odt.atZoneSameInstant( z ) ;

Generate text to present to user.

DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.FULL ).withLocale( Locale.getDefault() ) ;
String output = zdt.format( f ) ;

For early Android before version 26, use the ThreeTenABP library to get most of the java.time functionality that was back-ported to Java 6 and Java 7 in the ThreeTen-Backport project.


Get current UTC :

public String getCurrentUTC(){
        Date time = Calendar.getInstance().getTime();
        SimpleDateFormat outputFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return outputFmt.format(time);


try this one

DateFormat formatterIST = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        Date dateobj = new Date();
        Date date = formatterIST.parse(formatterIST.format(dateobj));

        DateFormat formatterUTC = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        formatterUTC.setTimeZone(TimeZone.getTimeZone("UTC")); // UTC timezone

Leave a Reply

Your email address will not be published. Required fields are marked *