DateTime convert from UTC to another Timezone in Java – Simple way

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. Don’t be frustrated to change DatTime from UTC to another timezone in Java. I wrote it with simplicity.

//DateTime.java
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());
    }
}

I just created that class in Java. And now let’s see how to use 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();
        }
    }
}

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