java-Basics-Common APIs (time-date class, Date,dateformat,calendar)

Source: Internet
Author: User
Tags dateformat locale string format time and date

Date

The class Date represents a specific instantaneous, accurate to milliseconds.

Millisecond concept: 1000 milliseconds = 1 seconds

0 points in milliseconds:

System.currenttimemillis () return value long type parameter

The millisecond value used to get the current date

The origin of Time: January 1, 1970 A.D., Midnight 0:00:00 The corresponding millisecond value is 0

Note: The time and date calculations must depend on the millisecond value

Continue to look at the description of the date class and find that date has more than one constructor, except that it is partially obsolete, but that there are non-obsolete constructors that convert millisecond values to date objects.

string ToString () converts this date type to the following form string down Mon dd hh:mm:ss zzz yyyy obsolete

Dateformat.format (date date)

long GetTime () returns the number of milliseconds represented by this date object since January 1, 1970 00:00:00 GMT

DateFormat

DateFormat is an abstract class of date/time formatting subclasses that formats and resolves dates or times in a language-independent manner.

DateFormat helps you to format and resolve dates for any locale. For months, weeks, and even calendar formats (lunar and Gregorian), their code is completely unrelated to the conventions of the locale.

DateFormat is an abstract class, and we need to use its subclass SimpleDateFormat to create objects.

Construction Method:

SimpleDateFormat (String pattern) constructs a date format symbol for a given pattern and default locale SimpleDateFormat

DateFormat class Method:

string format (date date) formats a date as a day/time string

  code Demo:  //  Create date formatting objects, you can specify a style when you get a formatted object  DateFormat df= new  simpledateformat ( " Span style= "COLOR: #800000" >yyyy-mm-dd   "); //  Date date = new  date (1607616000000l   = Df.format (date); System.  out . println (Str_time); //  December 11, 2020   

L DATEFORMAT the function of a class: You can convert a Date object to a string that matches the specified format, or you can turn a string that conforms to the specified format into a Date object.

Specify the format of the specific rules we can refer to the description of the SimpleDateFormat class, here to do a brief introduction, the rule is in a string, the following letters will be replaced with the corresponding time components, the remaining content is output as:

When it appearsy时,会将y替换成年

When it appearsM时,会将M替换成月

When it appearsd时,会将d替换成日

When it appearsH时,会将H替换成时

When it appearsm时,会将m替换成分

When it appearss时,会将s替换成秒

converts a string to a date    object = "December 11, 2020";     New SimpleDateFormat ("yyyy mm month DD day");     = Df.parse (str);     // the contents of the Date object are Fri Dec 00:00:00 CST 2020

The format method, which is used to convert a Date object to a string

the parse method, which is used to convert a string to date (the string must conform to the specified format if it is converted, otherwise it cannot be converted).

Date Parse (String source)

Calendar

The Calendar class, which appears after date, replaces many of the date's methods. This class encapsulates all possible time information into static member variables for easy access.

Calendar is abstract class, because of the language sensitivity, the Calendar class is not created directly when the object is created, but is created by a static method, the language sensitive content is processed, and then the subclass object is returned.

Static Calendar getinstance Use the default time zone and locale to get a calendar

Calendar C = calendar.getinstance ();-----------return current time

Calendar Class Common methods

abstract void Add (int field, int amount) Adds or subtract a specified amount of time based on a calendar rule for a given calendar field

int get (int filed) returns the value of the given calendar field

static Calendar getinstance () Use the default time zone and locale to get a calender

Date getTime () Returns a Date object that represents this Calendar time value (the millisecond offset from the previous period to the present)

void set (int filed, int value)

The beginning of the Western week is Sunday, China is Monday.

In the Calendar class, the month representation is represented by 0-11 for January-December.

The date is the size of the relationship, the time depends on, the greater the time.

1.1 Find out how many days you've been born

Ideas:

1. Get the number of days that the current time corresponds to

2. Get the number of days corresponding to your date of birth

3. Two time subtraction (current time days – days of birth)

Code implementation:

public static void Main (string[] args) {

Calendar my = Calendar.getinstance ();

Calendar C = calendar.getinstance ();

Set Birth date 1995-05-10

My.set (Calendar.year, 1995);

My.set (Calendar.month, 4);

My.set (Calendar.date, 10);

Get the number of days in a time

Calendar my = Calendar.getinstance ();

Calendar C = calendar.getinstance ();

Set Month Date 2020-01-01

My.set (calendar.year, 2020);

My.set (calendar.month, 0);

My.set (calendar.date, 1);

Get the number of days in a time

int day = C.get (calendar.date);

int myday = My.get (calendar.date);

System.out.println (Myday-day);

}

int day = C.get (calendar.date);

int myday = My.get (calendar.date);

System.out.println (Day-myday);

}

java-Basics-Common APIs (time-date class, Date,dateformat,calendar)

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.