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:org.apache.accumulo.monitor.servlets.DefaultServlet.java

/**
 * Shows the current time zone (based on the current time) short name
 *///from w  w w .  j  a v  a2  s  . c  om
private static String getShortTZName() {
    TimeZone tz = TimeZone.getDefault();
    return tz.getDisplayName(tz.inDaylightTime(new Date()), TimeZone.SHORT);
}

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 ww .j  av  a  2 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:com.lm.lic.manager.util.GenUtil.java

public static Date resetClientTimezone(HttpServletRequest request, Date date) {
    final TimeZone timeZone = TimeZone.getDefault();
    final boolean daylight = timeZone.inDaylightTime(date);
    final Locale locale = request.getLocale();
    String tzName = timeZone.getDisplayName(daylight, TimeZone.LONG, locale);
    // String countryCode = locale.getCountry();
    // TimeZone ltz = TimeZone.getTimeZone(countryCode);
    // TimeZoneNameUtility.getZoneStrings(locale);
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);//from w  w  w.  ja  v  a2  s . com
    cal.setTimeZone(TimeZone.getTimeZone(tzName));
    return cal.getTime();
}

From source file:net.sf.jhylafax.fax.HylaFAXClientHelper.java

public static String calculateTime(Date sendTime, String timeZoneID) {
    if (sendTime == null) {
        return "NOW";
    } else {// www  . jav  a 2  s  .co  m
        long date = sendTime.getTime();

        TimeZone tz = TimeZone.getTimeZone(timeZoneID);
        //         tz.setStartRule(Calendar.MARCH, -1, Calendar.SUNDAY,  2*60*60*1000);
        //         tz.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY,  2*60*60*1000);

        date -= tz.getRawOffset();
        if (tz.inDaylightTime(sendTime)) {
            date -= 3600 * 1000;
        }

        return new SimpleDateFormat("yyyyMMddHHmm").format(new Date(date));
    }
}

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   www.j  a  v a  2  s  .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;
        }
    }
}

From source file:com.hurence.logisland.utils.DateUtils.java

public static String outputDateInfo(Date d) {
    String output = "";
    Calendar c = GregorianCalendar.getInstance(tz);
    c.setTimeInMillis(d.getTime());//w  w  w  .  ja  v a  2s.c  o m
    TimeZone tzCal = c.getTimeZone();

    output += "Date:                         " + d + "\n"; // toString uses current system TimeZone
    output += "Date Millis:                  " + d.getTime() + "\n";
    output += "Cal Millis:                   " + c.getTimeInMillis() + "\n";
    output += "Cal To Date Millis:           " + c.getTime().getTime() + "\n";
    output += "Cal TimeZone Name:            " + tzCal.getDisplayName() + "\n";
    output += "Cal TimeZone ID:              " + tzCal.getID() + "\n";
    output += "Cal TimeZone DST Name:        " + tzCal.getDisplayName(true, TimeZone.SHORT) + "\n";
    output += "Cal TimeZone Standard Name:   " + tzCal.getDisplayName(false, TimeZone.SHORT) + "\n";
    output += "In DayLight:                  " + tzCal.inDaylightTime(d) + "\n";

    output += "" + "\n";
    output += "Day Of Month:                 " + c.get(Calendar.DAY_OF_MONTH) + "\n";
    output += "Month Of Year:                " + c.get(Calendar.MONTH) + "\n";
    output += "Year:                         " + c.get(Calendar.YEAR) + "\n";

    output += "Hour Of Day:                  " + c.get(Calendar.HOUR_OF_DAY) + "\n";
    output += "Minute:                       " + c.get(Calendar.MINUTE) + "\n";
    output += "Second:                       " + c.get(Calendar.SECOND) + "\n";

    return output;
}

From source file:com.autodomum.daylight.algorithm.DaylightAlgorithm.java

@Override
public Date sunrise(final Coordinate coordinate, final Date date) {
    final Calendar calendar = GregorianCalendar.getInstance();
    calendar.setTime(date);/*from w w  w  . j a v a  2s . c om*/

    final int day = calendar.get(Calendar.DAY_OF_YEAR);

    final double total = length(coordinate.getLatitude(), day);

    final int hours = (int) total;
    final int minutes = (int) ((((double) total) - ((double) hours)) * 60d);

    calendar.set(Calendar.HOUR_OF_DAY, 12);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);

    calendar.add(Calendar.HOUR_OF_DAY, -hours / 2);
    calendar.add(Calendar.MINUTE, -minutes / 2);
    calendar.add(Calendar.MINUTE, (int) localSolarTime(day));

    final TimeZone timeZone = TimeZone.getDefault();

    if (timeZone.inDaylightTime(date)) {
        calendar.add(Calendar.MILLISECOND, timeZone.getDSTSavings());
    }

    return calendar.getTime();
}

From source file:com.autodomum.daylight.algorithm.DaylightAlgorithm.java

@Override
public Date sunset(final Coordinate coordinate, final Date date) {
    final Calendar calendar = GregorianCalendar.getInstance();
    calendar.setTime(date);//w ww  . ja  v  a 2 s .  c o  m

    final int day = calendar.get(Calendar.DAY_OF_YEAR);

    final double total = length(coordinate.getLatitude(), day);

    final int hours = (int) total;
    final int minutes = (int) ((((double) total) - ((double) hours)) * 60d);

    calendar.set(Calendar.HOUR_OF_DAY, 12);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);

    calendar.add(Calendar.HOUR_OF_DAY, hours / 2);
    calendar.add(Calendar.MINUTE, minutes / 2);
    calendar.add(Calendar.MINUTE, (int) localSolarTime(day));

    final TimeZone timeZone = TimeZone.getDefault();

    if (timeZone.inDaylightTime(date)) {
        calendar.add(Calendar.MILLISECOND, timeZone.getDSTSavings());
    }

    return calendar.getTime();
}

From source file:com.haulmont.cuba.core.global.TimeZones.java

/**
 * @return short string representation of the given time zone
 *///from ww w.j a v a 2s  . com
public String getDisplayNameShort(@Nullable TimeZone timeZone) {
    if (timeZone == null)
        return "";

    boolean dst = timeZone.inDaylightTime(timeSource.currentTimestamp());
    String name = timeZone.getDisplayName(dst, TimeZone.SHORT);

    if (AD_HOC_TZ_PATTERN.matcher(name).matches())
        return name;
    else
        return name + " (" + getDisplayOffset(timeZone) + ")";
}

From source file:com.synelixis.xifi.AuthWebClient.AToken.java

private Date getGMTime() {
    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  ww  . j a v  a  2s.c  om*/
    int offsetHrs = offset / 1000 / 60 / 60;
    int offsetMins = offset / 1000 / 60 % 60;
    c.add(Calendar.HOUR_OF_DAY, (-offsetHrs));
    c.add(Calendar.MINUTE, (-offsetMins));
    return c.getTime();

}