Java Date Create createDateTime(int year, int month, int day, int hour, int minute, int second, String timezone)

Here you can find the source of createDateTime(int year, int month, int day, int hour, int minute, int second, String timezone)

Description

create Date Time

License

BSD License

Declaration

public static Date createDateTime(int year, int month, int day, int hour, int minute, int second,
            String timezone) 

Method Source Code


//package com.java2s;
//License from project: BSD License 

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

public class Main {
    public static Date createDateTime(int year, int month, int day, int hour, int minute, int second,
            String timezone) {/*from ww w .j ava  2s.  c  om*/
        TimeZone zone = null;
        if (timezone != null && !timezone.isEmpty()) {
            zone = TimeZone.getTimeZone(timezone);
        }
        if (zone == null) {
            zone = TimeZone.getDefault();
        }
        Calendar calendar = Calendar.getInstance(zone);
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, monthNumber(month));
        calendar.set(Calendar.DAY_OF_MONTH, day);
        calendar.set(Calendar.HOUR, hour);
        calendar.set(Calendar.MINUTE, minute);
        calendar.set(Calendar.SECOND, second);
        Date date = calendar.getTime();
        return date;
    }

    public static int monthNumber(int month) {
        switch (month) {
        case 1:
            return Calendar.JANUARY;
        case 2:
            return Calendar.FEBRUARY;
        case 3:
            return Calendar.MARCH;
        case 4:
            return Calendar.APRIL;
        case 5:
            return Calendar.MAY;
        case 6:
            return Calendar.JUNE;
        case 7:
            return Calendar.JULY;
        case 8:
            return Calendar.AUGUST;
        case 9:
            return Calendar.SEPTEMBER;
        case 10:
            return Calendar.OCTOBER;
        case 11:
            return Calendar.NOVEMBER;
        case 12:
            return Calendar.DECEMBER;
        default:
            return -1;
        }
    }
}

Related

  1. createDate(int year, int month, int day, int hour, int minute, int second, int millisecond)
  2. createDateObject(Integer year, Integer month, Integer dayOfMonth, Integer hourOfDay, Integer minute, Integer second, Integer milissecond)
  3. createDates(int yy, int mm, int dd, int hh, int min, int ss)
  4. createDateString(long startDate, long endDate)
  5. createDateTime(Date date, Date time)
  6. createFutureDate()
  7. createFutureDate(int min)
  8. createInitialDateQuery(final Date date)
  9. createTime(Date date)