Example usage for java.util Calendar roll

List of usage examples for java.util Calendar roll

Introduction

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

Prototype

public void roll(int field, int amount) 

Source Link

Document

Adds the specified (signed) amount to the specified calendar field without changing larger fields.

Usage

From source file:fr.inria.ucn.Helpers.java

/**
 * //from  w  w w  . j  ava  2  s .  c  o m
 * @param c
 * @return -1 if not at night-time (or feature disabled), else milliseconds until morning.
 */
public static long getNightEnd(Context c) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c);
    if (!prefs.getBoolean(Constants.PREF_STOP_NIGHT, false))
        return -1;

    int nstart = prefs.getInt(Constants.PREF_NIGHT_START, 23 * 3600);
    int nstop = prefs.getInt(Constants.PREF_NIGHT_STOP, 6 * 3600);

    Calendar nightstart = Calendar.getInstance();
    nightstart.roll(Calendar.HOUR_OF_DAY, -1 * nightstart.get(Calendar.HOUR_OF_DAY));
    nightstart.roll(Calendar.MINUTE, -1 * nightstart.get(Calendar.MINUTE));
    nightstart.roll(Calendar.SECOND, -1 * nightstart.get(Calendar.SECOND));
    nightstart.roll(Calendar.MILLISECOND, -1 * nightstart.get(Calendar.MILLISECOND));
    nightstart.add(Calendar.SECOND, nstart);

    Calendar nightstop = Calendar.getInstance();
    nightstop.roll(Calendar.HOUR_OF_DAY, -1 * nightstop.get(Calendar.HOUR_OF_DAY));
    nightstop.roll(Calendar.MINUTE, -1 * nightstop.get(Calendar.MINUTE));
    nightstop.roll(Calendar.SECOND, -1 * nightstop.get(Calendar.SECOND));
    nightstop.roll(Calendar.MILLISECOND, -1 * nightstop.get(Calendar.MILLISECOND));
    nightstop.add(Calendar.SECOND, nstop);
    if (nightstop.before(nightstart))
        nightstop.add(Calendar.HOUR, 24);

    Log.d(Constants.LOGTAG, "nightstart " + nstart + " -> " + nightstart.toString());
    Log.d(Constants.LOGTAG, "nightstop " + nstop + " -> " + nightstop.toString());

    Calendar now = Calendar.getInstance();
    if (now.after(nightstart) && now.before(nightstop)) {
        return nightstop.getTimeInMillis();
    } else {
        return -1;
    }
}

From source file:com.marand.thinkmed.medications.dto.report.TherapyDayReportUtils.java

public static String getTherapyApplicationColumnLabel(@Nonnull final Date startDate, final int column) {
    final Calendar calendar = Calendar.getInstance();
    calendar.setTime(startDate);//from  w  w  w .  j a v a  2  s.  c  om
    calendar.roll(Calendar.DAY_OF_MONTH, column);

    return calendar.get(Calendar.DAY_OF_MONTH) + ".";
}

From source file:org.mifos.framework.util.helpers.DateUtils.java

public static Calendar getFistDayOfNextYear(Calendar cal) {
    cal.roll(Calendar.YEAR, 1);
    cal.set(Calendar.MONTH, Calendar.JANUARY);
    cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DATE));
    Calendar cal1 = new DateTimeService().getCurrentDateTime().toGregorianCalendar();
    cal1.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE), 0, 0, 0);
    return cal1;//from  ww  w.  j  a  v  a  2s.  c o  m
}

From source file:cn.mypandora.util.MyDateUtils.java

/**
 * ?/*  w w w  .  j a  va 2 s . c om*/
 *
 * @return
 */
public static String getNextMonthEnd() {
    Calendar cal = Calendar.getInstance();
    // cal.set(Calendar.DATE, +1);
    // cal.add(Calendar.MONTH, +2);
    // cal.add(Calendar.DATE, -1);
    // return DateFormatUtils.format(cal, DATE_FORMAT);

    // 
    cal.add(Calendar.MONTH, 1);// 
    cal.set(Calendar.DATE, 1);// 
    cal.roll(Calendar.DATE, -1);// ?
    return DateFormatUtils.format(cal, DATE_FORMAT);
}

From source file:ac.elements.parser.ExtendedFunctions.java

/**
 * Formats a number given as the number of milliseconds String or Long and
 * converts it to a format "H'h' m'm' s's'". Periods longer then a day do
 * not work in this method. If conversion doesn't work it returns the Object
 * casted as a string. (SimpleDateFormat syntax).
 * //from www .j  a v  a  2s .  com
 * @returns null if myObject is null, otherwise a formatted time period as a
 *          string.
 * @see java.text.SimpleDateFormat
 */
