Example usage for java.util Calendar DAY_OF_MONTH

List of usage examples for java.util Calendar DAY_OF_MONTH

Introduction

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

Prototype

int DAY_OF_MONTH

To view the source code for java.util Calendar DAY_OF_MONTH.

Click Source Link

Document

Field number for get and set indicating the day of the month.

Usage

From source file:Main.java

public static String getToday() {
    Calendar calendar = Calendar.getInstance();
    int month = calendar.get(Calendar.MONTH) + 1;
    return calendar.get(Calendar.YEAR) + "-" + ((month < 10) ? "0" + month : "" + month) + "-"
            + calendar.get(Calendar.DAY_OF_MONTH);
}

From source file:Main.java

public static String getCurrentTimeStampYYYY_MM_DD_HH_MM_SS() {
    Calendar cal = new GregorianCalendar();
    return makeTimeString(cal.get(Calendar.YEAR), 4) + "/" + makeTimeString(cal.get(Calendar.MONTH) + 1, 2)
            + "/" + makeTimeString(cal.get(Calendar.DAY_OF_MONTH), 2) + " "
            + makeTimeString(cal.get(Calendar.HOUR_OF_DAY), 2) + ":"
            + makeTimeString(cal.get(Calendar.MINUTE), 2) + ":" + makeTimeString(cal.get(Calendar.SECOND), 2);
}

From source file:Main.java

public static int[] parseDate(String dateString) {
    try {/*from w  w w  .ja v  a2 s  .  com*/
        DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
        formatter.setLenient(false);
        Date date = (Date) formatter.parse(dateString);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int[] dateInts = new int[3];
        dateInts[0] = cal.get(Calendar.DAY_OF_MONTH);
        dateInts[1] = cal.get(Calendar.MONTH);
        dateInts[2] = cal.get(Calendar.YEAR);

        return dateInts;
    } catch (Exception e) {
        throw new RuntimeException(
                "Could not parse date:'" + dateString + "'. Date format is dd-MM-yyyy (ex: 31-12-2011).", e);
    }
}

From source file:Main.java

public static Date buildShiftEndTime(int startHour, int startMin, int startAmPm, int endHour, int endMin,
        int endAmPm) {
    endHour = endHour == 12 ? 0 : endHour;

    calendar.clear();/*from  w w  w  .  ja  v  a2  s. c om*/

    calendar.set(Calendar.HOUR, endHour);
    calendar.set(Calendar.MINUTE, endMin);

    calendar.set(Calendar.AM_PM, endAmPm);

    if (startAmPm == Calendar.PM && endAmPm == Calendar.AM) {
        calendar.add(Calendar.DAY_OF_MONTH, 1);
    }

    return calendar.getTime();
}

From source file:Main.java

/**
 * yyyyMMddHHmmss//from  w  w  w.  ja  v  a  2 s.  c  o  m
 */
public static String getFormatTime3(Calendar c) {
    DecimalFormat df = new DecimalFormat("00");
    String strFileName = c.get(Calendar.YEAR) + df.format((c.get(Calendar.MONTH) + 1))
            + df.format(c.get(Calendar.DAY_OF_MONTH)) + df.format(c.get(Calendar.HOUR_OF_DAY))
            + df.format(c.get(Calendar.MINUTE)) + df.format(c.get(Calendar.SECOND));

    return strFileName;
}

From source file:Main.java

/**
 * Get the number of rows for the provided month
 * @param year year/*from  w  ww. j  a va 2  s.c  o m*/
 * @param month month
 * @return number of rows
 */
public static int getNumOfRowsForTheMonth(int year, int month, int startDayOfTheWeek) {
    Calendar cal = Calendar.getInstance();
    cal.set(year, month, 1);
    MonthDisplayHelper displayHelper = new MonthDisplayHelper(year, month, startDayOfTheWeek);
    return displayHelper.getRowOf(cal.getActualMaximum(Calendar.DAY_OF_MONTH)) + 1;
}

From source file:Main.java

/**
 * Calculates the difference in days, rounding the operands down to the nearest day.
 * Ex. Jan 3rd 12:31 pm - Jan 2nd 4:00 pm
 * =   Jan 3rd - Jan 2nd/*ww w.  j av a  2  s . co  m*/
 * =   1 day 
 * @return The difference in days.
 */
public static int differenceInDays(GregorianCalendar minuend, GregorianCalendar subtrahend) {
    GregorianCalendar minuendFloor = new GregorianCalendar(minuend.get(Calendar.YEAR),
            minuend.get(Calendar.MONTH), minuend.get(Calendar.DAY_OF_MONTH));
    GregorianCalendar subtrahendFloor = new GregorianCalendar(subtrahend.get(Calendar.YEAR),
            subtrahend.get(Calendar.MONTH), subtrahend.get(Calendar.DAY_OF_MONTH));
    GregorianCalendar result = new GregorianCalendar();
    result.setTimeInMillis(minuendFloor.getTimeInMillis() - subtrahendFloor.getTimeInMillis());
    return result.get(Calendar.DAY_OF_YEAR);
}

From source file:Main.java

public static XMLGregorianCalendar dateToXmlCalendar(Date date) {
    if (date != null) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);//from  ww  w. ja v  a  2s .co m

        try {
            XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar();
            xmlGregorianCalendar.setDay(calendar.get(Calendar.DAY_OF_MONTH));
            xmlGregorianCalendar.setMonth(calendar.get(Calendar.MONTH) + 1);
            xmlGregorianCalendar.setYear(calendar.get(Calendar.YEAR));
            return xmlGregorianCalendar;
        } catch (DatatypeConfigurationException e) {
            return null;
        }
    }

    return null;
}

From source file:Main.java

public static ArrayList<String> getCurrentWeek() {
    SimpleDateFormat format = new SimpleDateFormat("dd");
    Calendar calendar = Calendar.getInstance();
    calendar.setFirstDayOfWeek(Calendar.SUNDAY);
    calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);

    ArrayList<String> days = new ArrayList<String>();
    for (int i = 0; i < 7; i++) {
        days.add(format.format(calendar.getTime()));
        calendar.add(Calendar.DAY_OF_MONTH, 1);
    }/*w  ww .  j a v  a2  s .c om*/
    return days;
}

From source file:Main.java

/**
 * Returns Date object of the next day of d.
 * @return/*from w ww.  j ava  2 s. com*/
 */
public static Date getNextDay(Date d) {
    Calendar c = Calendar.getInstance();
    if (d != null)
        c.setTime(d);
    c.set(Calendar.HOUR_OF_DAY, 0);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.SECOND, 0);
    c.set(Calendar.MILLISECOND, 0);
    c.add(Calendar.DAY_OF_MONTH, 1);
    return c.getTime();
}