Constants value in Calendar

TypeFieldSummary
static intAMIndicating the period of the day from midnight to just before noon.
static intAM_PMField number for get and set indicating whether the HOUR is before or after noon.
static intPMIndicating the period of the day from noon to just before midnight.

TypeFieldSummary
static intDAY_OF_MONTHFor get and set indicating the day of the month.
static intDAY_OF_WEEKFor get and set indicating the day of the week.
static intDAY_OF_WEEK_IN_MONTHFor get and set indicating the ordinal number of the day of the week within the current month.
static intDAY_OF_YEARFor get and set indicating the day number within the current year.
static intHOURFor get and set indicating the hour of the morning or afternoon.
static intHOUR_OF_DAYFor get and set indicating the hour of the day.
static intDATEFor get and set indicating the day of the month.
static intERAFor get and set indicating the era, e.g., AD or BC in the Julian calendar.
static intMILLISECONDFor get and set indicating the millisecond within the second.
static intMINUTEFor get and set indicating the minute within the hour.
static intMONTHFor get and set indicating the month.
static intSECONDFor get and set indicating the second within the minute.
static intWEEK_OF_MONTHFor get and set indicating the week number within the current month.
static intWEEK_OF_YEARFor get and set indicating the week number within the current year.
static intYEARFor get and set indicating the year.

TypeFieldSummary
static intJANUARYfirst month
static intFEBRUARYsecond month
static intMARCHthird month
static intAPRILfourth month
static intMAYfifth month
static intJUNEsixth month
static intJULYseventh month
static intAUGUSTeighth month
static intSEPTEMBERninth month
static intOCTOBERtenth month
static intNOVEMBEReleventh month
static intDECEMBERtwelfth month

TypeFieldSummary
static intMONDAYMonday.
static intTUESDAYTuesday.
static intWEDNESDAYWednesday.
static intTHURSDAYThursday.
static intFRIDAYFriday.
static intSATURDAYSaturday.
static intSUNDAYSunday.

TypeFieldSummary
static intLONGFor getDisplayName and getDisplayNames indicating a long name, such as "January".
static intSHORTFor getDisplayName and getDisplayNames indicating a short name, such as "Jan".

TypeFieldSummary
static intALL_STYLESFor getDisplayNames indicating names in all styles, such as "January" and "Jan".
static intDST_OFFSETField number for get and set indicating the daylight savings offset in milliseconds.
static intFIELD_COUNTThe number of distinct fields recognized by get and set.
static intUNDECIMBERValue of the MONTH field indicating the thirteenth month of the year.
static intZONE_OFFSETField number for get and set indicating the raw offset from GMT in milliseconds.

import java.util.Calendar;

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

    Calendar calendar = Calendar.getInstance();

    // Display current time and date information.
    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));

    // Set the time and date information and display it.
    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));
  }

}

The output:


Date: Oct 30 2010
Time: 9:35:29
Updated time: 10:29:22

Display Month of year


import java.util.Calendar;

