Example usage for java.util Calendar setTime

List of usage examples for java.util Calendar setTime

Introduction

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

Prototype

public final void setTime(Date date) 

Source Link

Document

Sets this Calendar's time with the given Date.

Usage

From source file:net.ceos.project.poi.annotated.bean.SimpleObjectBuilder.java

/**
 * Validate the SimpleObject based on the object build with the method
 * 'buildSimpleObject'/*from   w  ww . j a va 2s .  c o m*/
 * 
 * @param toValidate
 *            the object to validate
 */
public static void validateSimpleObject(SimpleObject toValidate) {
    SimpleObject base = buildSimpleObject();

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new Date());

    Calendar calendarUnmarshal = Calendar.getInstance();
    calendarUnmarshal.setTime(toValidate.getDateAttribute());
    assertEquals(calendar.get(Calendar.YEAR), calendarUnmarshal.get(Calendar.YEAR));
    assertEquals(calendar.get(Calendar.MONTH), calendarUnmarshal.get(Calendar.MONTH));
    assertEquals(calendar.get(Calendar.DAY_OF_MONTH), calendarUnmarshal.get(Calendar.DAY_OF_MONTH));
    assertEquals(base.getStringAttribute(), toValidate.getStringAttribute());
    assertEquals(base.getIntegerAttribute(), toValidate.getIntegerAttribute());
    // TODO add new validation below
}

From source file:net.duckling.ddl.util.Utility.java

public static String getDateCustom(Date date, String fmt) {
    Calendar calToday = Calendar.getInstance();
    calToday.set(Calendar.HOUR_OF_DAY, 0);
    Calendar calYear = Calendar.getInstance();
    calYear.set(Calendar.DAY_OF_YEAR, 1);
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    return (new SimpleDateFormat(fmt)).format(cal.getTime());
}

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  ww.  j  a v a  2s  . c om
 *
 * @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:com.castis.xylophone.adsmadapter.common.util.CiDateUtil.java

public static Calendar convertCalendar(String srcDateString, String format) {
    Date tmpDate = new Date();
    SimpleDateFormat simple = new SimpleDateFormat(format);
    try {//  w  w  w . ja v  a 2  s. com
        tmpDate = simple.parse(srcDateString);
    } catch (ParseException e) {
        log.warn("Parse Exception : src-" + srcDateString + ", format-" + format);
        return null;
    }
    Calendar cal = Calendar.getInstance();
    cal.setTime(tmpDate);

    return cal;
}

From source file:Main.java

/**
 * Sets the specified field to a date returning a new object.  
 * This does not use a lenient calendar.
 * The original date object is unchanged.
 *
 * @param date  the date, not null//w w  w  . j a va2s . c o m
 * @param calendarField  the calendar field to set the amount to
 * @param amount the amount to set
 * @return a new Date object set with the specified value
 * @throws IllegalArgumentException if the date is null
 * @since 2.4
 */
private static Date set(Date date, int calendarField, int amount) {
    if (date == null) {
        throw new IllegalArgumentException("The date must not be null");
    }
    // getInstance() returns a new object, so this method is thread safe.
    Calendar c = Calendar.getInstance();
    c.setLenient(false);
    c.setTime(date);
    c.set(calendarField, amount);
    return c.getTime();
}

From source file:edu.sjsu.cmpe275.project.util.HtmlPageComposor.java

public static String orderReceipt(Reservation reservation) {
    //TODO: discount + total fee
    String bill = "";
    int total = 0;

    SimpleDateFormat sdf = new SimpleDateFormat("MM/DD/YYYY");
    Date date = reservation.getCheckinDate();
    bill = "<table border='1' style='width:100%'>";
    bill += "<tr>";
    bill += "<th>Date</th>";
    bill += "<th>Room No</th>";
    bill += "<th>Base Price</th>";
    bill += "<th>Discount(%)</th>";
    bill += "<th>Final Fee</th>";
    bill += "</tr>";
    while (date.before(reservation.getCheckoutDate())) {

        for (Room room : reservation.getRoomList()) {
            bill += "<tr>";
            bill += "<td>" + sdf.format(date) + "</td>";
            bill += "<td>" + room.getRoomNo() + "</td>";
            bill += "<td>" + room.getBasePrice() + "</td>";
            bill += "<td>" + reservation.getDiscount() + "</td>";
            int fee = room.getBasePrice() * (100 - reservation.getDiscount()) / 100;
            total += fee;/*from ww  w  .j a v a  2  s  .co  m*/
            bill += "<td>" + fee + "</td>";
            bill += "</tr>";
        }
        //increase one day
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DATE, 1);
        date = c.getTime();
    }
    bill += "</table>";
    bill += "<h3 align='right'>Total($): " + total + "</h3>";

    String page = "<html><body>" + "<h3>Dear " + reservation.getName().getFname() + ",</h3>"
            + "<h3>Thank you for choosing us serving you! Your room(s) have been checked out.</h3>"
            + "<h3>Here is the detail of bill:</h3>" + bill + "<h3>Regards,</h3>"
            + "<h3>CMPE275 Mini Hotel Team</h3>" + "</body></html>";
    return page;
}

From source file:Main.java

public static Calendar getDateFromString(String strDate) {
    Calendar date = Calendar.getInstance();
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
    try {/*w  ww  .j av  a 2 s. c  om*/
        date.setTime(simpleDateFormat.parse(strDate));
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return date;
}

From source file:Main.java

public static int getWeekNumber(String strDate, String inFormat) {
    Calendar calendar = new GregorianCalendar();
    DateFormat df = new SimpleDateFormat(inFormat);
    try {//w ww.  jav  a  2 s. c o m
        calendar.setTime(df.parse(strDate));
    } catch (Exception e) {
        return -1;
    }
    int intTemp = calendar.get(Calendar.DAY_OF_WEEK) - 1;
    return intTemp;
}

From source file:net.duckling.ddl.util.Utility.java

public static String getDateShort(Date date) {
    Calendar calToday = Calendar.getInstance();
    calToday.set(Calendar.HOUR_OF_DAY, 0);
    Calendar calYear = Calendar.getInstance();
    calYear.set(Calendar.DAY_OF_YEAR, 1);
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    if (cal.after(calToday)) {
        return (new SimpleDateFormat("HH:mm")).format(cal.getTime());
    } else if (cal.after(calYear)) {
        return (new SimpleDateFormat("MMdd")).format(cal.getTime());
    } else {/*from   ww  w.  j  a  v  a  2  s. com*/
        return (new SimpleDateFormat("yyyy")).format(cal.getTime());
    }
}

From source file:com.github.daddo.validation.validator.Util.java

static Calendar safeCastToCalendar(Object object) {

    if (object == null)
        return null;

    if (object instanceof Calendar)
        return (Calendar) object;

    if (object instanceof Date) {
        Calendar result = Calendar.getInstance();
        result.setTime((Date) object);
        return result;
    }//from  w w w . ja  v  a2s.  c  om

    if (object instanceof Number) {
        Calendar result = Calendar.getInstance();
        result.setTimeInMillis(((Number) object).longValue());
        return result;
    }

    throw new UnexpectedTypeException(object.getClass(), Calendar.class, Date.class, Number.class);
}