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:MainClass.java

public static void main(String args[]) {
    GregorianCalendar today = new GregorianCalendar();

    int todayMonth = today.get(Calendar.MONTH);

    int todayDayOfMonth = today.get(Calendar.DAY_OF_MONTH);

    int todayYear = today.get(Calendar.YEAR);

    int todayDayOfYear = today.get(Calendar.DAY_OF_YEAR);

    int numDays;//  w w  w .  j  a  va  2s  .  c  o  m
    if (todayMonth == 11 && todayDayOfMonth > 25) {
        GregorianCalendar xmas = new GregorianCalendar(todayYear + 1, 11, 25);
        int xmasDayOfYear = xmas.get(Calendar.DAY_OF_YEAR);
        numDays = xmasDayOfYear + 31 - todayDayOfMonth;
    } else {
        GregorianCalendar xmas = new GregorianCalendar(todayYear, 11, 25);
        int xmasDayOfYear = xmas.get(Calendar.DAY_OF_YEAR);
        numDays = xmasDayOfYear - todayDayOfYear;
    }

    System.out.println("The number of days till Christmas is " + numDays);
}

From source file:Main.java

public static void main(String[] args) {

    Date today = new Date();

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(today);/* ww w . ja va2  s. c  o m*/

    calendar.add(Calendar.MONTH, 1);
    calendar.set(Calendar.DAY_OF_MONTH, 1);
    calendar.add(Calendar.DATE, -1);

    Date lastDayOfMonth = calendar.getTime();

    DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    System.out.println("Today            : " + sdf.format(today));
    System.out.println("Last Day of Month: " + sdf.format(lastDayOfMonth));
}

From source file:Main.java

public static void main(String[] args) {
    int dayOfWeek = Calendar.SUNDAY;
    Calendar cal = new GregorianCalendar();
    cal.set(2015, 0, 1, 0, 0);//  w  w w .  ja  v  a2 s  .  c  om
    cal.set(Calendar.DAY_OF_WEEK, dayOfWeek);
    while (cal.get(Calendar.YEAR) == 2015) {
        System.out.println(cal.getTime());
        cal.add(Calendar.DAY_OF_MONTH, 7);
    }
}

From source file:Main.java

public static void main(String[] args) {
    GregorianCalendar gc = new GregorianCalendar();
    // current year value
    int year = gc.get(Calendar.YEAR);
    System.out.println(year);// w  ww.  j  a v  a 2  s. c  om
    // current month value
    int month = gc.get(Calendar.MONTH);
    System.out.println(month);
    // day of month
    int day = gc.get(Calendar.DAY_OF_MONTH);
    System.out.println(day);
    // hour value
    int hour = gc.get(Calendar.HOUR);
    System.out.println(hour);
    // minute value
    int minute = gc.get(Calendar.MINUTE);
    System.out.println(minute);
    // second values
    int second = gc.get(Calendar.SECOND);
    System.out.println(second);
}

From source file:Main.java

public static void main(String[] args) {
    Calendar cal = new GregorianCalendar(1999, 1, 1);
    Calendar now = new GregorianCalendar();
    int res = now.get(Calendar.YEAR) - cal.get(Calendar.YEAR);
    if ((cal.get(Calendar.MONTH) > now.get(Calendar.MONTH))
            || (cal.get(Calendar.MONTH) == now.get(Calendar.MONTH)
                    && cal.get(Calendar.DAY_OF_MONTH) > now.get(Calendar.DAY_OF_MONTH))) {
        res--;/*from  ww  w  .  java  2  s .  c  om*/
    }
    System.out.println(res);
}

From source file:GregCalDemo.java

public static void main(String[] av) {
    //+/* w  w w.j a  v  a2  s.  co  m*/
    GregorianCalendar d1 = new GregorianCalendar(1986, 04, 05); // May 5
    GregorianCalendar d2 = new GregorianCalendar(); // today
    Calendar d3 = Calendar.getInstance(); // today

    System.out.println("It was then " + d1.getTime());
    System.out.println("It is now " + d2.getTime());
    System.out.println("It is now " + d3.getTime());
    d3.set(Calendar.YEAR, 1915);
    d3.set(Calendar.MONTH, Calendar.APRIL);
    d3.set(Calendar.DAY_OF_MONTH, 12);
    System.out.println("D3 set to " + d3.getTime());
    //-
}

From source file:MainClass.java

