Example usage for java.util Calendar MONTH

List of usage examples for java.util Calendar MONTH

Introduction

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

Prototype

int MONTH

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

Click Source Link

Document

Field number for get and set indicating the month.

Usage

From source file:Main.java

public static void main(String[] args) {

    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:00Z");
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.YEAR, 2015);
    cal.set(Calendar.MONTH, Calendar.SEPTEMBER);
    cal.set(Calendar.DATE, 1);/*from w  w w .  j a va  2s.c  o  m*/
    cal.set(Calendar.HOUR_OF_DAY, 12);
    cal.set(Calendar.MINUTE, 15);
    Date date = cal.getTime();
    System.out.println(date);
    TimeZone tz = TimeZone.getTimeZone("IST");
    dateFormat.setTimeZone(tz);
    String actual = dateFormat.format(date);

    System.out.println(actual);
}

From source file:Main.java

public static void main(String[] args) {

    Calendar cal = Calendar.getInstance();

    // return the minimum value that the year field could have
    int i = cal.getActualMinimum(Calendar.YEAR);
    System.out.println("Minimum Year :" + i);

    // returns the minimum value that the month field could have
    int a = cal.getActualMinimum(Calendar.MONTH);
    System.out.println("Minimum month :" + a);
}

From source file:Main.java

public static void main(String[] args) {

    Calendar cal = Calendar.getInstance();

    System.out.println("The current date is : " + cal.getTime());

    // add 20 days to the calendar
    cal.add(Calendar.DATE, 20);//w ww.ja  v a 2s . c o m
    System.out.println("20 days later: " + cal.getTime());

    // subtract 2 months from the calendar
    cal.add(Calendar.MONTH, -2);
    System.out.println("2 months ago: " + cal.getTime());

    // subtract 5 year from the calendar
    cal.add(Calendar.YEAR, -5);
    System.out.println("5 years ago: " + cal.getTime());
}

From source file:Main.java

public static void main(String[] args) {
    Calendar now = Calendar.getInstance();

    System.out.println("Current week of month is : " + now.get(Calendar.WEEK_OF_MONTH));
    System.out.println("Current week of year is : " + now.get(Calendar.WEEK_OF_YEAR));

    now.add(Calendar.WEEK_OF_MONTH, 1);

    System.out.println("date after one year : " + (now.get(Calendar.MONTH) + 1) + "-" + now.get(Calendar.DATE)
            + "-" + now.get(Calendar.YEAR));
}

From source file:GregorianCalendarDemo.java

public static void main(String args[]) {
    String months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
    int year;/*from   ww  w  . j  a v a 2s. co  m*/

    GregorianCalendar gcalendar = new GregorianCalendar();

    System.out.print("Date: ");
    System.out.print(months[gcalendar.get(Calendar.MONTH)]);
    System.out.print(" " + gcalendar.get(Calendar.DATE) + " ");
    System.out.println(year = gcalendar.get(Calendar.YEAR));

    System.out.print("Time: ");
    System.out.print(gcalendar.get(Calendar.HOUR) + ":");
    System.out.print(gcalendar.get(Calendar.MINUTE) + ":");
    System.out.println(gcalendar.get(Calendar.SECOND));

    if (gcalendar.isLeapYear(year)) {
        System.out.println("The current year is a leap year");
    } else {
        System.out.println("The current year is not a leap year");
    }
}

From source file:Main.java

public static void main(String[] args) {
    Calendar calendar = Calendar.getInstance();

    // 15-04-2012
    calendar.set(Calendar.DAY_OF_MONTH, 15);
    calendar.set(Calendar.YEAR, 2012);
    calendar.set(Calendar.HOUR, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MONTH, 3);
    Date start = calendar.getTime();

    // 15-06-2012
    calendar.set(Calendar.MONTH, 5);
    Date end = calendar.getTime();

    calendar.setTime(start);/*from  w  ww  . j  a v a  2s. c o m*/
    Date d = null;
    while ((d = calendar.getTime()).before(end) || d.equals(end)) {
        int day = calendar.get(Calendar.DAY_OF_WEEK);
        if (day != Calendar.SATURDAY && day != Calendar.SUNDAY) {
            System.out.println(d);
        }
        calendar.add(Calendar.DAY_OF_MONTH, 1);
    }
}

