DateTime convert from UTC to another Timezone in Java

Java is one of the most popular programming language in the world. It’s also one of the oldest programming languages. Due to it’s huge library and extensive community, it’s one of the best used language I have ever seen. However, I am not an advanced Java programmer but I brought some pieces from everywhere and build a small things that helped me to work with one of my practice project easily.

Date time conversion in Java

Working with date and time is a regular stuff we all do as a software engineer. And timezone conversion and other date time related operation is one of the most important task for mostly every software we develop. In Java, it’s not so easy to work with Date Time conversion. Here is some use-cases that requires java date time conversion –

  • Java timezone in UTC
  • Convert java timezone from UTC to another timezone
  • Java calendar related operation
  • Getting date and time in various timezone

But don’t be frustrated. I also faced similar types of problems multiple times while I was working with one of my Java library. That’s why I have prepared this article for you so you don’t need to spent too much time to figure it out for UTC timezone conversion in Java. Here is the script I have written myself. May be it will help you to work with Java UTC timezone.

Java class to convert UTC timezone

Let’s write a simple class which will convert our timezone to another timezone in Java.

//DateTime.java by Shaharia Azam (@shaharia)
package com.shaharia.javautil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class DateTime {
    public static String changeDateTime(String dt, String timezone) throws ParseException {
        SimpleDateFormat sdfOriginal = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdfOriginal.setTimeZone(TimeZone.getTimeZone("UTC"));
        Date date1 = sdfOriginal.parse(dt);

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a");
        sdf.setTimeZone(TimeZone.getTimeZone(timezone));
        return sdf.format(calendar.getTime());
    }
}

Ok. The class has been created. We need to provide the date time string and our expected timezone after conversion.

Java UTC timezone conversion usage

Let’s now use the class we created earlier and see the real usage of it.

package com.shaharia.javautil;

import java.text.ParseException;

public class TestDateTime {
    public static void main(String[] args) {
        
        String input = "2018-04-18 18:00:50";   //yyyy-mm-dd HH:mm:ss
        
        try {
            String t = DateTime.changeDateTime(input, "Asia/Dhaka");
            System.out.println(t);  //It outputs 2018-04-19 00:00:50
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}

See closely I just used my class here with the following code & see the output.

String t = DateTime.changeDateTime(input, "Asia/Dhaka");
System.out.println(t); //It outputs 2018-04-19 00:00:50

Write your comments if it works for you or if you face any issue, please let me know.

Shaharia is a professional software engineer with more than 10 years of experience in the relevant fields. Digital ad certified, cloud platform architect, Big data enthusiasts, tech early adopters.