Example usage for java.util Calendar clone

List of usage examples for java.util Calendar clone

Introduction

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

Prototype

@Override
public Object clone() 

Source Link

Document

Creates and returns a copy of this object.

Usage

From source file:Main.java

/**
 * <p>Round this date, leaving the field specified as the most
 * significant field.</p>/*ww w .  j  a  v  a  2  s  .co m*/
 *
 * <p>For example, if you had the datetime of 28 Mar 2002
 * 13:45:01.231, if this was passed with HOUR, it would return
 * 28 Mar 2002 14:00:00.000. If this was passed with MONTH, it
 * would return 1 April 2002 0:00:00.000.</p>
 * 
 * <p>For a date in a timezone that handles the change to daylight
 * saving time, rounding to Calendar.HOUR_OF_DAY will behave as follows.
 * Suppose daylight saving time begins at 02:00 on March 30. Rounding a 
 * date that crosses this time would produce the following values:
 * <ul>
 * <li>March 30, 2003 01:10 rounds to March 30, 2003 01:00</li>
 * <li>March 30, 2003 01:40 rounds to March 30, 2003 03:00</li>
 * <li>March 30, 2003 02:10 rounds to March 30, 2003 03:00</li>
 * <li>March 30, 2003 02:40 rounds to March 30, 2003 04:00</li>
 * </ul>
 * </p>
 * 
 * @param date  the date to work with
 * @param field  the field from <code>Calendar</code>
 *  or <code>SEMI_MONTH</code>
 * @return the rounded date (a different object)
 * @throws IllegalArgumentException if the date is <code>null</code>
 * @throws ArithmeticException if the year is over 280 million
 */
public static Calendar round(Calendar date, int field) {
    if (date == null) {
        throw new IllegalArgumentException("The date must not be null");
    }
    Calendar rounded = (Calendar) date.clone();
    modify(rounded, field, true);
    return rounded;
}

From source file:nl.strohalm.cyclos.utils.DateHelper.java

/**
 * Truncates a date and adds 1 day, handling null. Doesn't modify the Calendar parameter, returning a new modified instance
 *///from  www.j av  a 2  s  . com
public static Calendar truncateNextDay(Calendar date) {
    if (date == null) {
        return null;
    }
    date = (Calendar) date.clone();
    date.add(Calendar.DATE, 1);
    return truncate(date);
}

From source file:nl.strohalm.cyclos.utils.DateHelper.java

/**
 * Truncates a date and subtracts 1 day, handling null. Doesn't modify the Calendar parameter, returning a new modified instance
 *///from   w w  w  .  j a  v  a  2  s.  co  m
public static Calendar truncatePreviosDay(Calendar date) {
    if (date == null) {
        return null;
    }
    date = (Calendar) date.clone();
    date.add(Calendar.DATE, -1);
    return truncate(date);
}

From source file:nl.utwente.bigdata.MapReduce5.java

private static String parseAndMatchTime(String date, Configuration conf) {
    String[] splitdate = date.toString().split("\\s");
    int year = Integer.parseInt(splitdate[5]);
    int month = translateMonthCalendar(splitdate[1]);
    int day = Integer.parseInt(splitdate[2]);
    int hour = Integer.parseInt(splitdate[3].substring(0, 2));
    int minute = Integer.parseInt(splitdate[3].substring(3, 5));
    int second = Integer.parseInt(splitdate[3].substring(6, 7) + "0");
    Calendar start = Calendar.getInstance();
    start.set(Integer.parseInt(conf.get("year")), translateMonthCalendar(conf.get("month")),
            Integer.parseInt(conf.get("day")), Integer.parseInt(conf.get("hour")),
            Integer.parseInt(conf.get("minute")));
    Calendar end = (Calendar) start.clone();
    end.add(Calendar.MINUTE, Integer.parseInt(conf.get("duration")));
    Calendar value = Calendar.getInstance();
    value.set(year, month, day, hour, minute, second);
    if (value.compareTo(start) >= 0 && value.compareTo(end) <= 0) {
        return year + ":" + translateMonth(splitdate[1]) + ":" + day + ":" + hour + ":" + minute + ":" + second;
    } else// www . j  a  v a2 s.co m
        return "false";
}

From source file:com.brayanarias.alarmproject.receiver.AlarmReceiver.java

