Java Calendar Code implementation

Source: Internet
Author: User

 

    

  

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.