Example usage for java.util TimeZone inDaylightTime

List of usage examples for java.util TimeZone inDaylightTime

Introduction

In this page you can find the example usage for java.util TimeZone inDaylightTime.

Prototype

public abstract boolean inDaylightTime(Date date);

Source Link

Document

Queries if the given date is in Daylight Saving Time in this time zone.

Usage

From source file:Main.java

public static void main(String[] args) {
    TimeZone tz = TimeZone.getTimeZone("America/New_York");

    System.out.println(tz.inDaylightTime(new Date()));
}

From source file:Main.java

public static void main(String args[]) {

    TimeZone timezoneone = TimeZone.getDefault();

    // create date object
    Date date = new Date();

    // checking day light
    System.out.println("In daylight time:" + timezoneone.inDaylightTime(date));
}

From source file:Main.java

public static Date receiverTimeToDate(long delta) {
    int currentTZOffset = TimeZone.getDefault().getRawOffset();
    long epochMS = 1230768000000L; // Jan 01, 2009 00:00 in UTC
    long milliseconds = epochMS - currentTZOffset;
    long timeAdd = milliseconds + (1000L * delta);
    TimeZone tz = TimeZone.getDefault();
    if (tz.inDaylightTime(new Date()))
        timeAdd = timeAdd - (1000 * 60 * 60);
    return new Date(timeAdd);
}

From source file:org.nodatime.tzvalidate.Java7Dump.java

/**
 * Work out the next transition from the given starting point, in the given time zone.
 * This checks each day until END, and assumes there is no more than one transition per day.
 * Once it's found a day containing a transition, it performs a binary search to find the actual
 * time of the transition./*from  w w w.  j av  a  2  s  . c  om*/
 * Unfortunately, java.util.TimeZone doesn't provide a cleaner approach than this :(
 */
private static Long getNextTransition(TimeZone zone, long start, long end) {
    long startOffset = zone.getOffset(start);
    boolean startDaylight = zone.inDaylightTime(new Date(start));

    long now = start + ONE_DAY_MILLIS;
    Date nowDate = new Date();

    // Inclusive upper bound to enable us to find transitions within the last day.
    while (now <= end) {
        nowDate.setTime(now);
        if (zone.getOffset(now) != startOffset || zone.inDaylightTime(nowDate) != startDaylight) {
            // Right, so there's a transition strictly after now - ONE_DAY_MILLIS, and less than or equal to now. Binary search...
            long upperInclusive = now;
            long lowerExclusive = now - ONE_DAY_MILLIS;
            while (upperInclusive > lowerExclusive + 1) {
                // The values will never be large enough for this addition to be a problem.
                long candidate = (upperInclusive + lowerExclusive) / 2;
                nowDate.setTime(candidate);
                if (zone.getOffset(candidate) != startOffset || zone.inDaylightTime(nowDate) != startDaylight) {
                    // Still seeing a difference: look earlier
                    upperInclusive = candidate;
                } else {
                    // Same as at start of day: look later
                    lowerExclusive = candidate;
                }
            }
            // If we turn out to have hit the end point, we're done without a final transition. 
            return upperInclusive == end ? null : upperInclusive;
        }
        now += ONE_DAY_MILLIS;
    }
    return null;
}

From source file:Main.java

public static String getTimeZoneOffset() {
    TimeZone timezone = TimeZone.getDefault();
    int i = timezone.getRawOffset() / 1000;
    int j;/* w  w  w  .  j  a  v a2  s  .  c om*/
    double d;
    Object aobj[];
    if (timezone.useDaylightTime() && timezone.inDaylightTime(new java.sql.Date(System.currentTimeMillis())))
        j = 1;
    else
        j = 0;
    d = (double) i / 3600D + (double) j;
    aobj = new Object[1];
    aobj[0] = Double.valueOf(d);
    return String.format("%.2f", aobj);
}

From source file:com.hp.hpl.jena.sparql.util.Utils.java

