Package com. zyx. date;
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. collections;
/** * Visualized calendar Program * @ Author lenovo * */ Public class VisualCalendar {
Public static void main (String [] args ){ System. out. println ("Enter the date in the format of" YYYY )"); /* Get user input */ Required bytes = new bytes (System. in ); String toDay = response. nextLine (); DateFormat df = new SimpleDateFormat ("yyyy-MM-dd "); Date date = null; Try { Date = df. parse (toDay); // convert string date to date Calendar calendar = new GregorianCalendar (); Calendar. setTime (date); // convert a date to a calendar Int day = calendar. get (Calendar. DATE); // get the day of the current DATE Calendar. set (Calendar. DATE, 1); // obtain the first day of the DATE. Int begin = calendar. get (Calendar. DAY_OF_WEEK); // obtain the day of the week on the first day of the date. Int end = calendar. getActualMaximum (Calendar. DATE); // obtain the last day of the DATE. System. out. println ("day \ t one \ t Two \ t three \ t four \ t five \ t six "); /* Display the empty week before the first day */ For (int I = 1; I <begin; I ++ ){ System. out. print ("\ t "); } /* Display all dates */ For (int I = 1; I <= end; I ++ ){ If (day = I) {// use * to identify the input date System. out. print (I + "* \ t "); } Else { System. out. print (I + "\ t "); } Int week = calendar. get (Calendar. DAY_OF_WEEK); // obtain the day of the week. If (week = Calendar. SATURDAY) {// line feed on SATURDAY System. out. println (); } Calendar. add (Calendar. DATE, 1); // add one day to the calendar } } Catch (ParseException e ){ System. out. println ("Please input according to the format "); } } } |