Java print calendar

Source: Internet
Author: User

Source code:


[Java]
Import java. util. Calendar;
Import java. util. Date;
Import java. util. GregorianCalendar;
 
Public class MyDate {
Public static void main (String [] args ){
MyDate date = new MyDate ();
Date. myCalendar ();
}
// Calendar Implementation Method
Public void myCalendar (){
GregorianCalendar now = new GregorianCalendar ();
// Obtain a Date object
Date date = new Date ();
// Print the current time
System. out. println (date. toString ());
// Set the current time
Now. setTime (date );
// Obtain the current day from the date
Int toDay = now. get (Calendar. DAY_OF_MONTH );
// Obtain the current month from the date
Int month = now. get (Calendar. MONTH );
// Set the date of now to 1
Now. set (Calendar. DAY_OF_MONTH, 1 );
// Get now as the day of the week
Int week = now. get (Calendar. DAY_OF_WEEK );
// Print the calendar Header
System. out. println ("day \ t one \ t Two \ t three \ t four \ t five \ t six ");
// Print the space before the current date
For (int I = Calendar. SUNDAY; I <week; I ++ ){
System. out. print ("\ t ");
}
// Print the calendar subject
While (now. get (Calendar. MONTH) = month ){
Int day = now. get (Calendar. DAY_OF_MONTH );
// Alignment the output calendar with a space less than 10
If (day <10 ){
// If it is the current date, add the mark
If (day = toDay ){
System. out. print ("▲" + day +" ▲\ t ");
} Else {
System. out. print ("" + day + "\ t ");
}
} Else {
// If it is the current date, add the mark
If (day = toDay ){
System. out. print ("▲" + day +" ▲\ t ");
} Else {
System. out. print ("" + day + "\ t ");
}
}
// Line feed for Saturday
If (week = Calendar. SATURDAY ){
System. out. println ();
}
// After each output date, add the date to one day
Now. add (Calendar. DAY_OF_MONTH, 1 );
// Obtain the day of the week again
Week = now. get (Calendar. DAY_OF_WEEK );
}
}
}

Import java. util. Calendar;
Import java. util. Date;
Import java. util. GregorianCalendar;

Public class MyDate {
Public static void main (String [] args ){
MyDate date = new MyDate ();
Date. myCalendar ();
}
// Calendar Implementation Method
Public void myCalendar (){
GregorianCalendar now = new GregorianCalendar ();
// Obtain a Date object
Date date = new Date ();
// Print the current time
System. out. println (date. toString ());
// Set the current time
Now. setTime (date );
// Obtain the current day from the date
Int toDay = now. get (Calendar. DAY_OF_MONTH );
// Obtain the current month from the date
Int month = now. get (Calendar. MONTH );
// Set the date of now to 1
Now. set (Calendar. DAY_OF_MONTH, 1 );
// Get now as the day of the week
Int week = now. get (Calendar. DAY_OF_WEEK );
// Print the calendar Header
System. out. println ("day \ t one \ t Two \ t three \ t four \ t five \ t six ");
// Print the space before the current date
For (int I = Calendar. SUNDAY; I <week; I ++ ){
System. out. print ("\ t ");
}
// Print the calendar subject
While (now. get (Calendar. MONTH) = month ){
Int day = now. get (Calendar. DAY_OF_MONTH );
// Alignment the output calendar with a space less than 10
If (day <10 ){
// If it is the current date, add the mark
If (day = toDay ){
System. out. print ("▲" + day +" ▲\ t ");
} Else {
System. out. print ("" + day + "\ t ");
}
} Else {
// If it is the current date, add the mark
If (day = toDay ){
System. out. print ("▲" + day +" ▲\ t ");
} Else {
System. out. print ("" + day + "\ t ");
}
}
// Line feed for Saturday
If (week = Calendar. SATURDAY ){
System. out. println ();
}
// After each output date, add the date to one day
Now. add (Calendar. DAY_OF_MONTH, 1 );
// Obtain the day of the week again
Week = now. get (Calendar. DAY_OF_WEEK );
}
}
}
Output result:

 

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.