private static String calcTimezone(Calendar cal) {
    Date date = cal.getTime();/* www .  j a  v  a 2  s  .  c  o  m*/
    TimeZone z = cal.getTimeZone();
    int tz = z.getRawOffset();

    if (z.inDaylightTime(date)) {
        int tzDst = z.getDSTSavings();
        tz = tz + tzDst;
    }

    String sign = "+";
    if (tz < 0) {
        sign = "-";
        tz = -tz;
    }

    int tzH = tz / (60 * 60 * 1000); // Integer divide towards zero.
    int tzM = (tz - tzH * 60 * 60 * 1000) / (60 * 1000);

    String tzH_str = Integer.toString(tzH);
    String tzM_str = Integer.toString(tzM);

    if (tzH < 10)
        tzH_str = "0" + tzH_str;
    if (tzM < 10)
        tzM_str = "0" + tzM_str;
    return sign + tzH_str + ":" + tzM_str;
}

From source file:es.tekniker.framework.ktek.util.Utils.java

private static Calendar getCalendarUTC() {
    Calendar c = Calendar.getInstance();
    System.out.println("current: " + c.getTime());

    TimeZone z = c.getTimeZone();
    int offset = z.getRawOffset();
    if (z.inDaylightTime(new Date())) {
        offset = offset + z.getDSTSavings();
    }//from   w  w w. ja va 2 s . c om
    int offsetHrs = offset / 1000 / 60 / 60;
    int offsetMins = offset / 1000 / 60 % 60;

    System.out.println("offsetHrs: " + offsetHrs);
    System.out.println("offsetMins: " + offsetMins);
    c.add(Calendar.HOUR_OF_DAY, (-offsetHrs));
    c.add(Calendar.MINUTE, (-offsetMins));

    System.out.println("GMT Time: " + c.getTime());

    return c;
}

From source file:es.tekniker.framework.ktek.util.Utils.java

private static long getTimeinMillis4TimeUTC(int hours, int minutes, int seconds) {
    Calendar c = Calendar.getInstance();
    System.out.println("current: " + c.getTime());

    TimeZone z = c.getTimeZone();
    int offset = z.getRawOffset();
    if (z.inDaylightTime(new Date())) {
        offset = offset + z.getDSTSavings();
    }/*  w w  w .j a  v a 2s . c o  m*/
    int offsetHrs = offset / 1000 / 60 / 60;
    int offsetMins = offset / 1000 / 60 % 60;

    System.out.println("offsetHrs: " + offsetHrs);
    System.out.println("offsetMins: " + offsetMins);

    c.set(Calendar.HOUR_OF_DAY, (hours - offsetHrs));
    c.set(Calendar.MINUTE, (minutes - offsetMins));
    c.set(Calendar.SECOND, seconds);

    System.out.println("GMT Time: " + c.getTime());

    long timeinmillis = c.getTimeInMillis();
    System.out.println(" system time in millis " + timeinmillis);

    return timeinmillis;
}

From source file:es.tekniker.framework.ktek.util.Utils.java

private static long getTimeinMillis4FullDateTimeUTC(int year, int month, int day, int hours, int minutes,
        int seconds) {
    Calendar c = Calendar.getInstance();
    System.out.println("current: " + c.getTime());

    TimeZone z = c.getTimeZone();
    int offset = z.getRawOffset();
    if (z.inDaylightTime(new Date())) {
        offset = offset + z.getDSTSavings();
    }//from   w  w  w . java  2s . co m
    int offsetHrs = offset / 1000 / 60 / 60;
    int offsetMins = offset / 1000 / 60 % 60;

    System.out.println("offsetHrs: " + offsetHrs);
    System.out.println("offsetMin: " + offsetMins);

    c.set(Calendar.YEAR, year);
    c.set(Calendar.MONTH, month);
    c.set(Calendar.DAY_OF_MONTH, day);

    c.set(Calendar.HOUR_OF_DAY, (hours - offsetHrs));
    c.set(Calendar.MINUTE, (minutes - offsetMins));
    c.set(Calendar.SECOND, seconds);

    System.out.println("GMT Time: " + c.getTime());

    long timeinmillis = c.getTimeInMillis();
    System.out.println(" system time in millis " + timeinmillis);

    return timeinmillis;
}

From source file:at.newsagg.utils.ParserUtils.java

private static int localTimeDiff(TimeZone tz, Date date) {
    if (tz.inDaylightTime(date)) {
        int dstSavings = 0;
        if (tz.useDaylightTime()) {
            dstSavings = 3600000; // shortcut, JDK 1.4 allows cleaner impl
        }//from w  ww  .jav  a 2  s  .co m
        return tz.getRawOffset() + dstSavings;
    }
    return tz.getRawOffset();
}