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