Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.Calendar;
import java.util.Date;

import java.util.TimeZone;

public class Main {
    public static Date toTimeZone(Date date, TimeZone source, TimeZone target) {
        Calendar cal1 = Calendar.getInstance(source);
        cal1.setTime(date);

        int[] dateSplit = splitDate(date);

        Calendar cal2 = Calendar.getInstance(target);
        cal2.set(dateSplit[0], dateSplit[1], dateSplit[2], dateSplit[3], dateSplit[4], dateSplit[5]);
        return cal2.getTime();
    }

    public static int[] splitDate(Calendar calendar) {
        return new int[] { calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.HOUR_OF_DAY),
                calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND) };
    }

    public static int[] splitDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return splitDate(calendar);
    }
}