"Javase" Date and Calendar classes

Source: Internet
Author: User
Tags dateformat

2.1 DateClasses and Common methods

The java.util.Date class is used to encapsulate date and time information. Most of the current date class methods for time component calculations have been replaced by the Calendar .

An argument-free construction method that constructs a Date object that encapsulates the current date and time information

Date Date=new date ();

The Date class has been re- toString Object

GetTime () method : Gets the number of milliseconds in 1970 year 1 months 1 days, for example

Long Time=date.gettime ();

The settime () method sets the time of the date object by the number of milliseconds, for example

Date.settime (Date.gettime () + 24*60 *60*1000);

2.2 SimpleDateFormatClass

The Java.text.SimpleDateFormat class is used to implement a direct conversion of date objects and string representations of day information. It is constructed by:

Simpledateformate sdf=new SimpleDateFormat ("Yyyy-mm-dd")

Date Date=new date ();

The format method of the SimpleDateFormat is used to convert date data to a string in the specified format

2.2.1 ParseMethod

The parse method is used to convert a string representing time into a date object in a specific format. For example:

String datestr= "2013-01-06"

Simpledateformate sdf=new SimpleDateFormat ("Yyyy-mm-dd");

Date Date=sdf.parse (DATESTR);

2.2.2Common Format Strings

Year: yyyy, yy

Month:MM

Day:DD

Week:E available values or parameters:Sun,

Morning:a available value the latter parameter is:PM,AM

Hour system:HH

Hour system:hh

Minutes:mm

Seconds:SS

2.3 DateFormatClass

DateFormat is the parent class of SimpleDateFormat , and its factory method Getxxxinstance can return a date format for a different region DateFormat objects, such as

Dateformart Df2=dateformat.getdateinstane (Dateformat.medium,locale.china);

String Datestr=df2.format (New Date ());

2.4 CalendarClass

The Java.util.Calendar class is used to encapsulate calendar information, and its primary function is to use the time component for operations. The calendar is an abstract class whose specific self-thunder targets different national calendars systems, the most extensive of which is the GregorianCalendar ( Gregorian calendar ), for example

New GregorianCalendar (Zone,alocale);

the Calendar object is usually obtained using the static method of Clendar getinstance ;getinstance Methods the implementation of different Calenar classes is returned according to the system's geographical detail.

2.4.1 CalendarComponent operations

The Calendar sets and gets the various world components through the set and get methods, and the time components include: calendar.year, Mounth, date, Day_of_week, Day_of_year, week_of_mouth,week_of_year wait.

For example:Calendar c=calendar.getinstance ();

C.set (calendar.year,2013)

C.set (calendar.mounth,calendar.january);

C.set (calendar.date,29);

Note:Calendar object automatically adapts, because 2 months No, automatically becomes 3 months 1 days

2.4.2 CalendarCommonAPI

The add method increments a time component:

C.add (calendar.mounth,1);

Getactualmaximum can return the maximum value of a time component

C. Getactualmaximum (Calendar.date)

"Javase" Date and Calendar classes

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.