System.out.println ("Please enter date (by format: 2030-3-10):");
In console input
String str= "2016-9-26";
Scanner scanner=new Scanner (system.in);
String Str=scanner.nextline ();
Convert the string to a date, remembering the abstract class DateFormat, so the new subclass
DateFormat dateformat=new SimpleDateFormat ("Yyyy-mm-dd");
Remember to java.util.Date when importing packages;
Date date=dateformat.parse (str);
Convert date to Calendar remember abstract class calendar
Calendar calendar = new GregorianCalendar ();
Calendar.settime (date);
/* To implement the functionality of a calendar
* 1. The first thing to know is how many days are there in this month
* 2. Know the number of each month on the day of the week, and know that the foreign and Chinese calendars are different, such as the first week of foreign countries is our Sunday
* We can convert the calendar from the console to the first day of the month, and we'll reset each calendar we get.
* So we can see how many spaces before the monthly number
*
* */
Get the largest number of days of the month
int Maxday=calendar.getactualmaximum (calendar.date);
SYSTEM.OUT.PRINTLN ("Maximum number of days Maxday:" +maxday);
Set the date to a number and find out the day of the Week and test
Calendar.set (calendar.date, 1);
int Day=calendar.get (calendar.date);
int Weekofday1=calendar.get (Calendar.day_of_week);
/*int Day=calendar.get (calendar.date);
System.out.println (day); */
System.out.println ("day \ t a \ Two \ three \ four \ t five \ t six");
To put a number on the day of the week, so the first space
for (int i=1;i<weekofday1;++i) {
System.out.print ("\ t");
}
for (int i=1;i<=maxday;++i) {
if (i==day) {
System.out.print (i+ "*" + "\ T");
}else{
System.out.print (i+ "\ t");
}
Note that print is not println.
Every time you arrive in Saturday, if you get the seventh day of the week, the week 6==calendar.day_of_week, you can change the line.
But note that we set the date to be number one, so the number of days to increase the date before the end of each cycle, or always a number
int Weekofday=calendar.get (Calendar.day_of_week);
if (weekofday==calendar.saturday) {
System.out.println ();
}
Calendar.add (calendar.date,1);
}
Java Calendar Code implementation