Java TimeZone Get getTimeTry(boolean lenient, TimeZone tz, int year, int month, int day, int hour, int minute, int second, int millis)

Here you can find the source of getTimeTry(boolean lenient, TimeZone tz, int year, int month, int day, int hour, int minute, int second, int millis)

Description

get Time Try

License

Open Source License

Declaration

private static long getTimeTry(boolean lenient, TimeZone tz, int year, int month, int day, int hour, int minute,
            int second, int millis) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;

public class Main {
    private static int zoneOffset;
    private static Calendar cachedCalendar;

    private static long getTimeTry(boolean lenient, TimeZone tz, int year, int month, int day, int hour, int minute,
            int second, int millis) {
        Calendar c;/*from   w w  w.  ja  va2  s  . co m*/
        if (tz == null) {
            c = getCalendar();
        } else {
            c = Calendar.getInstance(tz);
        }
        synchronized (c) {
            c.clear();
            c.setLenient(lenient);
            setCalendarFields(c, year, month, day, hour, minute, second, millis);
            return c.getTime().getTime();
        }
    }

    private static Calendar getCalendar() {
        if (cachedCalendar == null) {
            cachedCalendar = Calendar.getInstance();
            zoneOffset = cachedCalendar.get(Calendar.ZONE_OFFSET);
        }
        return cachedCalendar;
    }

    private static void setCalendarFields(Calendar cal, int year, int month, int day, int hour, int minute,
            int second, int millis) {
        if (year <= 0) {
            cal.set(Calendar.ERA, GregorianCalendar.BC);
            cal.set(Calendar.YEAR, 1 - year);
        } else {
            cal.set(Calendar.ERA, GregorianCalendar.AD);
            cal.set(Calendar.YEAR, year);
        }
        // january is 0
        cal.set(Calendar.MONTH, month - 1);
        cal.set(Calendar.DAY_OF_MONTH, day);
        cal.set(Calendar.HOUR_OF_DAY, hour);
        cal.set(Calendar.MINUTE, minute);
        cal.set(Calendar.SECOND, second);
        cal.set(Calendar.MILLISECOND, millis);
    }
}

Related

  1. getTime(final String time, final TimeZone tz)
  2. getTime(TimeZone tz, int year, int month, int day, int hour, int minute, int second)
  3. getTime(TimeZone zone)
  4. getTimeAsPerTimeZone(String time, String timeZOne)
  5. getTimeInCurrentDay(long now, TimeZone tz, int hour, int minute, int second, int millisecond)
  6. getTimeWithTimeZone(final String timezoneId)
  7. getTimeZone(Calendar cal)
  8. getTimeZone(final String ID)
  9. getTimeZoneByOffSet(String offSetInString)