Type | Field | Summary |
---|---|---|
static int | AM | Indicating the period of the day from midnight to just before noon. |
static int | AM_PM | Field number for get and set indicating whether the HOUR is before or after noon. |
static int | PM | Indicating the period of the day from noon to just before midnight. |
Type | Field | Summary |
---|---|---|
static int | DAY_OF_MONTH | For get and set indicating the day of the month. |
static int | DAY_OF_WEEK | For get and set indicating the day of the week. |
static int | DAY_OF_WEEK_IN_MONTH | For get and set indicating the ordinal number of the day of the week within the current month. |
static int | DAY_OF_YEAR | For get and set indicating the day number within the current year. |
static int | HOUR | For get and set indicating the hour of the morning or afternoon. |
static int | HOUR_OF_DAY | For get and set indicating the hour of the day. |
static int | DATE | For get and set indicating the day of the month. |
static int | ERA | For get and set indicating the era, e.g., AD or BC in the Julian calendar. |
static int | MILLISECOND | For get and set indicating the millisecond within the second. |
static int | MINUTE | For get and set indicating the minute within the hour. |
static int | MONTH | For get and set indicating the month. |
static int | SECOND | For get and set indicating the second within the minute. |
static int | WEEK_OF_MONTH | For get and set indicating the week number within the current month. |
static int | WEEK_OF_YEAR | For get and set indicating the week number within the current year. |
static int | YEAR | For get and set indicating the year. |
Type | Field | Summary |
---|---|---|
static int | JANUARY | first month |
static int | FEBRUARY | second month |
static int | MARCH | third month |
static int | APRIL | fourth month |
static int | MAY | fifth month |
static int | JUNE | sixth month |
static int | JULY | seventh month |
static int | AUGUST | eighth month |
static int | SEPTEMBER | ninth month |
static int | OCTOBER | tenth month |
static int | NOVEMBER | eleventh month |
static int | DECEMBER | twelfth month |
Type | Field | Summary |
---|---|---|
static int | MONDAY | Monday. |
static int | TUESDAY | Tuesday. |
static int | WEDNESDAY | Wednesday. |
static int | THURSDAY | Thursday. |
static int | FRIDAY | Friday. |
static int | SATURDAY | Saturday. |
static int | SUNDAY | Sunday. |
Type | Field | Summary |
---|---|---|
static int | LONG | For getDisplayName and getDisplayNames indicating a long name, such as "January". |
static int | SHORT | For getDisplayName and getDisplayNames indicating a short name, such as "Jan". |
Type | Field | Summary |
---|---|---|
static int | ALL_STYLES | For getDisplayNames indicating names in all styles, such as "January" and "Jan". |
static int | DST_OFFSET | Field number for get and set indicating the daylight savings offset in milliseconds. |
static int | FIELD_COUNT | The number of distinct fields recognized by get and set. |
static int | UNDECIMBER | Value of the MONTH field indicating the thirteenth month of the year. |
static int | ZONE_OFFSET | Field 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. |