Example usage for java.util Calendar getTimeZone

List of usage examples for java.util Calendar getTimeZone

Introduction

In this page you can find the example usage for java.util Calendar getTimeZone.

Prototype

public TimeZone getTimeZone() 

Source Link

Document

Gets the time zone.

Usage

From source file:models.Schedule.java

/**
 * @return next java timestamp when this may be run,
 * or 0 if it is not runnable in the next three months
 **///from ww w. j ava2  s  .c  o  m
public long calculateNextRunnableTime(Calendar calendar) {

    if (isRunnable(calendar)) {
        return System.currentTimeMillis();
    } else {
        Calendar calendarCopy = Calendar.getInstance(calendar.getTimeZone());
        calendarCopy.setTimeInMillis(calendar.getTimeInMillis());
        Calendar calMaxInFutureToCalculate = Calendar.getInstance();
        calMaxInFutureToCalculate.add(Calendar.MONTH, 3);
        long maxInFutureToCalculateMillis = calMaxInFutureToCalculate.getTimeInMillis();

        while (!isRunnable(calendarCopy)) {
            calendarCopy.add(Calendar.MINUTE, 1);
            if (calendarCopy.getTimeInMillis() > maxInFutureToCalculateMillis) {
                return 0; //1970
            }
        }

        return calendarCopy.getTimeInMillis();
    }

}

From source file:models.Schedule.java

/**
 * if calendar is runnable, check last run. if last run is runnable,
 * there is a possible dupe.  if all non-default runnable units are ==,
 * then this is a dupe/*from w  w w  .  j  av a  2 s  .c  o m*/
 *
 * false if last run cal == cal
 *
 * @return true if the schedule should run (considering the last run time)
 **/
public boolean isRunnable(Calendar lastRunCalendar, Calendar calendar) {
    if (!isActive()) {
        // don't run inactive schedules
        return false;
    }

    if (lastRunCalendar != null && !lastRunCalendar.getTimeZone().equals(calendar.getTimeZone())) {
        throw new IllegalArgumentException(
                "Calendar time zones must be the same for comparison lastRunCalendar=" + lastRunCalendar
                        + ",calendar=" + calendar);
    }

    if (lastRunCalendar != null && lastRunCalendar.equals(calendar)) {
        //same as last run time, so don't run again
        logger.log(Level.FINE, "lastRunCalendar.equals( calendar ), returning false");
        return false;
    }

    boolean calendarIsRunnable = isRunnable(calendar);
    if (!calendarIsRunnable) {
        // no need to check lastRun time, already not elible to run
        logger.log(Level.FINE, "!calendarIsRunnable, returning false");
        return false;
    }

    if (lastRunCalendar == null) {
        logger.log(Level.FINE, "lastRunCalendar is null, returning calendarIsRunnable={0}", calendarIsRunnable);
        return calendarIsRunnable;
    }

    // calendar is runnable, so see if last calendar applies
    boolean lastRunCalendarIsRunnable = isRunnable(lastRunCalendar);
    if (!lastRunCalendarIsRunnable) {
        // lastRunCalendar does not apply, ignore it
        logger.log(Level.FINE, "!lastRunCalendarIsRunnable, returning calendarIsRunnable={0}",
                calendarIsRunnable);
        return calendarIsRunnable;
    }

    // both calendars are runnable, so this could be a dupe run
    // if all non-default runnable units are ==, then this is a dupe run

    // 1 identify non-default
    List<Integer> nonDefaultUnits = identifyNonDefaultUnits();
    if (nonDefaultUnits.size() == 0) {
        // schedule is wide open, and calendars are not equal, so just use calendar
        logger.log(Level.FINE, "schedule is wide open, calendars not equal, returning calendarIsRunnable={0}",
                calendarIsRunnable);
        return calendarIsRunnable;
    }

    // 2 if something is set, and all set units are equal, then it's a dupe
    //   if they are not all equal, it's runnable
    calendarIsRunnable = !areAllSetRangesUnitsEqual(lastRunCalendar, calendar, nonDefaultUnits);
    logger.log(Level.FINE, "checking for dupe units, returning calendarIsRunnable={0}", calendarIsRunnable);

    return calendarIsRunnable;
}

From source file:org.candlepin.util.UtilTest.java

@Test
public void roundToMidnight() {
    Date now = new Date();
    Date midnight = Util.roundToMidnight(now);
    assertFalse(now.equals(midnight));//from w w w . jav  a 2 s . com

    Calendar cal = Calendar.getInstance();
    cal.setTime(midnight);
    assertEquals(23, cal.get(Calendar.HOUR_OF_DAY));
    assertEquals(59, cal.get(Calendar.MINUTE));
    assertEquals(59, cal.get(Calendar.SECOND));
    assertEquals(TimeZone.getDefault(), cal.getTimeZone());

    Date stillmidnight = Util.roundToMidnight(midnight);
    cal.setTime(stillmidnight);
    assertEquals(23, cal.get(Calendar.HOUR_OF_DAY));
    assertEquals(59, cal.get(Calendar.MINUTE));
    assertEquals(59, cal.get(Calendar.SECOND));
    assertEquals(TimeZone.getDefault(), cal.getTimeZone());
}

