Java Code Examples for java.util.GregorianCalendar#compareTo()

The following examples show how to use java.util.GregorianCalendar#compareTo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DoNotDisturbTile.java    From GravityBox with Apache License 2.0 6 votes vote down vote up
private Uri getTimeUntilNextAlarmCondition() {
    try {
        GregorianCalendar weekRange = new GregorianCalendar();
        final long now = weekRange.getTimeInMillis();
        setToMidnight(weekRange);
        weekRange.roll(Calendar.DATE, 6);
        final long nextAlarmMs = (long) XposedHelpers.callMethod(mZenCtrl, "getNextAlarm");
        if (nextAlarmMs > 0) {
            GregorianCalendar nextAlarm = new GregorianCalendar();
            nextAlarm.setTimeInMillis(nextAlarmMs);
            setToMidnight(nextAlarm);
            if (weekRange.compareTo(nextAlarm) >= 0) {
                Object condition = XposedHelpers.callStaticMethod(getZenModeConfigClass(), "toNextAlarmCondition",
                        mContext, now, nextAlarmMs, SysUiManagers.KeyguardMonitor.getCurrentUserId());
                return (Uri) XposedHelpers.getObjectField(condition, "id");
            }
        }
        return null;
    } catch (Throwable t) {
        XposedBridge.log(t);
        return null;
    }
}
 
Example 2
Source File: JsonDateReaderTest.java    From dropbox-sdk-java with MIT License 6 votes vote down vote up
@Test
public void parseDropboxDateTestMany()
{
    GregorianCalendar current = new GregorianCalendar(1970, GregorianCalendar.JANUARY, 1, 0, 0, 0);
    current.setTimeZone(JsonDateReader.UTC);

    GregorianCalendar end = new GregorianCalendar(3000, GregorianCalendar.JANUARY, 1, 0, 0, 0);
    end.setTimeZone(JsonDateReader.UTC);

    int count = 0;
    while (current.compareTo(end) < 0) {
        count++;
        validateDropboxDateParser(dateFormatHolder.get().format(current.getTime()));
        current.add(GregorianCalendar.DAY_OF_MONTH, 1);
        validateDropboxDateParser(dateFormatHolder.get().format(current.getTime()));
        current.add(GregorianCalendar.HOUR, 1);
        validateDropboxDateParser(dateFormatHolder.get().format(current.getTime()));
        current.add(GregorianCalendar.MINUTE, 1);
        validateDropboxDateParser(dateFormatHolder.get().format(current.getTime()));
        current.add(GregorianCalendar.SECOND, 1);
    }

    if (count < 1000) throw new AssertionError("Loop didn't run enough: " + count);
}
 
Example 3
Source File: TimeScales.java    From diirt with MIT License 6 votes vote down vote up
/**
 * Determines the time(s) that will be represented by reference lines on
 * a time graph.
 *
 * @param timeInterval the interval of time spanning the duration of the
 * time graph
 * @param period the interval of time between each reference line
 * @return a list of times evenly spaced by the duration of <code>period</code>
 * and encompassing the duration of <code>timeInterval</code>
 */
static List<Instant> createReferences(TimeInterval timeInterval, TimePeriod period) {
    Date start = Date.from(timeInterval.getStart());
    Date end = Date.from(timeInterval.getEnd());
    GregorianCalendar endCal = new GregorianCalendar();
    endCal.setTime(end);
    GregorianCalendar cal = new GregorianCalendar();
    cal.setTime(start);
    round(cal, period.fieldId);
    cal.set(period.fieldId, (cal.get(period.fieldId) / (int) period.amount) * (int) period.amount);
    List<Instant> references = new ArrayList<>();
    while (endCal.compareTo(cal) >= 0) {
        Instant newTime = cal.getTime().toInstant();
        if (timeInterval.contains(newTime)) {
            references.add(newTime);
        }
        cal.add(period.fieldId, (int) period.amount);
    }
    return references;
}
 
Example 4
Source File: JobSetupUtil.java    From datawave with Apache License 2.0 4 votes vote down vote up
/**
 * Computes a set of ranges to scan over entries whose row is a shaded day (yyyyMMdd_shardNum), one range per day. We calculate a range per day so that we
 * can assume that all entries for a particular day go to the same mapper in a MapReduce job.
 *
 * This uses the supplied start and end date parameters that can be supplied at job start time, and if none are supplied, it uses the current day. The end
 * of the range is always set exclusively to the start of the day following the end of the supplied day (or the beginning of tomorrow if no end was
 * supplied).
 */
