Example usage for java.util Calendar add

List of usage examples for java.util Calendar add

Introduction

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

Prototype

public abstract void add(int field, int amount);

Source Link

Document

Adds or subtracts the specified amount of time to the given calendar field, based on the calendar's rules.

Usage

From source file:Main.java

public static Long GetUTCTime() {
    Calendar cal = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault());
    int zoneOffset = cal.get(Calendar.ZONE_OFFSET);
    int dstOffset = cal.get(Calendar.DST_OFFSET);
    cal.add(java.util.Calendar.MILLISECOND, -(zoneOffset + dstOffset));
    return cal.getTimeInMillis();
}

From source file:Main.java

public static String getStringByOffset(Date date, String format, int calendarField, int offset) {
    String strDate = null;/* w w  w. ja  v a  2s . c om*/
    try {
        Calendar c = new GregorianCalendar();
        SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
        c.setTime(date);
        c.add(calendarField, offset);
        strDate = mSimpleDateFormat.format(c.getTime());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return strDate;
}

From source file:it.unicaradio.android.models.Transmission.java

private static String adjustTime(String time) {
    SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
    String adjustedTime = time;//from  w w  w . ja va  2 s  .co  m
    try {
        Date date = formatter.parse(time);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.HOUR_OF_DAY, 1);
        adjustedTime = formatter.format(cal.getTime());
    } catch (ParseException e) {
    }

    return adjustedTime;
}

From source file:Main.java

public static String getStringByOffset(Date date, String format, int calendarField, int offset) {
    String strDate = null;//from  w w  w . j a v  a 2  s.c o m
    try {
        Calendar c = new GregorianCalendar();
        SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format, Locale.CHINA);
        c.setTime(date);
        c.add(calendarField, offset);
        strDate = mSimpleDateFormat.format(c.getTime());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return strDate;
}

From source file:Main.java

public static Date startOfNextMonth(int startDayOfMonth) {
    Calendar cal = Calendar.getInstance();

    if (cal.get(Calendar.DATE) >= startDayOfMonth)// the end is one day past startDayOfMonth, next month
        cal.add(Calendar.MONTH, 1);

    int lastDayInMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH);

    if (startDayOfMonth < lastDayInMonth)
        cal.set(Calendar.DATE, startDayOfMonth + 1);
    else {//from   w w w  . jav  a 2  s.  co  m
        cal.add(Calendar.MONTH, 1);
        cal.set(Calendar.DATE, 1);
    }

    return startOfDay(cal.getTime());
}

From source file:com.acmeair.loader.FlightLoader.java

private static Date getArrivalTime(Date departureTime, int mileage) {
    double averageSpeed = 600.0; // 600 miles/hours
    double hours = (double) mileage / averageSpeed; // miles / miles/hour = hours
    double partsOfHour = hours % 1.0;
    int minutes = (int) (60.0 * partsOfHour);
    Calendar c = Calendar.getInstance();
    c.setTime(departureTime);//from   w  w  w.j  a va 2 s  . c  o  m
    c.add(Calendar.HOUR, (int) hours);
    c.add(Calendar.MINUTE, minutes);
    return c.getTime();
}

From source file:com.beligum.core.accounts.UserManager.java

public static User getCurrentUser() {
    User retVal = null;//from  ww w. j a  v  a  2  s  .  co m
    if (Http.Context.current().session().get(SESSION_KEY_USER) != null) {
        Integer id;
        try {
            id = Integer.parseInt(Http.Context.current().session().get(SESSION_KEY_USER));
        } catch (Exception e) {
            return null;
        }
        retVal = UserRepository.find(id);
        if (retVal != null) {
            String currentTimeOut = Http.Context.current().session().get(SESSION_KEY_TIMEOUT);
            if (currentTimeOut == null) {
                retVal = null;
            } else if (DateTimeHelper.getCurrentTime()
                    .before(DateTimeHelper.formatStringToDate(currentTimeOut, "dd/MM/yyyy HH:mm"))) {
                Calendar timeOut = DateTimeHelper.getCurrentTime();
                timeOut.add(Calendar.HOUR, 1);
                Http.Context.current().session().put(SESSION_KEY_TIMEOUT,
                        DateTimeHelper.formatDate(timeOut, "dd/MM/yyyy HH:mm"));
            } else {
                retVal = null;
            }
        }

    }
    return retVal;

}

From source file:com.iflytek.kcloud.web.utils.BookDateUtil.java

/**
 * getDay:??. <br/>/* ww w  .  j a va  2s .  co  m*/
 *
 * @param gap ?(0,1,-1....)
 * @return
 * @author zyyang3
 * @since JDK 1.6
 */
public static String getDay(int gap) {
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.DAY_OF_MONTH, gap);
    Date date = cal.getTime();
    String day = DateFormatUtils.format(date, "yyyy-MM-dd");
    return day;
}

From source file:Main.java

/**
 * Parses a date/time string from within an XMLTV stream.  Only the local
 * time part of the value is parsed. Timezone specifications are ignored.
 * /*from  w w  w  .j  a va  2  s  . com*/
 * @param str the string
 * @param convertTimezone true to convert date/time values to local time
 * @return the date/time value as a Calendar object
 */
public static Calendar parseDateTime(String str, boolean convertTimezone) {
    // Format looks like: 20111209060000 +1100
    if (str == null)
        return null;
    int year = parseField(str, 0, 4);
    int month = parseField(str, 4, 2);
    int day = parseField(str, 6, 2);
    int hour = parseField(str, 8, 2);
    int minute = parseField(str, 10, 2);
    int second = parseField(str, 12, 2);
    if (convertTimezone) {
        if (cachedTimeZone == null) {
            cachedTimeZone = TimeZone.getDefault();
            cachedOffset = cachedTimeZone.getOffset(System.currentTimeMillis());
        }
        int tz = parseTZField(str, 14);
        if (tz != cachedOffset) {
            Calendar calendar = new GregorianCalendar(year, month - 1, day, hour, minute, second);
            calendar.add(Calendar.MILLISECOND, cachedOffset - tz);
            return calendar;
        }
    }
    return new GregorianCalendar(year, month - 1, day, hour, minute, second);
}

From source file:Main.java

public static Date getLastWeekMonday() {
    Calendar cal = Calendar.getInstance();
    for (int mondays = 0; mondays < 2;) {
        if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) {
            mondays++;/*from   ww w.  ja v a 2  s .c  o  m*/
        }
        cal.add(Calendar.DAY_OF_WEEK, -1);
    }
    return cal.getTime();
}