From source file:org.apache.tika.parser.pdf18.PDF2XHTML.java

private void handleSignature(AttributesImpl parentAttributes, PDSignatureField sigField,
        XHTMLContentHandler handler) throws SAXException {

    PDSignature sig = sigField.getSignature();
    if (sig == null) {
        return;//from   ww  w. j  a  v  a 2  s. com
    }
    Map<String, String> vals = new TreeMap<String, String>();
    vals.put("name", sig.getName());
    vals.put("contactInfo", sig.getContactInfo());
    vals.put("location", sig.getLocation());
    vals.put("reason", sig.getReason());

    Calendar cal = sig.getSignDate();
    if (cal != null) {
        dateFormat.setTimeZone(cal.getTimeZone());
        vals.put("date", dateFormat.format(cal.getTime()));
    }
    //see if there is any data
    int nonNull = 0;
    for (String val : vals.keySet()) {
        if (val != null && !val.equals("")) {
            nonNull++;
        }
    }
    //if there is, process it
    if (nonNull > 0) {
        handler.startElement("li", parentAttributes);

        AttributesImpl attrs = new AttributesImpl();
        attrs.addAttribute("", "type", "type", "CDATA", "signaturedata");

        handler.startElement("ol", attrs);
        for (Map.Entry<String, String> e : vals.entrySet()) {
            if (e.getValue() == null || e.getValue().equals("")) {
                continue;
            }
            attrs = new AttributesImpl();
            attrs.addAttribute("", "signdata", "signdata", "CDATA", e.getKey());
            handler.startElement("li", attrs);
            handler.characters(e.getValue());
            handler.endElement("li");
        }
        handler.endElement("ol");
        handler.endElement("li");
    }
}

From source file:org.apache.tika.parser.pdf.PDF2XHTML.java

private void handleSignature(AttributesImpl parentAttributes, PDSignatureField sigField,
        XHTMLContentHandler handler) throws SAXException {

    PDSignature sig = sigField.getSignature();
    if (sig == null) {
        return;/*from  ww w  .j  av  a2s.c  om*/
    }
    Map<String, String> vals = new TreeMap<>();
    vals.put("name", sig.getName());
    vals.put("contactInfo", sig.getContactInfo());
    vals.put("location", sig.getLocation());
    vals.put("reason", sig.getReason());

    Calendar cal = sig.getSignDate();
    if (cal != null) {
        dateFormat.setTimeZone(cal.getTimeZone());
        vals.put("date", dateFormat.format(cal.getTime()));
    }
    //see if there is any data
    int nonNull = 0;
    for (String val : vals.keySet()) {
        if (val != null && !val.equals("")) {
            nonNull++;
        }
    }
    //if there is, process it
    if (nonNull > 0) {
        handler.startElement("li", parentAttributes);

        AttributesImpl attrs = new AttributesImpl();
        attrs.addAttribute("", "type", "type", "CDATA", "signaturedata");

        handler.startElement("ol", attrs);
        for (Map.Entry<String, String> e : vals.entrySet()) {
            if (e.getValue() == null || e.getValue().equals("")) {
                continue;
            }
            attrs = new AttributesImpl();
            attrs.addAttribute("", "signdata", "signdata", "CDATA", e.getKey());
            handler.startElement("li", attrs);
            handler.characters(e.getValue());
            handler.endElement("li");
        }
        handler.endElement("ol");
        handler.endElement("li");
    }
}

From source file:org.apache.tika.parser.pdf.AbstractPDF2XHTML.java

private void handleSignature(AttributesImpl parentAttributes, PDSignatureField sigField) throws SAXException {

    PDSignature sig = sigField.getSignature();
    if (sig == null) {
        return;/*from   w  w w  .  j  av  a  2 s. c  o m*/
    }
    Map<String, String> vals = new TreeMap<>();
    vals.put("name", sig.getName());
    vals.put("contactInfo", sig.getContactInfo());
    vals.put("location", sig.getLocation());
    vals.put("reason", sig.getReason());

    Calendar cal = sig.getSignDate();
    if (cal != null) {
        dateFormat.setTimeZone(cal.getTimeZone());
        vals.put("date", dateFormat.format(cal.getTime()));
    }
    //see if there is any data
    int nonNull = 0;
    for (String val : vals.keySet()) {
        if (val != null && !val.equals("")) {
            nonNull++;
        }
    }
    //if there is, process it
    if (nonNull > 0) {
        xhtml.startElement("li", parentAttributes);

        AttributesImpl attrs = new AttributesImpl();
        attrs.addAttribute("", "type", "type", "CDATA", "signaturedata");

        xhtml.startElement("ol", attrs);
        for (Map.Entry<String, String> e : vals.entrySet()) {
            if (e.getValue() == null || e.getValue().equals("")) {
                continue;
            }
            attrs = new AttributesImpl();
            attrs.addAttribute("", "signdata", "signdata", "CDATA", e.getKey());
            xhtml.startElement("li", attrs);
            xhtml.characters(e.getValue());
            xhtml.endElement("li");
        }
        xhtml.endElement("ol");
        xhtml.endElement("li");
    }
}