public static String timePeriod(Object myObject) {

    long timePeriodInMilliseceonds = 0;

    if (myObject == null)
        return null;

    try {

        if (myObject instanceof String) {
            timePeriodInMilliseceonds = Long.parseLong(myObject.toString());
        } else if (myObject instanceof Number) {
            timePeriodInMilliseceonds = ((Number) myObject).longValue();
        } else {
            return "Not supported: ".concat(myObject.getClass().getName());
        }

        String format = "D' days' H'h' mm'm' ss's'";
        if (timePeriodInMilliseceonds < 60 * 1000l)
            format = "ss's'";
        else if (timePeriodInMilliseceonds < 3600 * 1000l)
            format = "mm'm' ss's'";
        else if (timePeriodInMilliseceonds < 3600 * 24 * 1000l)
            format = "H'h' mm'm' ss's'";
        else if (timePeriodInMilliseceonds < 3600 * 48 * 1000l) {
            format = "D' day' H'h' mm'm' ss's'";
        }

        // Get Greenwich time zone.
        TimeZone theTz = TimeZone.getTimeZone("GMT");

        SimpleDateFormat mySimpleDateFormat = new SimpleDateFormat(format);
        mySimpleDateFormat.setTimeZone(theTz);

        // create a date in the locale's calendar,
        // set its timezone and hour.
        Calendar day = Calendar.getInstance();
        day.setTimeZone(theTz);
        day.setTime(new Date(timePeriodInMilliseceonds));
        day.roll(Calendar.DAY_OF_MONTH, false);
        day.set(Calendar.HOUR, day.get(Calendar.HOUR));

        return mySimpleDateFormat.format(day.getTime());

    } catch (NullPointerException npe) {
        // npe.printStackTrace();
        return null;
    } catch (NumberFormatException nfe) {
        // nfe.printStackTrace();
        return null;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

}

From source file:fr.univrouen.poste.domain.AppliConfig.java

public static Date getCacheDateEndCandidat() {
    if (cacheDateEndCandidat == null) {
        List<AppliConfig> configs = AppliConfig.findAllAppliConfigs();
        if (!configs.isEmpty()) {
            cacheDateEndCandidat = configs.get(0).getDateEndCandidat();
        }/*from w w w  .j a  v a 2 s.c  om*/
        if (cacheDateEndCandidat == null) {
            // initialize to this currentTime + 5 years
            Calendar c = Calendar.getInstance();
            c.roll(Calendar.YEAR, 5);
            cacheDateEndCandidat = c.getTime();
        }
    }
    return cacheDateEndCandidat;
}

From source file:fr.univrouen.poste.domain.AppliConfig.java

public static Date getCacheDateEndCandidatActif() {
    if (cacheDateEndCandidatActif == null) {
        List<AppliConfig> configs = AppliConfig.findAllAppliConfigs();
        if (!configs.isEmpty()) {
            cacheDateEndCandidatActif = configs.get(0).getDateEndCandidatActif();
        }//from  w  w  w  .  jav a 2 s.  c o  m
        if (cacheDateEndCandidatActif == null) {
            // initialize to this currentTime + 5 years
            Calendar c = Calendar.getInstance();
            c.roll(Calendar.YEAR, 5);
            cacheDateEndCandidatActif = c.getTime();
        }
    }
    return cacheDateEndCandidatActif;
}

From source file:fr.univrouen.poste.domain.AppliConfig.java

public static Date getCacheDateEndMembre() {
    if (cacheDateEndMembre == null) {
        List<AppliConfig> configs = AppliConfig.findAllAppliConfigs();
        if (!configs.isEmpty()) {
            cacheDateEndMembre = configs.get(0).getDateEndMembre();
        }/*from   w ww . jav  a2s.  com*/
        if (cacheDateEndMembre == null) {
            // initialize to this currentTime + 5 years
            Calendar c = Calendar.getInstance();
            c.roll(Calendar.YEAR, 5);
            cacheDateEndMembre = c.getTime();
        }
    }
    return cacheDateEndMembre;
}

From source file:com.siriusit.spezg.multilib.service.spring.SpringLoanService.java

@Override
@Transactional//w w  w  .ja  va2s .c om
public Loan storeLoan(Unit unit, Person person) {
    Date dateOut = new Date();
    Calendar deadline = Calendar.getInstance();
    deadline.roll(Calendar.MONTH, 1);
    return loanRepository.createLoan(person, unit, dateOut, null, deadline.getTime());
}

From source file:org.sipfoundry.sipxconfig.phone.polycom.MigrationTask.java

@Override
public boolean setup(SetupManager manager) {
    if (manager.isFalse(MIGRATION_FLAG)) {
        LOG.info("Starting migrating Polycom phones.");
        for (Phone phone : m_phoneContext.loadPhones()) {
            if (phone instanceof PolycomPhone) {
                Calendar c = Calendar.getInstance();
                c.roll(Calendar.MINUTE, 1);
                m_profileManager.generateProfile(phone.getId(), true, c.getTime());
                LOG.info(//from  ww w.j  a v a 2  s  .  co m
                        String.format("Generated profiles for %s which will be rebooted in 1 minute from  now.",
                                phone.getSerialNumber()));
            }
        }
    }
    manager.setTrue(MIGRATION_FLAG);
    return true;
}