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:ezbake.services.provenance.graph.Utils.java

public static Date getCurrentDate() {
    // zero out millisecond
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.MILLISECOND, 0);
    return cal.getTime();
}

From source file:Main.java

/**
 * Get a list of initial and end calendar of months in the range received
 * @param cStart Calendar date to start/*from   w ww  .  jav a  2 s.co  m*/
 * @param cEnd Calendar date to end
 * @return List<Pair<Calendar, Calendar>> list of calendars (initial and end)
 */
public static List<Pair<Calendar, Calendar>> getRangeInMonths(Calendar cStart, Calendar cEnd) {

    //generate the list
    List<Pair<Calendar, Calendar>> calendars = new ArrayList<>();

    //from the first calendar start adding a month until the actual calendar is after the end
    Calendar cActual = generateCalendar(cStart);
    cActual.set(Calendar.DAY_OF_MONTH, 1);
    Calendar c0;
    Calendar cF;

    while (cActual.compareTo(cEnd) < 0) {

        //calendar start
        if (calendars.size() == 0) {
            c0 = generateCalendar(cStart);
        } else {
            c0 = generateCalendar(cActual);
        }

        //increment a month
        cActual.add(Calendar.MONTH, 1);

        //calendar end
        if (cActual.after(cEnd)) {
            cF = generateCalendar(cEnd);
        } else {
            cF = generateCalendar(cActual);

            //remove 1 day to set the last day of the month
            cF.add(Calendar.DAY_OF_YEAR, -1);
        }

        //add the pair to the list
        calendars.add(new Pair<Calendar, Calendar>(c0, cF));
    }

    //return the list
    return calendars;
}

From source file:org.openmrs.module.uiframeworkpatientsummarysupport.fragment.controller.PatientObsFlowsheetFragmentController.java

/**
 * This method was actually added in core as of 1.9
 *//*  ww  w. ja v a2 s. c om*/
private static Date startOfDay(Date date) {
    if (date == null)
        return null;

    Calendar c = Calendar.getInstance();
    c.setTime(date);
    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: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);//from   ww  w .  j ava2s .  c om
    return (new SimpleDateFormat(fmt)).format(cal.getTime());
}

From source file:Utils.java

public static Date getEndOfMinute(Date day, Calendar cal) {
    if (day == null || cal == null) {
        return day;
    }/*from w w w .ja  v  a 2s . co m*/

    cal.setTime(day);
    cal.set(Calendar.SECOND, cal.getMaximum(Calendar.SECOND));
    cal.set(Calendar.MILLISECOND, cal.getMaximum(Calendar.MILLISECOND));
    return cal.getTime();
}

From source file:com.wiiyaya.framework.common.utils.DateUtils.java

/**
 * 00:00:00/*from w w w  .  j av  a  2s  .c  o m*/
 *
 * @param date 
 * @return ??
 */
public static Date setDayStart(final Date date) {
    final Calendar c = Calendar.getInstance();
    c.setLenient(false);
    c.setTime(date);
    c.set(Calendar.HOUR_OF_DAY, 0);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.SECOND, 0);
    return c.getTime();
}

From source file:com.wiiyaya.framework.common.utils.DateUtils.java

/**
 * ?23:59:59//from   ww w  .java 2  s . c  o m
 * @param date 
 * @return ??
 */
public static Date setDayEnd(final Date date) {
    final Calendar c = Calendar.getInstance();
    c.setLenient(false);
    c.setTime(date);
    c.set(Calendar.HOUR_OF_DAY, 23);
    c.set(Calendar.MINUTE, 59);
    c.set(Calendar.SECOND, 59);
    return c.getTime();
}

From source file:Main.java

/**
 * Try to parse input with SimpleDateFormat
 *
 * @param input input.//from  w ww .j a va 2  s  . c  om
 * @param format      SimpleDateFormat
 * @param setYear1700 When true the age will be not displayed in brackets
 * @param locale locale.
 * @return Date object if successful, otherwise null
 */
private static Date parseStringWithSimpleDateFormat(String input, String format, boolean setYear1700,
        Locale locale) {
    SimpleDateFormat dateFormat = new SimpleDateFormat(format, locale);
    dateFormat.setTimeZone(TimeZone.getDefault());

    try {
        Date parsedDate = dateFormat.parse(input);

        /*
         * Because no year is defined in address book, set year to 1700
         *
         * When year < 1800, the age will be not displayed in brackets
         */
        if (setYear1700) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(parsedDate);
            cal.set(Calendar.YEAR, 1700);
        }

        return parsedDate;
    } catch (ParseException ignored) {
        return null;
    }
}

From source file:Main.java

/**
 * Returns milliseconds of the date argument dt. If the argument
 * isIncludeTime is false then the returned milliseconds does not include
 * time./*ww  w .jav a  2s .  c  o  m*/
 * 
 * @param dt
 * @param isIncludeTime
 * @return
 */
private static long getDate(Date dt, boolean isIncludeTime) {
    Calendar cal = GregorianCalendar.getInstance();
    cal.setTime(dt);
    if (!isIncludeTime) {
        cal.set(Calendar.HOUR, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
    }
    return cal.getTimeInMillis();
}

From source file:gabriel.domain.BirthdateValidatorTest.java

private static Date getDate(int years) {
    Date date = new Date();
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);//from   w w w.  j  a  va2 s  . c o m
    calendar.set(YEAR, calendar.get(YEAR) - years);
    return calendar.getTime();
}