Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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

public class Main {
    public static void main(String[] args) {
        System.out.println(my_time_in("GMT-5:00", "MM/dd/yyyy HH:mm:ss"));
        System.out.println(my_time_in("GMT+5:30", "HH:mm a z 'on' MM/dd/yyyy"));

        System.out.println(my_time_in("America/Los_Angeles", "HH:mm a z 'on' MM/dd/yyyy"));
        System.out.println(my_time_in("America/Buenos_Aires", "HH:mm a z 'on' MM/dd/yyyy"));
    }

    public static String my_time_in(String target_time_zone, String format) {
        TimeZone tz = TimeZone.getTimeZone(target_time_zone);
        Date date = Calendar.getInstance().getTime();
        SimpleDateFormat date_format_gmt = new SimpleDateFormat(format);
        date_format_gmt.setTimeZone(tz);
        return date_format_gmt.format(date);
    }
}