private static void setAlarm(Context context, Alarm alarm) {
    try {// w  w w . ja v a 2 s. c  om
        PendingIntent pendingIntent = createPendingIntent(context, alarm);
        AlarmManager alarmManager = getAlarmManager(context);
        Calendar actual = Calendar.getInstance();
        actual.set(Calendar.SECOND, 0);
        actual.set(Calendar.MILLISECOND, 0);
        Calendar calendar = (Calendar) actual.clone();
        int ampm = alarm.getAmPm().equals("AM") ? Calendar.AM : Calendar.PM;
        if (alarm.getHour() == 12) {
            calendar.set(Calendar.HOUR, 0);
        } else {
            calendar.set(Calendar.HOUR, alarm.getHour());
        }
        calendar.set(Calendar.AM_PM, ampm);
        calendar.set(Calendar.MINUTE, alarm.getMinute());
        int day = Calendar.getInstance().get(Calendar.DAY_OF_WEEK);
        if (actual.getTimeInMillis() >= calendar.getTimeInMillis()) {
            if (alarm.getDaysOfAlamr().equals("0000000")) {
                calendar.add(Calendar.DATE, 1);
            } else {
                calendar.add(Calendar.DATE, getDaysFromNextAlarm(alarm.getDaysOfAlamr()));
            }
        } else {
            if (!AlarmUtilities.isToday(alarm.getDaysOfAlamr(), day)) {
                calendar.add(Calendar.DATE, getDaysFromNextAlarm(alarm.getDaysOfAlamr()));
            }
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            AlarmManager.AlarmClockInfo clockInfo = new AlarmManager.AlarmClockInfo(calendar.getTimeInMillis(),
                    pendingIntent);
            alarmManager.setAlarmClock(clockInfo, pendingIntent);
        } else if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
            alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
        } else {
            alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
        }

    } catch (Exception e) {
        Log.e(tag, e.getLocalizedMessage(), e);
    }
}

From source file:Main.java

public static boolean CoversFYStart(Calendar one, Calendar two, Calendar fy_start) throws Exception {
    if (one == null || two == null)
        throw new Exception("CoversFYStart: Null dates passed in");
    if (fy_start == null)
        throw new Exception("CoversFYStart: Null FY Start");

    int fy_month = fy_start.get(Calendar.MONTH);
    int fy_day = fy_start.get(Calendar.DAY_OF_MONTH);
    Calendar cal = (Calendar) one.clone();

    while (true) {
        cal.add(Calendar.DATE, 1);
        int month = cal.get(Calendar.MONTH);
        int day = cal.get(Calendar.DAY_OF_MONTH);
        if (month == fy_month && day == fy_day)
            break;
    }/* ww  w .ja v  a  2  s .  c om*/

    return cal.before(two);
}

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

/**
 * /*from w ww.j a v a 2  s . c  o  m*/
 *
 * @return
 */
public static String getNextMonthFirst() {
    Calendar cal = Calendar.getInstance();
    Calendar f = (Calendar) cal.clone();
    f.clear();
    // 
    // f.set(Calendar.YEAR, cal.get(Calendar.YEAR));
    // f.set(Calendar.MONTH, cal.get(Calendar.MONTH) + 1);
    // f.set(Calendar.DATE, 1);
    // or f.set(Calendar.DAY_OF_MONTH,cal.getActualMinimum(Calendar.DATE));
    // return DateFormatUtils.format(f, DATE_FORMAT);

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

From source file:Main.java

public static boolean isCheckChangePermitted(Context context, Calendar date) {

    SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
    String limitActivePref = sharedPrefs.getString("active-date-key", "ALLOW_ALL");

    Calendar today = Calendar.getInstance();
    today.set(Calendar.HOUR_OF_DAY, 0);
    today.set(Calendar.MINUTE, 0);
    today.set(Calendar.SECOND, 0);
    today.set(Calendar.MILLISECOND, 0);

    Calendar yesterday;//w ww  .  j a  v a 2  s .c om

    switch (limitActivePref) {
    case "ALLOW_CURRENT":
        return (date.compareTo(today) == 0);
    case "ALLOW_CURRENT_AND_NEXT_DAY":
        yesterday = (Calendar) today.clone();
        yesterday.add(Calendar.DATE, -1);
        return (date.compareTo(yesterday) >= 0);
    case "ALLOW_CURRENT_AND_NEXT_DAY_AND_WEEKEND":
        yesterday = (Calendar) today.clone();
        yesterday.add(Calendar.DATE, today.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY ? -2 : -1);
        return (date.compareTo(yesterday) >= 0);
    case "ALLOW_ALL":
    default:
        return true;
    }
}

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

/**
 * //from  w w w  . j av a 2 s .  c o  m
 *
 * @return
 */
public static String getPreviousMonthFirst() {
    Calendar cal = Calendar.getInstance();
    Calendar f = (Calendar) cal.clone();
    f.clear();
    // 
    // f.set(Calendar.YEAR, cal.get(Calendar.YEAR));
    // f.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 1);
    // f.set(Calendar.DATE, 1);
    // return DateFormatUtils.format(f, DATE_FORMAT);

    // 
    // f.set(Calendar.YEAR, cal.get(Calendar.YEAR));
    // f.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 1);
    // f.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DATE));
    // return DateFormatUtils.format(f, DATE_FORMAT);

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

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

/**
 * ??// w w  w.j a va  2  s .c  o m
 *
 * @return
 */
public static String getMonthLastDay() {
    Calendar cal = Calendar.getInstance();
    Calendar f = (Calendar) cal.clone();
    f.clear();
    // 
    // f.set(Calendar.YEAR, cal.get(Calendar.YEAR));
    // f.set(Calendar.MONTH, cal.get(Calendar.MONTH) + 1);
    // f.set(Calendar.MILLISECOND, -1);
    // return DateFormatUtils.format(f, DATE_FORMAT);

    // 
    // f.set(Calendar.YEAR, cal.get(Calendar.YEAR));
    // f.set(Calendar.MONTH, cal.get(Calendar.MONTH));
    // f.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DATE));
    // return DateFormatUtils.format(f, DATE_FORMAT);

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