Calendar Cal = calendar. getinstance ();
Java. Text. simpledateformat SDF = new simpledateformat ("YYYY ");
String cdate = SDF. Format (Cal. gettime ());
1. Use Java. util. Calendar
Calendar CA = calendar. getinstance ();
Date Now = Ca. gettime ();
CA. Add (calendar. day_of_month, 7 );
Date Fu = Ca. gettime ();
Fu is the date seven days later.
2. In addition, the current time can be obtained using new date (), but it seems that the date type is not recommended for new java. util. Date ()
A applet that displays the system date:
Import java. AWT .*;
Import java. Applet .*;
Import java. util .*;
Public class showtimeapplet extends applet {
Calendar calcurrent = calendar. getinstance ();
Textarea txadate = new textarea (5, 20 );
Int intday = calcurrent. Get (calendar. date );
Int intmonth = calcurrent. Get (calendar. month) + 1;
Int intyear = calcurrent. Get (calendar. year );
Public void Init (){
Add (txadate );
Txadate. append (intyear + "-" + intmonth + "-" + intday );
}
Java display current date and time
Import java. util .*;
Public class calendar
{
Public static void main (string [] ARGs)
{
Gregoriancalendar calendar = new gregoriancalendar ();
// Retrieve the current year, month, and day
Int year = calendar. Get (calendar. year );
// Add 1 to the value of the month to make it the expected month size (1 ~ December)
Int month = calendar. Get (calendar. month) + 1;
Int today = calendar. Get (calendar. day_of_month );
// Output the format samples of the current year, month, day, and week
System. Out. println ("today is" + year + "/" + month + "/" + today + "! ");
System. Out. println ();
System. Out. println ("Sun mon Tue wed Thu Fri Sat"); // There are two spaces in the middle
// Set to display the calendar from the first day of the current month and view the day of the week.
Calendar. Set (calendar. day_of_month, 1 );
Int weekday = calendar. Get (calendar. day_of_week );
// Align Day 1 with the corresponding week
For (INT I = calendar. Sunday; I system. Out. Print (""); // five spaces in the middle
Int day, month;
// Use the do-while loop to implement calendar output
Do {
Day = calendar. Get (calendar. day_of_month );
System. Out. Print (day );
/* If the date is today, an "*" is output to identify it. If the date is not today, the system determines whether it is less than 10,
Output Space. This is mainly used to align the format. */
If (Day = Today)
{
System. Out. Print ("*");
If (day <10) system. Out. Print ("");
}
Else if (day <10) system. Out. Print ("");
Else system. Out. Print ("");
Weekday = calendar. Get (calendar. day_of_week );
// According to the format requirements, line feed is required on Saturday. If no line feed is required, a certain space is output.
If (weekday = calendar. Saturday) system. Out. println ();
Else system. Out. Print ("");
Calendar. Add (calendar. day_of_week, 1 );
Month = calendar. Get (calendar. month) + 1;
}
While (month = month); // If month exceeds the current month, the loop is exited,
System. Out. println ();
System. Out. println ();
}
}