Use the Date and SimpleDateFormat classes to represent the time, simpledateformat
In program development, we often need to process Date and time-related data. In this case, we can use the Date class in the java. util package. The main function of this class is to get the current time. Let's look at the usage of the Date class:
Date d = new Date (); // use the default constructor to create the Date object System. out. println (d); // output the Date object
Objects created using the default no-argument constructor of the Date Class represent the current time. We can directly output the Date object to display the current time. The display architecture is as follows:
Fri Apr 13:57:05 CST 2015
Fri stands for Friday, Apr stands for April, and 24 stands for the 24-day CST stands for China's standard time, that is, Beijing time, and UTC + 8.
From the above output, we found that the default time format is not very friendly (and we do not want to display it), so we use java. the SimpledDateFormat class in the text package shows its advantages. You can use SimpleDateFormat to format the date and time. For example, you can replace the date with the text in the specified format or convert the text to the date.
1. Use the format () method to convert a date to a text in the specified format
// Create a Date object to indicate the current time Date d = new Date (); // create a SimpleDateFormat object to specify the target format SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss "); // call the format () method, format the time, and convert it to String today = sdf in the specified format. format (d); // output the converted string System. out. println (today );
Output result: (now I can guess): 14:07:03
2. convert text to date using the parse () method
// Create a date String day = "April 24, 2015 14:13:50"; // create a SimpleDateFomat object, specify the date format of the string SimpleDateFormat df = new SimpleDateFormat ("yyyy MM dd HH: mm: ss"); // call the parse () method, convert string to Date d = df. parse (day); // output System. out. println ("current time:" + d );
Output result: Current Time: Fri Apr 24 14:13:50 CST 2015
Finally: Here is a demo of both:
Import java. text. parseException; import java. text. simpleDateFormat; import java. util. date; public class HelloWorld {public static void main (String [] args) throws ParseException {// use format () method: Convert the date to the text SimpleDateFormat sdf1 = new SimpleDateFormat ("MM dd, yyyy, HH, mm, ss seconds") in the specified format "); simpleDateFormat sdf2 = new SimpleDateFormat ("yyyy/MM/dd HH: mm"); SimpleDateFormat sdf3 = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss "); // create a Date object, which indicates the current time Date now = new Date (); // call the format () method, convert the Date to a string, and output the System. out. println (sdf1.format (now); System. out. println (sdf2.format (now); System. out. println (sdf3.format (now); // use the parse () method to convert the text to the date String d = "21:05:36 "; simpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); // call the parse () method to convert the string to Date date Date = sdf. parse (d); System. out. println (date );}}