Java Common class ———— date class

Source: Internet
Author: User

The date class is in the Java.util package.

First, function Description: Create a Date object, get time, format the output time.

Ii. object creation: 1. Objects created using the date class parameterless construction method can get local time. For example: Date nowtime=new date ();

2. Computer time "ad" set on January 1, 1970 0 O'Clock (GMT), can be based on this using the date class with parameters of the construction method:

Date (long time), to create an object,

For example: Date Date1=new date (1000);

Date Date2=new Date (-1000); \

If the local time to run the Java program is Beijing time zone, then the above date1 is January 01, 1970 08:00 01 seconds, Date2 is January 01, 1970 07:59 59 seconds;

Third, method: 1. You can use the system static method public long Currenttimemillis () to get the current time of the system, if the local time zone running the Java program is the Beijing time zone, this time is from January 1, 1970 08 o'clock to the current time the number of milliseconds traversed.

namely: System.currenttimemillis ();

The default order in which 2.Date objects represent time is week, month, day, hour, minute, second, year.

3. Date formatting: According to a habit to output time, such as the Order of time: Year Sunday or month Sunday hours seconds

Public SimpleDateFormat (String pattern);

Public String Format (date date);

First create an object with SimpleDateFormat, the parameter is the time format you require, and the pattern is composed of ordinary characters and some sequences of characters called format symbols.

The patter object then calls the format method to reformat the original time and returns the string, that is, the string returned by the format method is the sequence of characters that replaces the format character in the form with the corresponding time.

Use the above two sentences to format the time object:

For example: Date date=new date ();

SimpleDateFormat matter=new SimpleDateFormat ("" Now Time: ' Y year m D Day H M min S second ");

Stirng Time=matter.format (nowtime);

The results of the program run are based on the date and time of the day.

Four, the following is the date format and the result of being replaced:

    • G: Replace with the A.D., for example AD or "ad";
    • Y: Year replaced with 2 digits, for example: 98;
    • M: Replace with the month in the year, for example: July, July, 7;
    • W: Replace with the number of weeks in the year, for example: 28;
    • W: Replace with the number of weeks in the month, for example: 3;
    • D: Replace with the number of days in the year, for example: 189;
    • D: Replace with the number of days in the month, for example: 26;
    • F: Replace with the month of the week, for example: 4;
    • E: Replace with days of the week, such as Tuesday, Tue, Tuesday;
    • A: Replace with am/pm tag, for example PM;
    • H: Replace with the number of hours in the day (0~23), for example 0;
    • K: Replace with the number of hours in a day (1~24), for example 11;
    • K: Replace with the number of hours in am/pm (0~11), for example: 11;
    • H: Replace with the number of hours in am/pm (1~12), for example: 12.
    • M: Replace with the number of minutes in the hour, for example: 39;
    • S: The number of seconds to replace with the number of minutes, for example 49;
    • S: Replace with the number of milliseconds, for example 290;
    • Z: Replace with time zone, for example CST;

Top

Java Common class ———— date class

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.