Java Learning: Date class, Datefomart class

Source: Internet
Author: User
Tags string format

Date class

Date: The class date represents a specific moment, accurate to milliseconds.

Construction Method:
Date (): The default is to get the current date time
Date (long date): Sets the current date according to the specified millisecond value

member methods:
Public Long GetTime (): Gets the millisecond value of the Date object
public void SetTime (long time): Sets the millisecond value of a Date object

1 //Date (): The default is to get the current date time2Date d =NewDate ();3 System.out.println (d);4         5         //Public Long GetTime (): Gets the millisecond value of the Date object6System.out.println (System.currenttimemillis ());//14928472033787System.out.println (NewDate (). GetTime ());8         9         //Date (long date): Sets the current date according to the specified millisecond valueTenDate D2 =NewDate (1492847203378L); One System.out.println (D2); A          -         //Public void SetTime (long time): Sets the millisecond value of a Date object -Date D3 =NewDate (); theD3.settime (1492847203378L); -SYSTEM.OUT.PRINTLN (D3);
Date Class Demo

Datefomart class

Formatting:
date--string
September 27, 2015 15:14:23

String Format (Date D)

parsing:
string--date
"2015-12-12"

Date Parse (String s)


Construction:
SimpleDateFormat () formats a Date object with the default mode
SimpleDateFormat (String pattern): Formats a Date object with a given pattern
For example:
yyyy mm month DD Day HH:MM:SS
YYYY-MM-DD HH:mm:ss

member Methods:
Public final string format (date date) formats one date as a date/time string
Public Date Parse (string source) throws ParseException parses the text from the beginning of the given string to produce a Date object,

1 //Requirement 1: Create a Date object to format the Date object2         //Create Date Object3Date d =NewDate ();4         //creates a date formatting object, uses the default format when formatting a Date object, using the dates created by the parameterless construct5SimpleDateFormat SDF =NewSimpleDateFormat ();6         //call his format (date D) to reformat the date object7String Datestr =Sdf.format (d);8 System.out.println (DATESTR);9         Ten         //requirements: Formatted format like custom date: 2017-4-22 16:00:00 One         //when we create a date format object, we need to specify a pattern A         //2017-4-22 16:00:00 The corresponding mode is: Yyyy-mm-dd HH:mm:ss -         //re-create a date formatted object and specify the appropriate pattern -SimpleDateFormat SDF2 =NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); theString DATESTR2 =Sdf2.format (d); - System.out.println (DATESTR2); -          -         //yyyy mm month DD Day HH:mm:ss date of this pattern +SimpleDateFormat SDF3 =NewSimpleDateFormat ("yyyy mm month DD Day HH:MM:SS"); -String DATESTR3 =Sdf3.format (d); +System.out.println (DATESTR3);
Code Demo

Java Learning: Date class, Datefomart 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.