public static Collection<Range> computeShardedDayRange(Configuration conf, Logger log) {
    String start = conf.get(MetricsConfig.START);
    String end = conf.get(MetricsConfig.END);
    
    GregorianCalendar from = new GregorianCalendar();
    if (start != null)
        from.setTime(DateConverter.convert(start));
    from.set(Calendar.HOUR_OF_DAY, 0);
    from.set(Calendar.MINUTE, 0);
    from.set(Calendar.SECOND, 0);
    from.set(Calendar.MILLISECOND, 0);
    if (log.isDebugEnabled() && start == null)
        log.debug("Defaulting start to the beginning of today: " + from);
    
    GregorianCalendar until = new GregorianCalendar();
    if (end != null)
        until.setTimeInMillis(DateConverter.convert(end).getTime() + TimeUnit.DAYS.toMillis(1));
    until.set(Calendar.HOUR_OF_DAY, 0);
    until.set(Calendar.MINUTE, 0);
    until.set(Calendar.SECOND, 0);
    until.set(Calendar.MILLISECOND, 0);
    until.add(Calendar.DAY_OF_YEAR, 1);
    if (log.isDebugEnabled() && end == null)
        log.debug("Defaulting end to the beginning of tomorrow: " + until);
    
    if (until.compareTo(from) <= 0) {
        log.error("Warning: end date (" + until + ") is after begin date (" + from + "), swapping!");
        GregorianCalendar tmp = until;
        until = from;
        from = tmp;
    }
    
    ArrayList<Range> ranges = new ArrayList<>();
    while (from.compareTo(until) < 0) {
        String rangeStart = DateHelper.format(from.getTime());
        from.add(GregorianCalendar.DAY_OF_YEAR, 1);
        String rangeEnd = DateHelper.format(from.getTime());
        ranges.add(new Range(rangeStart, true, rangeEnd, false));
    }
    
    return ranges;
}
 
Example 5
Source File: UniversityDateFiscalYearMakerImpl.java    From kfs with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * @see org.kuali.kfs.coa.batch.dataaccess.impl.FiscalYearMakerHelperImpl#performCustomProcessing(java.lang.Integer)
 */
@Override
public void performCustomProcessing(Integer baseFiscalYear, boolean firstCopyYear) {
    int fiscalYearStartMonth = getFiscalYearStartMonth(baseFiscalYear);

    // determine start date year, if start month is not January the year will be one behind the fiscal year
    int startDateYear = baseFiscalYear;
    if (Calendar.JANUARY == fiscalYearStartMonth) {
        startDateYear += 1;
    }
    getPersistenceBrokerTemplate();
    // start with first day of fiscal year and create records for each year up to end date
    GregorianCalendar univPeriodDate = new GregorianCalendar(startDateYear, fiscalYearStartMonth, 1);

    // setup end date
    GregorianCalendar enddate = new GregorianCalendar(univPeriodDate.get(Calendar.YEAR), univPeriodDate.get(Calendar.MONTH), univPeriodDate.get(Calendar.DAY_OF_MONTH));
    enddate.add(Calendar.MONTH, 12);
    enddate.add(Calendar.DAY_OF_MONTH, -1);

    // the fiscal year is always the year of the ending date of the fiscal year
    Integer nextFiscalYear = enddate.get(Calendar.YEAR);

    // get rid of any records already existing for next fiscal year
    deleteNewYearRows(nextFiscalYear);

    // initialize the period variables
    int period = 1;
    String periodString = String.format("%02d", period);
    int compareMonth = univPeriodDate.get(Calendar.MONTH);
    int currentMonth = compareMonth;

    // loop through the dates until we are past end date
    while (univPeriodDate.compareTo(enddate) <= 0) {
        // if we hit period 13 something went wrong
        if (period == 13) {
            LOG.error("Hit period 13 while creating university date records");
            throw new RuntimeException("Hit period 13 while creating university date records");
        }
        
        // create the university date record
        UniversityDate universityDate = new UniversityDate();
        universityDate.setUniversityFiscalYear(nextFiscalYear);
        universityDate.setUniversityDate(new Date(univPeriodDate.getTimeInMillis()));
        universityDate.setUniversityFiscalAccountingPeriod(periodString);

        businessObjectService.save(universityDate);

        // add one to day for the next record
        univPeriodDate.add(Calendar.DAY_OF_MONTH, 1);

        // does this kick us into a new month and therefore a new accounting period?
        compareMonth = univPeriodDate.get(Calendar.MONTH);
        if (currentMonth != compareMonth) {
            period = period + 1;
            periodString = String.format("%02d", period);
            currentMonth = compareMonth;
        }
    }
}
 
Example 6
Source File: GregorianCalendarExample.java    From tutorials with MIT License 4 votes vote down vote up
public int compareDates(GregorianCalendar firstDate, GregorianCalendar secondDate) {
    return firstDate.compareTo(secondDate);
}