The Calendar class is an abstract class, which provides us with functions related to date calculation, such as display and calculation of year, month, day, hour, minute and second.
GregorianCalendar is a specific subclass of Calendar and provides a standard calendar system for most countries in the world.
Rookie minefield
Notice the representation of the month, JANUARY is 0, FEBRUARY is 1, and so on, December is 11. Because most people are used to using words instead of numbers to represent months, the program may be easier to read. The parent class Calendar uses constants to represent months: JANUARY, FEBRUARY, and so on.
Use of the GregorianCalendar and Calendar classes:
import java.util.*; public class TestCalendar { public static void main(String[] args) { // Get related date elements GregorianCalendar calendar = new GregorianCalendar(2999, 10, 9, 22, 10, 50); int year = calendar.get(Calendar.YEAR); // Print: 1999 int month = calendar.get(Calendar.MONTH); // Print: 10 int day = calendar.get(Calendar.DAY_OF_MONTH); // Print: 9 int day2 = calendar.get(Calendar.DATE); // Print: 9 // Day: synonymous with Calendar.DATE and calendar.day of month int date = calendar.get(Calendar.DAY_OF_WEEK); // Print: 3 // Today is: 1-7. Sunday is 1, Monday is 2,... Saturday is 7. System.out.println(year); System.out.println(month); System.out.println(day); System.out.println(day2); System.out.println(date); // Setting date GregorianCalendar calendar2 = new GregorianCalendar(); calendar2.set(Calendar.YEAR, 2999); calendar2.set(Calendar.MONTH, Calendar.FEBRUARY); // Months: 0-11 calendar2.set(Calendar.DATE, 3); calendar2.set(Calendar.HOUR_OF_DAY, 10); calendar2.set(Calendar.MINUTE, 20); calendar2.set(Calendar.SECOND, 23); printCalendar(calendar2); // Date calculation GregorianCalendar calendar3 = new GregorianCalendar(2999, 10, 9, 22, 10, 50); calendar3.add(Calendar.MONTH, -7); // Month decreased by 7 calendar3.add(Calendar.DATE, 7); // Increase 7 days printCalendar(calendar3); // Calendar object and time object conversion Date d = calendar3.getTime(); GregorianCalendar calendar4 = new GregorianCalendar(); calendar4.setTime(new Date()); long g = System.currentTimeMillis(); } static void printCalendar(Calendar calendar) { int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH) + 1; int day = calendar.get(Calendar.DAY_OF_MONTH); int date = calendar.get(Calendar.DAY_OF_WEEK) - 1; // What day is it String week = "" + ((date == 0) ? "day" : date); int hour = calendar.get(Calendar.HOUR); int minute = calendar.get(Calendar.MINUTE); int second = calendar.get(Calendar.SECOND); System.out.printf("%d year%d month%d day,week%s %d:%d:%d\n", year, month, day, week, hour, minute, second); } }
The execution result is as shown in the figure:
Write a program to print the calendar of the current month by using the GregorianCalendar class. Today's date is 2017-05-18, as shown in Figure 8-19
Preparation of visual calendar:
import java.text.ParseException; import java.util.Calendar; import java.util.GregorianCalendar; import java.util.Scanner; public class TestCalendar2 { public static void main(String[] args) throws ParseException { System.out.println("Please enter the date (Format: 2010-3-3): "); Scanner scanner = new Scanner(System.in); String dateString = scanner.nextLine(); // 2010-3-1 // Convert the input string to date class System.out.println("The date you just entered is:" + dateString); String[] str = dateString.split("-"); int year = Integer.parseInt(str[0]); int month = new Integer(str[1]); int day = new Integer(str[2]); Calendar c = new GregorianCalendar(year, month - 1, day); // Month:0-11 // You can add another way: convert string to Date object through SImpleDateFormat, //Then convert the Date object to a Date class // SimpleDateFormat sdfDateFormat = new SimpleDateFormat("yyyy-MM-dd"); // Date date = sdfDateFormat.parse(dateString); // Calendar c = new GregorianCalendar(); // c.setTime(date); // int day = c.get(Calendar.DATE); c.set(Calendar.DATE, 1); int dow = c.get(Calendar.DAY_OF_WEEK); // week:1-7 System.out.println("day\t One\t Two\t Three\t Four\t Five\t Six"); for (int i = 0; i < dow - 1; i++) { System.out.print("\t"); } int maxDate = c.getActualMaximum(Calendar.DATE); // System.out.println("maxDate:"+maxDate); for (int i = 1; i <= maxDate; i++) { StringBuilder sBuilder = new StringBuilder(); if (c.get(Calendar.DATE) == day) { sBuilder.append(c.get(Calendar.DATE) + "*\t"); } else { sBuilder.append(c.get(Calendar.DATE) + "\t"); } System.out.print(sBuilder); // System.out.print(c.get(Calendar.DATE)+ // ((c.get(Calendar.DATE)==day)?"*":"")+"\t"); if (c.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) { System.out.print("\n"); } c.add(Calendar.DATE, 1); } } }
Operation effect: