Java Core API--4 (date Class)

Source: Internet
Author: User
Tags date now dateformat string format

1. Date Class (Java.utildate)

The Java.util.Date class is used to encapsulate date and time information, and it is generally used only to display a date, without any manipulation by him, for processing with the Calendar class for easy calculation.

Creates a date instance that is created by the default constructor method that represents the current system time

Date Date=new date ();

//This class overrides the ToString () method, and the output is a date format

SYSTEM.OUT.PRINTLN (date);

//View the millisecond value inside the date

Long Time=date.gettime ();

//Set the number of milliseconds so that a time date represents the current time of day

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


2. Calendar class

The Java.util.Calendar class is used to encapsulate calendar information, and its main function is that its methods can operate on the time component.

1) Obtain an instance through the static method of the calendar the method will decide the time zone according to the current system locale, help us to create a calendar instance, it is important to note that, in fact, according to different regions, Calendar has several sub-classes implemented. The calendar itself is an abstract class and cannot be instantiated! We don't need to be concerned about which subclass of the specific instance is created, we just need to use it in accordance with the calendar rules.

2) The fundamental problem that the Calendar class solves is the calculation of the simplified date, and the date class description that should be used to indicate that a day. The calendar is a date that can be described to date, and we only need to call its gettime () method to get the date object of the description.

3) Calculate time by Calendar class: Sets the time for the Calendar class, and the Calendar class sets the time using the Universal method set.

Set (int field,int value), field is the time component, and the calendar provides the corresponding constant value, which is the corresponding value.

4) only month starting from 0:0 for January, and so on, 11 for December, other time is normal starting from 1. You can also use the calendar's constant calendar. November ... such as.

5) What is the day---the calendar.day_of_month month inside;

Calendar.day_of_week days of the week---weeks

Calendar.day_of_year Days of the year

Calendar calendar=calendar.getinstance ();//constructed to represent the current time of the Calendars class

Date Now=calendar.gettime ();//Gets the dates described by the calendar

Calendar.set (Calendar.year, 2012);//Set Calendar to represent 2012

Calendar.set (calendar.day_of_month,15);//Set the calendar to indicate number 15th

Calendar.add (Calendar.day_of_year, 22);//want to get 22 days after the day

Calendar.add (Calendar.day_of_year,-5);//5 days ago is the day

Calendar.add (Calendar.month, 1); What day is it 1 months from here?

System.out.println (Calendar.gettime ());//Gets the calendar described by the

6) Gets a time unit in the date represented by the current calendar can use the Get method.

int Year=calendar.get (calendar.year);

int Month=calendar.get (calendar.month);

int Day=calendar.get (calendar.day_of_month);

SYSTEM.OUT.PRINTLN (year+ "year" + (month+1) + "month" +day+ "Day");//month to process

Case 5:

650) this.width=650; "title=" Clipboard.png "src=" http://s3.51cto.com/wyfs02/M02/6D/F6/ Wkiol1vv7wjbmh8iaageh7-v1bw031.jpg "alt=" Wkiol1vv7wjbmh8iaageh7-v1bw031.jpg "/>


3. SimpleDateFormat class

The Java.text.SimpleDateFormat class, the date conversion class, is useful for converting between string and date classes easily.

The parse method is used to convert a string representing time into a Date object in a specific format.

The format method is used to convert date data (objects) to a string in the specified format

Create a SimpleDateFormat and tell it to read the string format

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

//Create a date format string

String dateformat= "2013-05-14";

Converts a string to the corresponding Date object

Date Date=sdf.parse (DateFormat);//To catch an exception first

SYSTEM.OUT.PRINTLN (date);//Output This Date object

Defines a string format for displaying dates

SimpleDateFormat format1=new SimpleDateFormat ("Yyyy/mm/dd hh:mm:ss");

Date Now=new date (); Get current time

String Nowstr=sdf.format (now);//Transfer the object in and out of the string


This article is from "Forever Young" blog, please be sure to keep this source http://kingkongzhao.blog.51cto.com/6319491/1658463

Java Core API--4 (date Class)

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.