Java date data type (non-original)

Source: Internet
Author: User
Tags dateformat
In Java data types, date data types are often used to deal with some daily application problems.

However, many people do not really understand the date data type in Java data types.

Generally, some data types are not necessarily used here. There are often various data types that are constantly changing.

1. directly create the date data type instance variable date and output it directly: 

/**
* Create a date variable. this is date: Sat dec 06 00:26:54 CST 2008
*ProgramPrint the current time directly during running
*/
Date = new date ();
System. Out. println ("this is date:" + date );

2. Output The directly instantiated date data in string format: 

**
* Instance method tostring () sat dec 06 00:26:54 CST 2008
* In fact, the effect is the same as the direct output of the program, except that the output parameter type is changed to the string type.
*/
String str_date = date. tostring ();
System. Out. println ("this is date _ STR:" + str_date );

3. format or filter the required data parameters using the dateformat () method: 

/**
* Method: dateformat. getinstance () Output: 08-12-6
* The dateformat () method is used to format the date and output it to the score of the current day.
*/
String str_date_1 = dateformat. getinstance (). Format (date );
System. Out. println ("this is str_date_1:" + str_date_1 );

4. Use simpledateformat () to format or filter the required data parameters: 

/**
* Method: simpledateformat () Output: 2008 12 06 00 26 54
* The simpledateformat () release method and dateformat () are similar to that used to specify the current second for a certain stage of the current date, for example, the instance.
*/
Simpledateformat time = new simpledateformat ("YYYY mm dd hh mm SS ");
System. Out. println ("this is simpledateformat:" + time. Format (date ));

5. format or filter the required data parameters using the messageformat () method: 

/**
* Method: messageformat () Output: 2008-12-06-00-26: 54: 2654
*/
String datetime = messageformat. Format (
"{0, date, yyyy-mm-dd-hh-MM: SS: Ms }",
New object [] {New java. SQL. Date (system. currenttimemillis ())});
System. Out. println ("This Is Message datetime:" + datetime );

/**
* Method: messageformat () Output: 2008-12-06-00-26: 54: 2654
*/
String datetime = messageformat. Format (
"{0, date, yyyy-mm-dd-hh-MM: SS: Ms }",
New object [] {New java. SQL. Date (system. currenttimemillis ())});
System. Out. println ("This Is Message datetime:" + datetime );

6. Use simpledateformat () to format or filter the required data parameters: 

/**
* Method: simpledateformat () Output: 2008-12-06
* Similar to the simpledateformat () method mentioned earlier, the principle is the same, but other additional conditions are used in the processing process.
*
*/
Simpledateformat datefm = new simpledateformat ("yyyy-mm-dd"); // format the current system date
String datetime_1 = datefm. Format (New java. util. Date ());
System. Out. println ("this is simpledateformat:" + datetime_1 );

7. Use the dateformat () method to format the required date parameter: 

/**
* Method: dateformat ()
* Different parameters controlled by the dateformat () method are used to display the current date and time.
*/
// Briefly display the current date and time
Dateformat required dateformat = dateformat. getdatetimeinstance (
Dateformat. Short, dateformat. Short );
System. Out. println (required dateformat. Format (date ));

// Precisely display the current date and time
Dateformat mediumdateformat = dateformat. getdatetimeinstance (
Dateformat. Medium, dateformat. Medium );
System. Out. println (mediumdateformat. Format (date ));

// Display the current date and time completely
Dateformat longdateformat = dateformat. getdatetimeinstance (
Dateformat. Long, dateformat. Long );
System. Out. println (longdateformat. Format (date ));

// All standardized outputs the current date and time.
Dateformat fulldateformat = dateformat. getdatetimeinstance (
Dateformat. Full, dateformat. Full );
System. Out. println (fulldateformat. Format (date ));

8. You can also use the gettime () method to obtain the time of the current date:

However, the data of the date type is calculated in seconds and starts from January 1, January 1, 1970. When declaring a data type, a large storage space is required to use long data type or data of the same storage type.

/**
* Method: gettime () Output: 1228494414199 this time is based on the number of milliseconds that have elapsed since January 1, January 1, 1970,
*/
Long str_get = date. gettime ();
System. Out. println ("this is gettime:" + str_get );

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.