From source file:MainClass.java

public static void main(String args[]) {
    String months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };

    Calendar calendar = Calendar.getInstance();

    System.out.print("Date: ");
    System.out.print(months[calendar.get(Calendar.MONTH)]);
    System.out.print(" " + calendar.get(Calendar.DATE) + " ");
    System.out.println(calendar.get(Calendar.YEAR));

    System.out.print("Time: ");
    System.out.print(calendar.get(Calendar.HOUR) + ":");
    System.out.print(calendar.get(Calendar.MINUTE) + ":");
    System.out.println(calendar.get(Calendar.SECOND));

    calendar.set(Calendar.HOUR, 10);
    calendar.set(Calendar.MINUTE, 29);
    calendar.set(Calendar.SECOND, 22);

    System.out.print("Updated time: ");
    System.out.print(calendar.get(Calendar.HOUR) + ":");
    System.out.print(calendar.get(Calendar.MINUTE) + ":");
    System.out.println(calendar.get(Calendar.SECOND));
}

From source file:MainClass.java

public static void main(String args[]) {
    GregorianCalendar gc = new GregorianCalendar();
    int year = gc.get(Calendar.YEAR);
    System.out.println(year);/*from w w w  . j av  a2 s .  c  om*/
    System.out.println(gc.isLeapYear(year));
    System.out.println("Month = " + gc.get(Calendar.MONTH));
    System.out.println("Week of year = " + gc.get(Calendar.WEEK_OF_YEAR));
    System.out.println("Week of month = " + gc.get(Calendar.WEEK_OF_MONTH));
    System.out.println("Day of year = " + gc.get(Calendar.DAY_OF_YEAR));
    System.out.println("Day of week = " + gc.get(Calendar.DAY_OF_WEEK));
}

From source file:MainClass.java

public static void main(String[] args) {
    JFrame frame = new JFrame();

    Calendar now = Calendar.getInstance();
    Calendar earliest = (Calendar) now.clone();
    earliest.add(Calendar.MONTH, -6);
    Calendar latest = (Calendar) now.clone();
    latest.add(Calendar.MONTH, 6);
    SpinnerModel model = new SpinnerDateModel(now.getTime(), earliest.getTime(), latest.getTime(),
            Calendar.WEEK_OF_YEAR);
    final JSpinner spinner = new JSpinner(model);

    model.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
            System.out.println(((SpinnerDateModel) e.getSource()).getDate());
        }//from  www.j  a  va 2s. c o m
    });

    frame.getContentPane().add("North", new JLabel("Choose a week"));
    frame.getContentPane().add("Center", spinner);
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Calendar start = Calendar.getInstance();
    start.set(2015, 1, 21, 11, 1, 11);//from   ww w  .  j a  v  a 2s.com
    Calendar end = Calendar.getInstance();

    Calendar clone = (Calendar) start.clone();
    int year = elapsed(clone, end, Calendar.YEAR);
    System.out.println(year + " year");
    clone.add(Calendar.YEAR, year);
    int month = elapsed(clone, end, Calendar.MONTH);
    clone.add(Calendar.MONTH, month);
    System.out.println(month + " month");

    int date = elapsed(clone, end, Calendar.DATE);
    clone.add(Calendar.DATE, date);
    System.out.println(date + " day");

    int hour = (int) (end.getTimeInMillis() - clone.getTimeInMillis()) / 3600000;
    clone.add(Calendar.HOUR, hour);
    System.out.println(hour + " hour");

    int minute = (int) (end.getTimeInMillis() - clone.getTimeInMillis()) / 60000;
    clone.add(Calendar.MINUTE, minute);
    System.out.println(minute + " minute");

    int millis = (int) (end.getTimeInMillis() - clone.getTimeInMillis()) / 1000;
    System.out.println(millis + " millis");

}