Constants value in Calendar
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