Example usage for java.util TimeZone getDSTSavings

List of usage examples for java.util TimeZone getDSTSavings

Introduction

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

Prototype

public int getDSTSavings() 

Source Link

Document

Returns the amount of time to be added to local standard time to get local wall clock time.

Usage

From source file:Main.java

public static void main(String args[]) {

    TimeZone timezone = TimeZone.getTimeZone("Europe/Paris");

    // checking DST saving         
    System.out.println("DST saving is :" + timezone.getDSTSavings());
}

From source file:Main.java

/**
 * Produce a string describing the offset of the given time zone from
 * UTC, including the DST offset if there is one.
 * /*from  ww  w . ja va  2 s.  co m*/
 * @param   zone         TimeZone whose offset we want.
 * @return               Formatted offset.
 */
public static String formatOffset(TimeZone zone) {
    String fmt = "";

    int base = zone.getRawOffset();
    fmt += "UTC" + intervalMsToHmsShort(base);

    int dst = zone.getDSTSavings();
    if (dst != 0)
        fmt += " (UTC" + intervalMsToHmsShort(base + dst) + ")";

    return fmt;
}

From source file:org.forgerock.openam.cts.TokenTestUtils.java

/**
 * Required to correctly account for all factors when dealing with TimeZones.
 *
 * @param zone Non null.// ww w.j a  v  a 2s.c  om
 * @return
 */
private static int getTotalTimeZoneOffset(TimeZone zone) {
    int r = zone.getRawOffset();
    if (zone.useDaylightTime()) {
        r += zone.getDSTSavings();
    }
    return r;
}

From source file:Main.java

/**
 * Produce a string describing the offset of the given time zone from
 * UTC, including the DST offset if there is one.
 * //from  ww w  .  j  ava  2 s.c o  m
 * @param   zone         TimeZone whose offset we want.
 * @return               Formatted offset.
 */
public static String formatOffsetFull(TimeZone zone) {
    String fmt = zone.getID() + ": ";

    int base = zone.getRawOffset();
    fmt += zone.getDisplayName(false, TimeZone.LONG) + "=UTC" + intervalMsToHmsShort(base);

    int dst = zone.getDSTSavings();
    if (dst != 0)
        fmt += " (" + zone.getDisplayName(true, TimeZone.LONG) + "=UTC" + intervalMsToHmsShort(base + dst)
                + ")";

    return fmt;
}

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

private static String calcTimezone(Calendar cal) {
    Date date = cal.getTime();//from www. ja va2  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   www  . ja  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.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();
    }/*from www  .  j a v  a  2 s . 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();
    }/* w w  w .ja  v  a 2s. com*/
    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:com.mycompany.craftdemo.utility.java

public static boolean isValidTime() {
    Calendar calendar = Calendar.getInstance();
    TimeZone fromTimeZone = calendar.getTimeZone();
    TimeZone toTimeZone = TimeZone.getTimeZone("EST");

    calendar.setTimeZone(fromTimeZone);//from w w  w. j  a  va2 s  .c o m
    calendar.add(Calendar.MILLISECOND, fromTimeZone.getRawOffset() * -1);
    if (fromTimeZone.inDaylightTime(calendar.getTime())) {
        calendar.add(Calendar.MILLISECOND, calendar.getTimeZone().getDSTSavings() * -1);
    }

    calendar.add(Calendar.MILLISECOND, toTimeZone.getRawOffset());
    if (toTimeZone.inDaylightTime(calendar.getTime())) {
        calendar.add(Calendar.MILLISECOND, toTimeZone.getDSTSavings());
    }

    int hour = calendar.get(Calendar.HOUR_OF_DAY);

    return hour >= 9 && hour <= 16;
}

From source file:org.sakaiproject.component.section.sakai.CourseSectionImpl.java

public static final Time convertStringToTime(String str) {
    if (StringUtils.trimToNull(str) == null) {
        return null;
    }/*from  ww  w .  j  ava2s  .c o  m*/
    try {
        SimpleDateFormat sdf = new SimpleDateFormat(CourseSectionImpl.TIME_FORMAT_LONG);
        return new Time(sdf.parse(str).getTime());
    } catch (Exception e) {

        // Stored in other format, with date and time zone. 
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(CourseSectionImpl.TIME_FORMAT_DATE_TZ);

            Calendar src = new GregorianCalendar();
            src.setTime(sdf.parse(str));
            src.setTimeInMillis(src.getTimeInMillis());

            TimeZone srcTz = sdf.getTimeZone();
            TimeZone userTz = timeService.getLocalTimeZone();
            TimeZone serverTz = TimeZone.getDefault();

            Calendar now = new GregorianCalendar(userTz);

            // STORED IN DAYLIGHT SAVING TIME and NOW IS STANDARD
            if (srcTz.inDaylightTime(src.getTime()) && !srcTz.inDaylightTime(now.getTime())) {
                src.setTimeInMillis(src.getTimeInMillis() + srcTz.getDSTSavings());
            }

            // STORED IN STANDAR TIME and NOW IS DAYLIGHT SAVING TIME
            if (srcTz.inDaylightTime(now.getTime()) && !srcTz.inDaylightTime(src.getTime())) {
                src.setTimeInMillis(src.getTimeInMillis() - srcTz.getDSTSavings());
            }

            // DO THE SAME IN SERVER TIMEZONE
            if (serverTz.inDaylightTime(src.getTime()) && !serverTz.inDaylightTime(now.getTime())) {
                src.setTimeInMillis(src.getTimeInMillis() - serverTz.getDSTSavings());
            }

            if (serverTz.inDaylightTime(now.getTime()) && !serverTz.inDaylightTime(src.getTime())) {
                src.setTimeInMillis(src.getTimeInMillis() + serverTz.getDSTSavings());
            }

            src.set(Calendar.DAY_OF_MONTH, now.get(Calendar.DAY_OF_MONTH));
            src.set(Calendar.YEAR, now.get(Calendar.YEAR));
            src.set(Calendar.MONTH, now.get(Calendar.MONTH));

            return new Time(src.getTimeInMillis() + userTz.getOffset(now.getTimeInMillis())
                    - serverTz.getOffset(now.getTimeInMillis()));

        } catch (Exception ex) {
            if (log.isDebugEnabled())
                log.debug("Unable to parse " + str);
            return null;
        }
    }
}