public static void main(String[] a) {
    Calendar calendar = new GregorianCalendar();
    calendar.setTime(new Date());

    System.out.println("ERA: " + calendar.get(Calendar.ERA));
    System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
    System.out.println("MONTH: " + calendar.get(Calendar.MONTH));
    System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR));
    System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH));
    System.out.println("DATE: " + calendar.get(Calendar.DATE));
    System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
    System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));
    System.out.println("DAY_OF_WEEK_IN_MONTH: " + calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
    System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM));
    System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND));
    System.out.println("ZONE_OFFSET: " + (calendar.get(Calendar.ZONE_OFFSET) / (60 * 60 * 1000)));
    System.out.println("DST_OFFSET: " + (calendar.get(Calendar.DST_OFFSET) / (60 * 60 * 1000)));
}

From source file:com.pureinfo.srm.config.notice.TestTimer.java

public static void main(String[] args) throws ParseException {
    Calendar start = new GregorianCalendar();
    start.setTime(format.parse("18:40"));
    Calendar cal = new GregorianCalendar();
    start.set(Calendar.YEAR, cal.get(Calendar.YEAR));
    start.set(Calendar.MONTH, cal.get(Calendar.MONTH));
    start.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH));
    while (start.before(cal)) {
        start.setTimeInMillis(start.getTimeInMillis() + DateUtils.MILLIS_PER_MINUTE);
    }/*from   w  w  w. j  av a2  s.  co m*/
    new Timer().scheduleAtFixedRate(new test1(), start.getTime(), DateUtils.MILLIS_PER_MINUTE);
}

From source file:CalendarTest.java

public static void main(String[] args) {
    // construct d as current date
    GregorianCalendar d = new GregorianCalendar();

    int today = d.get(Calendar.DAY_OF_MONTH);
    int month = d.get(Calendar.MONTH);

    // set d to start date of the month
    d.set(Calendar.DAY_OF_MONTH, 1);

    int weekday = d.get(Calendar.DAY_OF_WEEK);

    // get first day of week (Sunday in the U.S.)
    int firstDayOfWeek = d.getFirstDayOfWeek();

    // determine the required indentation for the first line
    int indent = 0;
    while (weekday != firstDayOfWeek) {
        indent++;/*ww w.j a v a2s .c  o  m*/
        d.add(Calendar.DAY_OF_MONTH, -1);
        weekday = d.get(Calendar.DAY_OF_WEEK);
    }

    // print weekday names
    String[] weekdayNames = new DateFormatSymbols().getShortWeekdays();
    do {
        System.out.printf("%4s", weekdayNames[weekday]);
        d.add(Calendar.DAY_OF_MONTH, 1);
        weekday = d.get(Calendar.DAY_OF_WEEK);
    } while (weekday != firstDayOfWeek);
    System.out.println();

    for (int i = 1; i <= indent; i++)
        System.out.print("    ");

    d.set(Calendar.DAY_OF_MONTH, 1);
    do {
        // print day
        int day = d.get(Calendar.DAY_OF_MONTH);
        System.out.printf("%3d", day);

        // mark current day with *
        if (day == today)
            System.out.print("*");
        else
            System.out.print(" ");

        // advance d to the next day
        d.add(Calendar.DAY_OF_MONTH, 1);
        weekday = d.get(Calendar.DAY_OF_WEEK);

        // start a new line at the start of the week
        if (weekday == firstDayOfWeek)
            System.out.println();
    } while (d.get(Calendar.MONTH) == month);
    // the loop exits when d is day 1 of the next month

    // print final end of line if necessary
    if (weekday != firstDayOfWeek)
        System.out.println();
}

From source file:MainClass.java

public static void main(String argv[]) throws Exception {
    Calendar cc1 = Calendar.getInstance();
    Calendar cc2 = Calendar.getInstance();
    cc1.add(Calendar.DAY_OF_MONTH, 10);

    DateDiff(cc1.getTime(), cc2.getTime());

    java.util.Date d1 = makeDate("10/10/2000");
    java.util.Date d2 = makeDate("10/18/2000");
    DateDiff(d1, d2);/*from   w ww  .  j av  a 2 s .  c om*/

    java.util.Date d3 = makeDate("1/1/2000");
    java.util.Date d4 = makeDate("12/31/2000");
    int diff34 = diff(d3, d4);
    System.out.println("diff34=" + diff34);
}