public class Main {
  public static void main(String[] args) {
    Calendar now = Calendar.getInstance();
  
    String[] strMonths = new String[] { "Jan", "Feb", "Mar", "Apr", "May",
        "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
    System.out.println("Current month is : "
        + strMonths[now.get(Calendar.MONTH)]);
  }
}

The output:


Current month is : Oct

Calendar class automatically adjust the date or hour when adding or subtracting minutes


import java.util.Calendar;

public class Main {
  public static void main(String[] args) {
    Calendar now = Calendar.getInstance();
    System.out.println("Current time : ");
    System.out.println(now.get(Calendar.HOUR_OF_DAY) + ":"
        + now.get(Calendar.MINUTE) + ":" + now.get(Calendar.SECOND));

    // Calendar class automatically adjust the date or hour when adding or subtracting minutes
    now.add(Calendar.MINUTE, 200);

    System.out.println("After adding 200 minutes:");
    
    System.out.println(now.get(Calendar.HOUR_OF_DAY) + ":"
        + now.get(Calendar.MINUTE) + ":" + now.get(Calendar.SECOND));

  }
}

The output:


Current time : 
9:41:54
After adding 200 minutes:
13:1:54

Add or substract months to current date


import java.util.Calendar;

public class Main {
  public static void main(String[] args) {

    Calendar now = Calendar.getInstance();
    System.out.println("Now: ");
    System.out.println((now.get(Calendar.MONTH) + 1));
    System.out.println(now.get(Calendar.DATE));
    System.out.println(now.get(Calendar.YEAR));

    // add months to current date using Calendar.add method
    now.add(Calendar.MONTH, 1);
    System.out.println("adding one month : ");
    System.out.println((now.get(Calendar.MONTH) + 1));
    System.out.println(now.get(Calendar.DATE));
    System.out.println(now.get(Calendar.YEAR));

    // adding minus value
    now = Calendar.getInstance();
    now.add(Calendar.MONTH, -1);
    System.out.println("adding minus value: ");
    System.out.println((now.get(Calendar.MONTH) + 1));
    System.out.println(now.get(Calendar.DATE));
    System.out.println(now.get(Calendar.YEAR));
  }
}

The output:


Now: 
10
30
2010
adding one month : 
11
30
2010
adding minus value: 
9
30
2010

Calendar class automatically adjust the date when adding or subtracting months


import java.util.Calendar;

public class Main {
  public static void main(String[] args) {

    Calendar now = Calendar.getInstance();
    System.out.println("Now: ");
    System.out.println((now.get(Calendar.MONTH) + 1));
    System.out.println(now.get(Calendar.DATE));
    System.out.println(now.get(Calendar.YEAR));

    // adding 100 months to current date
    now.add(Calendar.MONTH, 100);
    System.out.println("adding one hundred month : ");
    System.out.println((now.get(Calendar.MONTH) + 1));
    System.out.println(now.get(Calendar.DATE));
    System.out.println(now.get(Calendar.YEAR));

  }
}

The output:


Now: 
10
30
2010
adding one hundred month : 
2
28
2019

Add or substract seconds to current time


import java.util.Calendar;

public class Main {
  public static void main(String[] args) {
    Calendar now = Calendar.getInstance();
    System.out.println("Now:");
    System.out.println(now.get(Calendar.MINUTE) + ":"
        + now.get(Calendar.SECOND));

    // add seconds to current date
    now.add(Calendar.SECOND, 500);
    System.out.println("adding 500 seconds : ");
    System.out.println(now.get(Calendar.MINUTE) + ":"
        + now.get(Calendar.SECOND));
    // adding minus value
    now = Calendar.getInstance();
    now.add(Calendar.SECOND, -500);
    System.out.println("adding -500 seconds: ");
    System.out.println(now.get(Calendar.MINUTE) + ":"
        + now.get(Calendar.SECOND));
  }
}

The output:


Now:
43:33
adding 500 seconds : 
51:53
adding -500 seconds: 
35:13

Getting current week of the month


import java.util.Calendar;

public class Main {
  public static void main(String[] args) {
    Calendar now = Calendar.getInstance();
    System.out.println("Now: ");
    System.out.println((now.get(Calendar.MONTH) + 1) + "-"
        + now.get(Calendar.DATE) + "-" + now.get(Calendar.YEAR));
    
    System.out.println("Current week of month is : "
        + now.get(Calendar.WEEK_OF_MONTH));
  }
}

The output:


Now: 
10-30-2010
Current week of month is : 5

Getting current week of the year


import java.util.Calendar;

public class Main {
  public static void main(String[] args) {
    Calendar now = Calendar.getInstance();
    
    System.out.println("Now: ");
    System.out.println((now.get(Calendar.MONTH) + 1) + "-"
        + now.get(Calendar.DATE) + "-" + now.get(Calendar.YEAR));
    System.out.println("Current week of year is : "
        + now.get(Calendar.WEEK_OF_YEAR));
    
  }
}

The output:


Now: 
10-30-2010
Current week of year is : 44
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.