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: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  va2 s  .  c o  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 getCalendarAtMidnight(int relativeDayToToday) {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.add(Calendar.DAY_OF_YEAR, relativeDayToToday);
    return calendar;
}

From source file:Main.java

public static Date getFetchDelayDate(Date startDate, Integer dateDelayMinutes) {
    Calendar c = Calendar.getInstance();
    c.setTime(startDate);//  w w  w.j a  va  2s .c  o m
    if (dateDelayMinutes > 0) {
        dateDelayMinutes = -dateDelayMinutes;
    }
    c.add(Calendar.MINUTE, dateDelayMinutes);
    return c.getTime();
}

From source file:parlare.application.server.controller.ControllerFunctions.java

public static String listCookies(String method, String listCookies) throws IOException {

    String printHTML = "";

    switch (method) {

    case "javascript":

        printHTML += "<script>";
        printHTML += "function setCookie(name,value,days) { ";
        printHTML += "var expires = \"\"; if (days) { var date = new Date(); date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); expires = \"; expires=\" + date.toGMTString(); } ";
        printHTML += "document.cookie = name + \"=\" + value + expires + \"; path=/\";";
        printHTML += "} ";

        printHTML += "function getCookie(name) { var nameEQ = name + \"=\"; var ca = document.cookie.split(';'); for (var i = 0; i < ca.length; i++) { var c = ca[i]; ";
        printHTML += "while (c.charAt(0) === ' ') { c = c.substring(1, c.length); } if (c.indexOf(nameEQ) === 0) { return c.substring(nameEQ.length, c.length); } } return null; } ";

        printHTML += "function eraseCookie(name) { setCookie(name, \"\", -1); } ";
        printHTML += "</script>";

        if (listCookies != null) {
            String[] tokens = listCookies.split(";");
            printHTML += "<script>";
            for (String token : tokens) {
                String[] data = token.trim().split("=");
                if (data.length == 2) {
                    printHTML += " setCookie('" + data[0] + "', '" + data[1] + "', 60);";
                }// w ww .j  a v  a2  s .  c o m
            }
            printHTML += "</script>";
        }
        break;
    case "java":

        DefaultHttpClient httpClient = new DefaultHttpClient();
        CookieStore cookieStore = httpClient.getCookieStore();
        BasicClientCookie cookie = new BasicClientCookie("HelpMeUniverse", "I need your help my dear universe");

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR, 100);
        cookie.setExpiryDate(calendar.getTime());
        cookie.setDomain("localhost");
        cookie.setPath("/");

        cookieStore.addCookie(cookie);
        httpClient.setCookieStore(cookieStore);

        // Prepare a request object
        HttpGet httpGet = new HttpGet("http://localhost/");

        // Execute the request
        HttpResponse httpResponse = httpClient.execute(httpGet);

        // Examine the response status
        System.out.println("Http request response is: " + httpResponse.getStatusLine());

        break;
    }

    return printHTML;

}

From source file:com.google.cloud.backend.spi.BlobUrlManager.java

private static long getExpiration(int minutesFromNow) {
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.MINUTE, minutesFromNow);
    long expiration = calendar.getTimeInMillis() / 1000L;
    return expiration;
}

From source file:com.fluke.util.Util.java

public static String addDate(Date date, int noOfDays) {
    Calendar c = Calendar.getInstance();
    c.setTime(date);/*from   w  ww .  jav  a  2s .  com*/
    c.add(Calendar.DATE, noOfDays); // Adding 5 days
    return getDate(c.getTime());

}

From source file:com.ourlife.dev.common.utils.DateUtils.java

/**
 * ??//www.  ja v a 2  s. co  m
 * 
 * @param date
 * @return
 */
public static String[] getDayBeginTimeAndEndTime(String date) {
    String[] dates = new String[2];
    Date beginDate = DateUtils.parseDate(date);
    dates[0] = DateUtils.formatDate(beginDate, "yyyy-MM-dd HH:mm:ss");
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(beginDate);
    calendar.add(Calendar.DATE, 1);
    dates[1] = DateUtils.formatDate(calendar.getTime(), "yyyy-MM-dd HH:mm:ss");
    return dates;
}

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

public static String getNextThreeDay() {
    Calendar c = Calendar.getInstance();
    c.setTime(new Date());
    c.add(Calendar.DATE, 3);
    return AoneTimeUtils.formatToDateTime(c.getTime());
}

From source file:Main.java

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

    if (cal.get(Calendar.DATE) < startDayOfMonth) // the start is startDayOfMonth, last month
        cal.add(Calendar.MONTH, -1);

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

    if (startDayOfMonth <= lastDayInMonth)
        cal.set(Calendar.DATE, startDayOfMonth);
    else {//  w  ww . j  a  va  2s .  co  m
        cal.add(Calendar.MONTH, 1);
        cal.set(Calendar.DATE, 1);
    }

    return startOfDay(cal.getTime());
}

From source file:io.github.benas.jpopulator.impl.BeanValidationRandomizer.java

/**
 * Generate a random value according to the validation constraint present on a field.
 *
 * @param field the field to populate//ww  w.j ava  2  s  .  c o  m
 * @return a random value according to the validation constraint
 */
public static Object getRandomValue(final Field field) {

    Class<?> fieldType = field.getType();

    Object result = null;

    if (field.isAnnotationPresent(AssertFalse.class)) {
        result = false;
    }
    if (field.isAnnotationPresent(AssertTrue.class)) {
        result = true;
    }
    if (field.isAnnotationPresent(Null.class)) {
        result = null;
    }
    if (field.isAnnotationPresent(Future.class)) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.YEAR, ConstantsUtil.DEFAULT_DATE_RANGE);
        result = new DateRangeRandomizer(new Date(), calendar.getTime()).getRandomValue();
    }
    if (field.isAnnotationPresent(Past.class)) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.YEAR, -ConstantsUtil.DEFAULT_DATE_RANGE);
        result = new DateRangeRandomizer(calendar.getTime(), new Date()).getRandomValue();
    }
    if (field.isAnnotationPresent(Max.class)) {
        Max maxAnnotation = field.getAnnotation(Max.class);
        long maxValue = maxAnnotation.value();
        result = MaxValueRandomizer.getRandomValue(fieldType, maxValue);
    }
    if (field.isAnnotationPresent(DecimalMax.class)) {
        DecimalMax decimalMaxAnnotation = field.getAnnotation(DecimalMax.class);
        BigDecimal decimalMaxValue = new BigDecimal(decimalMaxAnnotation.value());
        result = MaxValueRandomizer.getRandomValue(fieldType, decimalMaxValue.longValue());
    }
    if (field.isAnnotationPresent(Min.class)) {
        Min minAnnotation = field.getAnnotation(Min.class);
        long minValue = minAnnotation.value();
        result = MinValueRandomizer.getRandomValue(fieldType, minValue);
    }
    if (field.isAnnotationPresent(DecimalMin.class)) {
        DecimalMin decimalMinAnnotation = field.getAnnotation(DecimalMin.class);
        BigDecimal decimalMinValue = new BigDecimal(decimalMinAnnotation.value());
        result = MinValueRandomizer.getRandomValue(fieldType, decimalMinValue.longValue());
    }
    if (field.isAnnotationPresent(Size.class)) {
        Size sizeAnnotation = field.getAnnotation(Size.class);
        int minSize = sizeAnnotation.min();
        int maxSize = sizeAnnotation.max();
        result = RandomStringUtils.randomAlphabetic(new RandomDataGenerator().nextInt(minSize, maxSize));
    }
    return result;

}