Example usage for java.util Calendar set

List of usage examples for java.util Calendar set

Introduction

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

Prototype

public void set(int field, int value) 

Source Link

Document

Sets the given calendar field to the given value.

Usage

From source file:Main.java

/**
 * Get the SunRise for the given date//  w  ww. ja  va  2s . c  o m
 * @param date - input date
 * @return - Date with time specifying the Sun Rise
 */
public static Date getSunRise(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.set(Calendar.HOUR_OF_DAY, 06);
    cal.set(Calendar.MINUTE, 00);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    Date ret = cal.getTime();
    return ret;
}

From source file:Main.java

public static Date getStartOfDate(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);//  w ww .  ja v  a2s.  c  om
    calendar.set(Calendar.HOUR, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    return calendar.getTime();
}

From source file:Main.java

static void setDate(Calendar cal, int month, int date) {
    cal.clear();//from  ww w . j  av a2 s  .  co m

    cal.set(Calendar.YEAR, Calendar.getInstance().get(Calendar.YEAR));
    cal.set(Calendar.MONTH, month);
    cal.set(Calendar.DATE, date);
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
}

From source file:Main.java

public static Date dayBegin(final Date date) {
    Calendar c = Calendar.getInstance();
    c.setTime(date);/*from   ww w  .j a v  a  2s  . com*/
    c.set(Calendar.HOUR_OF_DAY, 0);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.SECOND, 0);
    c.set(Calendar.MILLISECOND, 0);
    return c.getTime();
}

From source file:Main.java

public static Date dayEnd(final Date date) {
    Calendar c = Calendar.getInstance();
    c.setTime(date);/* w  w w  .  j av  a 2 s  .c  o m*/
    c.set(Calendar.HOUR_OF_DAY, 23);
    c.set(Calendar.MINUTE, 59);
    c.set(Calendar.SECOND, 59);
    c.set(Calendar.MILLISECOND, 999);
    return c.getTime();
}

From source file:Main.java

public static Calendar tomorrow() {
    Calendar date = new GregorianCalendar();
    date.add(Calendar.DATE, 1);/*from  ww  w . j a  va2  s .  com*/
    date.set(Calendar.HOUR_OF_DAY, 0);
    date.set(Calendar.MINUTE, 0);
    date.set(Calendar.SECOND, 0);
    date.set(Calendar.MILLISECOND, 0);
    return date;
}

From source file:Main.java

public static long getDayInUTC(long time) {
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(time);/*from www  .  ja  v a 2s.  co  m*/
    cal.set(Calendar.HOUR, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    cal.setTimeZone(TimeZone.getTimeZone("UTC"));
    return cal.getTimeInMillis();
}

From source file:Main.java

/**
 * getDateEnd/*w w  w  .  j a  v a2s. c  om*/
 * @param date the date that needs to be converted to an end date
 * @return a date at 11:59 PM
 */
public static Date getDateEnd(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.set(Calendar.HOUR_OF_DAY, 23);
    cal.set(Calendar.MINUTE, 59);
    cal.set(Calendar.SECOND, 59);
    cal.set(Calendar.MILLISECOND, 59);
    long time = cal.getTimeInMillis();
    date.setTime(time);
    return date;
}

From source file:Main.java

/**
 * Using the expiry date, generate the application end date in the correct format.
 *
 * @param expiryMonth Month of expiry./*from  w  ww.j a v  a2s .c  o m*/
 * @param expiryYear  Year of expiry.
 * @return The application end date in the correct format.
 */
private static String generateExpiryDate(String expiryMonth, String expiryYear) {
    // Just combine the year and month
    String applicationEndDate = expiryYear + expiryMonth;

    // Now need to work out the last day of the month
    // NB: Calendar's months range from 0-11 rather than 1-12
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.YEAR, Integer.parseInt(expiryYear));
    calendar.set(Calendar.MONTH, Integer.parseInt(expiryMonth) - 1);
    applicationEndDate += calendar.getActualMaximum(Calendar.DATE);

    return applicationEndDate;
}

From source file:Main.java

public static long formatTimeString(String timeString) {
    long time = 0;
    if (timeString == null || timeString.length() < 1) {
        return time;
    }/*ww w  .  j  a v  a2s.  c  o  m*/
    try {
        String[] array = timeString.split(DASH);
        int year = Integer.valueOf(array[0]);
        int month = Integer.valueOf(array[1]);
        int day = Integer.valueOf(array[2]);

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.DAY_OF_MONTH, day);

        time = calendar.getTimeInMillis();

    } catch (Exception e) {
        e.printStackTrace();
    }

    return time;
}