Example usage for java.util GregorianCalendar GregorianCalendar

List of usage examples for java.util GregorianCalendar GregorianCalendar

Introduction

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

Prototype

public GregorianCalendar() 

Source Link

Document

Constructs a default GregorianCalendar using the current time in the default time zone with the default Locale.Category#FORMAT FORMAT locale.

Usage

From source file:Main.java

/**
 * Given a calendar (possibly containing only a day of the year), returns the earliest possible
 * anniversary of the date that is equal to or after the current point in time if the date
 * does not contain a year, or the date converted to the local time zone (if the date contains
 * a year./*  w w  w.j  a va2 s . co m*/
 *
 * @param target The date we wish to convert(in the UTC time zone).
 * @return If date does not contain a year (year < 1900), returns the next earliest anniversary
 * that is after the current point in time (in the local time zone). Otherwise, returns the
 * adjusted Date in the local time zone.
 */
public static Date getNextAnnualDate(Calendar target) {
    final Calendar today = Calendar.getInstance();
    today.setTime(new Date());

    // Round the current time to the exact start of today so that when we compare
    // today against the target date, both dates are set to exactly 0000H.
    today.set(Calendar.HOUR_OF_DAY, 0);
    today.set(Calendar.MINUTE, 0);
    today.set(Calendar.SECOND, 0);
    today.set(Calendar.MILLISECOND, 0);

    final boolean isYearSet = isYearSet(target);
    final int targetYear = target.get(Calendar.YEAR);
    final int targetMonth = target.get(Calendar.MONTH);
    final int targetDay = target.get(Calendar.DAY_OF_MONTH);
    final boolean isFeb29 = (targetMonth == Calendar.FEBRUARY && targetDay == 29);
    final GregorianCalendar anniversary = new GregorianCalendar();
    // Convert from the UTC date to the local date. Set the year to today's year if the
    // there is no provided year (targetYear < 1900)
    anniversary.set(!isYearSet ? today.get(Calendar.YEAR) : targetYear, targetMonth, targetDay);
    // If the anniversary's date is before the start of today and there is no year set,
    // increment the year by 1 so that the returned date is always equal to or greater than
    // today. If the day is a leap year, keep going until we get the next leap year anniversary
    // Otherwise if there is already a year set, simply return the exact date.
    if (!isYearSet) {
        int anniversaryYear = today.get(Calendar.YEAR);
        if (anniversary.before(today) || (isFeb29 && !anniversary.isLeapYear(anniversaryYear))) {
            // If the target date is not Feb 29, then set the anniversary to the next year.
            // Otherwise, keep going until we find the next leap year (this is not guaranteed
            // to be in 4 years time).
            do {
                anniversaryYear += 1;
            } while (isFeb29 && !anniversary.isLeapYear(anniversaryYear));
            anniversary.set(anniversaryYear, targetMonth, targetDay);
        }
    }
    return anniversary.getTime();
}

From source file:edu.stanford.muse.email.CalendarUtil.java

public static String getDisplayMonth(Date d) {
    Calendar c = new GregorianCalendar();
    c.setTime(d);
    return getDisplayMonth(c);
}

From source file:com.esofthead.mycollab.core.utils.DateTimeUtils.java

public static Date getCurrentDateWithoutMS() {
    GregorianCalendar calendar = new GregorianCalendar();
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MINUTE, 0);
    return calendar.getTime();
}

From source file:com.mycompany.app.Section.java

public Section() {
    studentsInClass = 0;
    students = 0;
    location = null;
    startTime = endTime = new GregorianCalendar();
}

From source file:com.google.code.rptm.mailarchive.YearMonth.java

/**
 * Construct a new instance with the year and month given by the current data.
 *///from  w ww  .ja v a 2s  .c o  m
public YearMonth() {
    GregorianCalendar cal = new GregorianCalendar();
    year = cal.get(Calendar.YEAR);
    month = cal.get(Calendar.MONTH) + 1;
}

From source file:Main.java

