About the Java Date class Date,dateformat,simpledateformat,calendar summary

Source: Internet
Author: User
Tags dateformat

Date,dateformat,simpledateformat,calendar

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "Styl E= "Background:url ("/e/u261/lang/zh-cn/images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= " Spacer.gif "/>

1.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 (): Distribution 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

(2) Date Common methods:

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

It doesn't matter how 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


2,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. It 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 return is a date + time. At the same time,getdateinstance (int style),getdateinstance (int style, Locale Alocale) and other methods are also very useful. About the style value:

Full : Longest length For example: Wednesday, January 9, 2013 Long : Longer length example: January 9,

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. SimpleDateFormat class:

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. Calendar class:

under the Java.util package, the Calendar class is an abstract class, It is a "specific moment" with a group such as "year",

"MONTH", "Day_of_month", "HOUR" conversion between Calendar fields 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;

These insights are to find a large amount of information condensed into the essence, I feel able to a new entry of the people play some key role, as for these classes there are many many ways, I think there is no need to remember, do not know when you can check the API documentation.

This article from "Not _ Heroes" blog, reproduced please contact the author!

About the Java Date class Date,dateformat,simpledateformat,calendar summary

Related Article

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.