I genius the official free tutorial 23: Java Common class Date class SimpleDateFormat class Calendar class

Source: Internet
Author: User

Date class

Gets the system current time by default when creating an object

SimpleDateFormat class

Used to format the date; The creation object can be passed in the format: New SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss"); Main method Format (date)

Calendar class

You can use the Getcalendar () method in the SimpleDateFormat class to get the object. Common methods: Get (int); Common properties: Year MONTH ...

Example: Package common_class;import java.text.simpledateformat;import java.util.calendar;import  java.util.Date;/** *  Demo Date-related classes  *  @author   Genius Federation  -  Yukun  */public  Class datedemo {public static void main (String[] args)  {//Create Date Class object, The default is to get the system current Time date date = new date ();/* *  Create the formatted date class object  *  parameter for the date format that you want to output  * YYYY: Year  * mm: Month, be sure to capitalize  * DD: date, be sure to lowercase  * hh: hours  * mm: minutes, be sure to lowercase  * ss: seconds, be sure to lowercase  */simpledateformat sdf = new simpledateformat ("yyyy-MM-dd  hh:mm:ss ");//default is the International date format; output result: sun jun 14 01:17:56 cst 2015system.out.println (date );//Format Date String datestr = sdf.format (date);//Output Result: 2015-06-14 01:22:27system.out.println ( DATESTR);//Gets the Calendar class object Calendar calendar = sdf.getcalendar ();//Gets the month and day time of the second int year =  Calendar.get (calendar.year);///NOTE: Get monthThe value is Int month = calendar.get starting from 0 (calendar.month); Int day = calendar.get ( Calendar.date); Int hour = calendar.get (Calendar.hour); Int minute = calendar.get ( Calendar.minute); Int second = calendar.get (calendar.second);//output result: June 14, 2015    1:31 10 sec System.out.println (year +  "Year"  +  (month + 1)  +  "Month"  +  day +  "Day   "  + hour +  "Time"  + minute +  "Minute"  +  second +  "seconds");}} Running Result: thu jun 25 00:39:00 cst 20152015-06-25 12:39:002015 June 25    0:39 0 sec

Note: When using the SimpleDateFormat class, the case of the letter in the passed format string parameter should not be written incorrectly;
The month that you get with the Calendar class starts with 0



This article from the "Genius Union Education Official Blog" blog, reproduced please contact the author!

I genius the official free tutorial 23: Java Common class Date class SimpleDateFormat class Calendar 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.