Date and Time in Java (Date,calendar)

Source: Internet
Author: User
Tags dateformat

1. Calendar is an abstract class, static method GetInstance () can get an instance of it

Calendar CA = calendar.getinstance ();
Ca.set (2015, 10,20);//through Set You can set year, month, date, hour, minute, second, note that the month is 0-11 from 0 (default is the current time of the system);

System.out.println (Ca.get (calendar.year));//Through Get (CALENDAR.XXX) can get the corresponding month day seconds;

The following methods can be used to obtain the current day of this XX:

Get (Calendar.day_of_month) get the day of the month
Get (Calendar.day_of_week) get the day of the week
Get (Calendar.day_of_year) get the day of the year
Gettimemillis () Gets the millisecond representation of the current time

2. Conversion of calendar and date

(1) Date date = Cal.gettime ();

(2) Date date = new Date (); Cal.settime (date) ;

3. Format output Date Time

Date date = new Date ();

SimpleDateFormat sdf = new SimpleDateFormat ("Yy-mm-dd hh:mm:ss");//month mm caps are differentiated from minute mm;
System.out.println (Sdf.format (date));//15-07-31 07:51:25

4. Date

Now, let's look at adding hours to a Date object. All date operations on date need to be completed by adding milliseconds to date. For example, if we want to add 6 hours, then we need to convert 6 hours into milliseconds. 6 hours = 6 * 60 * 60 * 1000 Ms. Take a look at the example below.  

Date date = new Date (),//increase time by 6 Hrsdate.settime (Date.gettime () + 6 * 60 * 60 * 1000); SYSTEM.OUT.PRINTLN (date);//decrease time by 6 hrsdate = new Date ();d ate.settime (Date.gettime ()-6 * 60 * 60 * 1000); SYSTEM.OUT.PRINTLN (date);

The formatted date needs to be done using the DateFormat class. Let's look at a few examples.

Formatting DatesSystem.out.println (dateformat.getinstance (). Format (        date);//10/16/12 5:18 AM

The formatted date with the locale is as follows:

System.out.println (Dateformat.getdateinstance (        dateformat.full, New Locale ("It", "it"))        . Format (date);// marted“ Ottobre 2012system.out.println (dateformat.getdateinstance (        dateformat.full, Locale.italian)        . Format ( Date));//marted“ Ottobre 2012//this uses default locale USSystem.out.println (dateformat.getdateinstance (Dateformat.full        ). Format (date);//tuesday, October, 2012system.out.println (Dateformat.getdateinstance ()        . Format (date));//oct 2012system.out.println (Dateformat.getdateinstance (        dateformat.short). Format (date));//10/16/ 12system.out.println (Dateformat.getdateinstance (        dateformat.medium). Format (date));//oct 16, 2012system.out.println (Dateformat.getdateinstance (        dateformat.long). Format (date));//october 16, 2012

Date and Time in Java (Date,calendar)

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.