From source file:net.sourceforge.msscodefactory.cflib.v1_11.CFLib.CFLibXmlUtil.java

public static String formatTZTime(Calendar cal) {
    final String S_ProcName = "formatTZTime";
    if (cal == null) {
        throw CFLib.getDefaultExceptionFactory().newNullArgumentException(CFLibXmlUtil.class, S_ProcName, 1,
                "cal");
    }//  w w w  .j a  va2 s  .  c  o  m
    StringBuffer buff = new StringBuffer();
    Formatter fmt = new Formatter(buff);
    fmt.format("%1$02d", cal.get(Calendar.HOUR_OF_DAY));
    buff.append(':');
    fmt.format("%1$02d", cal.get(Calendar.MINUTE));
    buff.append(':');
    fmt.format("%1$02d", cal.get(Calendar.SECOND));
    int tzoff = cal.getTimeZone().getRawOffset() / 60000;
    if (tzoff < 0) {
        tzoff = 0 - tzoff;
        buff.append('-');
    } else {
        buff.append('+');
    }
    int tzhour = tzoff / 60;
    int tzmin = tzoff % 60;
    if (tzhour > 12) {
        fmt.close();
        throw CFLib.getDefaultExceptionFactory().newInvalidArgumentException(CFLibXmlUtil.class, S_ProcName, 0,
                "tzhour", tzhour);
    }
    fmt.format("%1$02d", tzhour);
    buff.append(':');
    fmt.format("%1$02d", tzmin);
    String retval = buff.toString();
    fmt.close();
    return (retval);
}

From source file:net.sourceforge.msscodefactory.cflib.v1_11.CFLib.CFLibXmlUtil.java

public static String formatTZDate(Calendar cal) {
    final String S_ProcName = "formatTZDate";
    if (cal == null) {
        throw CFLib.getDefaultExceptionFactory().newNullArgumentException(CFLibXmlUtil.class, S_ProcName, 1,
                "cal");
    }/*from   w w w  .  j av  a2s .c  o  m*/
    StringBuffer buff = new StringBuffer();
    Formatter fmt = new Formatter(buff);
    fmt.format("%1$04d", cal.get(Calendar.YEAR));
    buff.append('-');
    fmt.format("%1$02d", cal.get(Calendar.MONTH) + 1);
    buff.append('-');
    fmt.format("%1$02d", cal.get(Calendar.DAY_OF_MONTH));
    int tzoff = cal.getTimeZone().getRawOffset() / 60000;
    if (tzoff < 0) {
        tzoff = 0 - tzoff;
        buff.append('-');
    } else {
        buff.append('+');
    }
    int tzhour = tzoff / 60;
    int tzmin = tzoff % 60;
    if (tzhour > 12) {
        fmt.close();
        throw CFLib.getDefaultExceptionFactory().newInvalidArgumentException(CFLibXmlUtil.class, S_ProcName, 0,
                "tzhour", tzhour);
    }
    fmt.format("%1$02d", tzhour);
    buff.append(':');
    fmt.format("%1$02d", tzmin);
    String retval = buff.toString();
    fmt.close();
    return (retval);
}

From source file:net.sourceforge.msscodefactory.cfasterisk.v2_1.CFAstSybase.CFAstSybaseSchema.java

public static String getTZDateString(Calendar val) {
    if (val == null) {
        return ("null");
    } else {//from w w w . j ava 2  s .  com
        Calendar cal;
        if (val.getTimeZone().equals(getServerTimeZone())) {
            cal = val;
        } else {
            cal = new GregorianCalendar(getServerTimeZone());
            cal.setTimeInMillis(val.getTimeInMillis());
        }
        StringBuffer buff = new StringBuffer();
        Formatter fmt = new Formatter(buff);
        fmt.format("%1$04d", cal.get(Calendar.YEAR));
        buff.append("-");
        fmt.format("%1$02d", cal.get(Calendar.MONTH) + 1);
        buff.append("-");
        fmt.format("%1$02d", cal.get(Calendar.DAY_OF_MONTH));
        buff.append("T");
        fmt.format("%1$02d", cal.get(Calendar.HOUR_OF_DAY));
        buff.append(":");
        fmt.format("%1$02d", cal.get(Calendar.MINUTE));
        buff.append(":");
        fmt.format("%1$02d", cal.get(Calendar.SECOND));
        fmt.close();
        return (buff.toString());
    }
}