Java Date Class Small summary

Source: Internet
Author: User
Tags dateformat

Usually in the development process is to deal with the date, basically any system can not be separated from the date, so I would like to summarize

Date,dateformat,simpledateformat,calendar

Date class:

Under the Java.util package, the class Date represents a specific moment, accurate to milliseconds. Starting with JDK 1.1, you should use the Calendar class to convert between date and Time fields, using the DateFormat class to format and parse date strings. The appropriate method in Date has been deprecated. So date is mostly used to generate time (online insights, I think).

(1) Date has two constructor methods:

(1) Date (): Allocates a Date object and initializes the object to indicate the time (in milliseconds) to allocate it.

(2) Date (long date) is basically useless to remember.

Eg:date date=new Date (); SYSTEM.OUT.PRINTLN (date); output: Tue June 20:51:24 CST 2015

(2) Date Common methods:

The Boolean after (date time) tests whether this date is after the specified date (

Nothing I feel.) Boolean before (date when) tests whether this date is before the specified date

int compareTo (date anotherdate) compares the order of two dates

DateFormat class:

Under the Java.text package, DateFormat is an abstract class of date/time formatting subclasses that formats and resolves dates or times in a language-independent manner. In many Java training videos, this class is an abstract class, so you cannot construct a method to instantiate it, and you can instantiate it with two static functions, Getdateinstance () and Getdatetimeinstance (). The difference between the two is that a return is a date, and a date + time is returned. At the same time, getdateinstance (int style), getdateinstance (int style, Locale Alocale) and other methods are very useful. About the style value:

Full: Longest for example: January 9, 2013 Wed long: longer lengths such as: January 9, 2013

MEDIUM: Length is shorter than short: Jan 9,2013: Full numbers, for example: 13/1/9

Eg:dateformat d1=dateformat.getdateinstance ();

DateFormat d2=dateformat.getdatetimeinstance ();

String S1=d1.format (New Date ());

String S2=d2.format (New Date ()); Output: 2015-6-16

System.out.println (S1); 2015-6-16 21:05:22

SYSTEM.OUT.PRINTLN (S2);

Eg1:dateformat d1=dateformat.getdateinstance (Dateformat.long); Output: June 16, 2015

3.

Under the Java.text package, it is a direct subclass of the DateFormat class, inheriting the DateFormat class. I understand the SimpleDateFormat class, it is more grounded than the Datef class, and you can give him a formal date and make changes. The main function of the SimpleDateFormat class is the conversion of the format between completion dates, and the following steps are required during the conversion: 1. Specify a template and, based on this template, take out the first all time numbers. 2. All time numbers will be saved using the date class. 3. Re-format all the time numbers. The template is the following table, note case sensitive

Date Template Describe
Years Y Represents year: YYYY
Month M Represents month: MM
Day D Mean Day: DD
When HH When represented: HH
Score of Mm Indication points: mm
Seconds Ss Represents seconds: ss
Milliseconds S MS: SSS

Eg:simpledateformat sdf=new SimpleDateFormat ("Y-year m-month D-day HH:mm:ss D"); It can also be "yyyy mm month DD Day"

System.out.println (Sdf.format (New Date ())); Output: June 16, 2015 21:39:10 167

4.

Under the Java.util package, the Calendar class is an abstract class that is a "specific moment" with a set of such things as "year",

Conversion between calendar fields, such as MONTH, Day_of_month, HOUR, provides methods, and provides methods for manipulating calendar fields, such as getting the date of the next week. There are two ways to instantiate a calendar, the first is calendar nowtime = new GregorianCalendar (), and the second is the calendar calendar=calendar.getinstance ();

Eg:calendar c=calendar.getinstance ();

System.out.println (C.get (calendar.year) + "year" + (C.get (calendar.month) +1) + "month" +

C.get (calendar.date) + "Day" +c.get (calendar.hour) + "point"); Output: June 16, 2015 9 o'clock

(1) Common methods:

Add (int date,int num);//returns NUM before and after the date//date represents the Date field, NUM represents the number of days added;

Get (int date);//Returns the specific information by getting the field;

Java Date Class Small summary

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.