Date is often required during development, and the code is repeated every time the date is obtained. Therefore, since the Code is repeated, it can be defined as a class to facilitate repeated calls, however, you must pay special attention to the following points during operations:
If the month is September, 09 is displayed, but 09 is displayed, the number is ignored to 0.
1. Calendar operations (obtain the date and time, Chinese date and time stamp)
In addition to the date retrieved, obtaining a timestamp is also a common operation, for example, the following date:
11:25:34. 953
Time stamp: 20090116112534953
[Java] view plaincopy import java. util. *; // required toolkit for import
Class DateTime {// you can use this class to obtain the date and time.
Private Calendar calendar = null; // declare a Calendar Object and obtain the time
Public DateTime () {// directly instantiate the object in the constructor
This. calendar = new GregorianCalendar ();
}
Public String getDate () {// get a date in the format of yyyy-MM-dd HH: mm: ss. SSS.
// Use StringBuffer to improve performance because the program needs to modify strings frequently
StringBuffer buf = new StringBuffer (); www.2cto.com
Buf. append (calendar. get (Calendar. YEAR). append ("-"); // Add YEAR
Buf. append (this. addZero (calendar. get (Calendar. MONTH) + 1, 2). append ("-"); // Add a MONTH
Buf. append (this. addZero (calendar. get (Calendar. DAY_OF_MONTH), 2). append (""); // get the day
Buf. append (this. addZero (calendar. get (Calendar. HOUR_OF_DAY), 2). append (":"); // get
Buf. append (this. addZero (calendar. get (Calendar. MINUTE), 2). append (":");
Buf. append (this. addZero (calendar. get (Calendar. SECOND), 2). append (".");
Buf. append (this. addZero (calendar. get (Calendar. MILLISECOND), 3 ));
Return buf. toString ();
}
Public String getDateComplete () {// get a date in the format of yyyy MM dd HH mm min ss s SSS Ms
// Use StringBuffer to improve performance because the program needs to modify strings frequently
StringBuffer buf = new StringBuffer ();
Buf. append (calendar. get (Calendar. YEAR). append ("YEAR"); // Add YEAR
Buf. append (this. addZero (calendar. get (Calendar. MONTH) + 1, 2). append ("MONTH"); // Add MONTH
Buf. append (this. addZero (calendar. get (Calendar. DAY_OF_MONTH), 2). append ("day"); // get day
Buf. append (this. addZero (calendar. get (Calendar. HOUR_OF_DAY), 2). append ("when"); // when obtaining
Buf. append (this. addZero (calendar. get (Calendar. MINUTE), 2). append ("points"); // get points
Buf. append (this. addZero (calendar. get (Calendar. SECOND), 2). append ("seconds"); // get seconds
Buf. append (this. addZero (calendar. get (Calendar. MILLISECOND), 3). append ("MILLISECOND"); // get MILLISECOND
Return buf. toString ();
}
Public String getTimeStamp () {// get a timestamp
// Use StringBuffer to improve performance because the program needs to modify strings frequently
StringBuffer buf = new StringBuffer ();
Buf. append (calendar. get (Calendar. YEAR); // Add YEAR
Buf. append (this. addZero (calendar. get (Calendar. MONTH) + 1, 2); // Add a MONTH
Buf. append (this. addZero (calendar. get (Calendar. DAY_OF_MONTH), 2); // get the day
Buf. append (this. addZero (calendar. get (Calendar. HOUR_OF_DAY), 2); // when obtaining
Buf. append (this. addZero (calendar. get (Calendar. MINUTE), 2); // get the score
Buf. append (this. addZero (calendar. get (Calendar. SECOND), 2); // obtain the SECOND
Buf. append (this. addZero (calendar. get (Calendar. MILLISECOND), 3); // get the MILLISECOND
Return buf. toString ();
}
// Considering that there is a leading 0 in the date, add the zero-padding method here.
Private String addZero (int num, int len ){
StringBuffer s = new StringBuffer ();
S. append (num );
While (s. length () <len) {// If the length is insufficient, add 0
S. insert (0, "0"); // Add 0 at the first position
}
Return s. toString ();
}
};
Public class DateDemo06 {
Public static void main (String args []) {
DateTime dt = new DateTime ();
System. out. println ("System date:" + dt. getDate ());
System. out. println ("Chinese Date:" + dt. getDateComplete ());
System. out. println ("timestamp:" + dt. getTimeStamp ());
}
};
2. SimpleDate-based operations
Java. util. date is a complete Date. There is a method in the SimpleDateFormat class that can be reformatted for Date, if a date object that represents the current date is formatted using a template specified by the SimpleDateFormat class, it is very convenient to get the time.
[Java] import java. util. *; // required toolkit for import
Import java. text. *; // import the package where SimpleDateFormat is located
Class DateTime {// you can use this class to obtain the date and time.
Private SimpleDateFormat sdf = null; // declare the SimpleDateFormat object
Public String getDate () {// get a date in the format of yyyy-MM-dd HH: mm: ss. SSS.
This. sdf = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss. SSS ");
Return this. sdf. format (new Date (); // format the current Date.
}
Public String getDateComplete () {// get a date in the format of yyyy MM dd HH mm min ss s SSS Ms
This. sdf = new SimpleDateFormat ("MM minute ss second SSS millisecond on mm dd, yyyy ");
Return this. sdf. format (new Date (); // format the current Date.
}
Public String getTimeStamp () {// get a timestamp
This. sdf = new SimpleDateFormat ("yyyyMMddHHmmssSSS ");
Return this. sdf. format (new Date (); // format the current Date.
}
};
Public class DateDemo07 {
Public static void main (String args []) {
DateTime dt = new DateTime ();
System. out. println ("System date:" + dt. getDate ());
System. out. println ("Chinese Date:" + dt. getDateComplete ());
System. out. println ("timestamp:" + dt. getTimeStamp ());
}
};
Summary:
The Code shows that using SimpleDateFormat directly to obtain the time is more convenient than using the Calendar class, and the zero-padding operation is not required. Therefore, if you need to obtain a date during development, the SimpleDateFormat class is basically used for operations. The above operation code is a simple method to get the date.