public static XMLGregorianCalendar getXMLDate(final Calendar calendar) {
    GregorianCalendar c;/*ww w .  ja va2 s  .co m*/
    if (calendar instanceof GregorianCalendar) {
        c = (GregorianCalendar) calendar;
    } else {
        c = new GregorianCalendar();
        c.setTimeZone(UTC);
        c.setTime(calendar.getTime());
    }

    try {
        XMLGregorianCalendar ret = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);
        ret.setMillisecond(DatatypeConstants.FIELD_UNDEFINED);
        return ret;
    } catch (DatatypeConfigurationException e) {
        return null;
    }
}

From source file:com.aw.core.format.FechaEnTextoFormatter.java

public Object format(Object attributeValue) {
    Date date = (Date) attributeValue;
    Calendar cal = new GregorianCalendar();
    cal.setTime(date);//from   www .j  a  va2s .  c o m
    int anio = cal.get(Calendar.YEAR);
    int mes = cal.get(Calendar.MONTH);
    int dia = cal.get(Calendar.DAY_OF_MONTH);
    Map meses = buildMesesMap();
    String valor = dia + " de " + meses.get(mes) + " del " + anio;
    return valor;
}

From source file:org.hoteia.qalingo.app.business.job.email.EmailCleanerTasklet.java

public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
    GregorianCalendar calendar = new GregorianCalendar();
    int day = calendar.get(GregorianCalendar.DAY_OF_YEAR);

    // TODO : Denis : 20130924 : add number of day configuration in database

    calendar.set(GregorianCalendar.DAY_OF_YEAR, day - 7);
    int row = emailService.deleteSendedEmail(new Timestamp(calendar.getTimeInMillis()));
    logger.debug(row + " emails deleted");
    return RepeatStatus.FINISHED;
}

From source file:org.hoteia.qalingo.app.business.job.status.ServerStatusCleanerTasklet.java

public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
    GregorianCalendar calendar = new GregorianCalendar();
    int day = calendar.get(GregorianCalendar.DAY_OF_YEAR);

    // TODO : Denis : 20131209 : add number of day configuration in database ?

    calendar.set(GregorianCalendar.DAY_OF_YEAR, day - 7);
    int row = serverService.deleteSendedServerStatus(new Timestamp(calendar.getTimeInMillis()));
    logger.debug(row + " server status deleted");
    return RepeatStatus.FINISHED;
}

From source file:com.github.dozermapper.core.converters.CalendarConverter.java

public Object convert(Class destClass, Object srcObj) {
    Calendar result = new GregorianCalendar();
    Class srcFieldClass = srcObj.getClass();
    // Convert from Date to Calendar
    if (java.util.Date.class.isAssignableFrom(srcFieldClass)) {
        result.setTime((java.util.Date) srcObj);
    } else if (Calendar.class.isAssignableFrom(srcFieldClass)) {
        //  Convert from Calendar to Calendar
        Calendar c = (Calendar) srcObj;
        result.setTime(c.getTime());//from   ww  w  .  java  2  s.  c  o m
        result.setTimeZone(c.getTimeZone());
    } else if (XMLGregorianCalendar.class.isAssignableFrom(srcFieldClass)) {
        Calendar c = ((XMLGregorianCalendar) srcObj).toGregorianCalendar();
        result.setTime(c.getTime());
        result.setTimeZone(c.getTimeZone());
    } else if (dateFormat != null && String.class.isAssignableFrom(srcFieldClass)) {
        // String to Calendar
        try {
            result.setTime(new Date(dateFormat.parse((String) srcObj).getTime()));
        } catch (ParseException e) {
            throw new ConversionException("Unable to parse source object using specified date format", e);
        }
    } else {
        // Default conversion
        try {
            result.setTime(new Date(Long.parseLong(srcObj.toString())));
        } catch (NumberFormatException e) {
            throw new ConversionException("Unable to determine time in millis of source object", e);
        }
    }

    // Calendar to String
    if (dateFormat != null && String.class.isAssignableFrom(destClass)) {
        return dateFormat.format(result.getTime());
    }

    return result;
}