Java-based Visual calendar exercises
Java-based Visual calendar exercises
20150802
After reading the Date, DateFormat, Calendar, and GregorianCalendar classes, make a small Calendar to practice the corresponding methods and attributes and enhance the memory:
Description: enter a date in the specified format, for example, 2001-12-25. The calendar of the current month is output.
Ideas:
1. input the string (str) in the specified format from the keyboard)
2. Convert the string into a Date using the format conversion class (date Date = format. parse (str ))
3. Set the date to the calendar class (calendar. setTime (date ))
4. Obtain the number of weeks on the first day of the month (calendar. get (Calendar. DAY_OF_WEEK ))
5. Obtain the number of days of the current month (calendar. getActualMaximum (Calendar. DATE ))
6. output by format
Code:
Package TestDate; import java. text. dateFormat; import java. text. parseException; import java. text. simpleDateFormat; import java. util. calendar; import java. util. date; import java. util. gregorianCalendar; import java. util. principal; public class VisualCalendar {/*** visual calendar * @ param args */public static void main (String [] args) {System. out. println (enter the year, month, and day in the format of 2001-12-25) :); scanned scan = new partition (System. in); // input String str = scan in the specified format from the keyboard. nextLine (); // System. out. println (str); // defines the formatting class DateFormat = new SimpleDateFormat (yyyy-MM-dd) for a specific format ); try {// convert the string to the Date date = format in the corresponding format. parse (str); // System. out. println (date); Calendar calendar = new GregorianCalendar (); // sets the date to the calendar Class Object Calendar. setTime (date); // System. out. println (calendar. getTime (); // get the input day int day = calendar. get (Calendar. DATE); // System. out. println (day = + day); // set the day of the calendar Object to 1, so that you can calculate the number of days of the calendar month in the next step. set (Calendar. DATE, 1); // The day for obtaining this DATE is the day of the week int week = calendar. get (Calendar. DAY_OF_WEEK); // obtain the maximum number of days of the month. int maxDay = calendar. getActualMaximum (Calendar. DATE); // System. out. println (week = + week); // System. out. println (maxDay = + maxDay); System. out. println (May 15, 1234); int flag = 0; for (int I = 1; I
========================================================== ================================ Conclusion: ------------------- common methods of the GregorianCalendar class ---------------------------------------- getActualMaximum (Calendar ar. DATE) ---- this method is not good at understanding, but more memory get (Calendar. DAY_OF_WEEK) ---- This method obtains the current day, which is the day of the week. setTime (date) ---- this method can set the date object to a Calendar Object, then, you can call various calendar methods to solve the corresponding problems. The problem of format conversion between strings and dates and the details of months and weeks is better, for details, refer to the Calendar class and GregorianCalendar class of common Java classes.