Java.util Package Introduction:1, Java.util package is a Java built-in toolkit, which contains a series of commonly used tool classes, such as the processing date and calendar classes and some collection classes; 2.Java.util package does not import by default, if you want to use a class in the package, you must at the beginning of the programexplicitly declare the following statement: Import java.util.*;
Date class:1. The date class object is used to represent time and date, 2, which provides a series of methods to manipulate the components of the date and time; 3. The most useful use of the date class is to obtain the current date and time of the system.
Common construction Methods:
Date () Create a Date object using the system current time
date (long date) use from 1970 Year 1 month 1 The specified number of milliseconds after the day creates a Date object
public class datedemo{ static void main (string[] args) {Date Date = new date (); // Get the current system date and time System.out.println ("Today's date is:" + date); long time = Date.gettime (); // Get the number of milliseconds System.out.println ("Time in milliseconds from January 1, 1970 (GMT):" +
Date format printing: (with the SimpleDateFormat class under the Java.text package)
They are simply copied to the output string when they are formatted, or matched to an input string when parsing. Used in the pattern of SimpleDateFormat.
ImportJava.text.SimpleDateFormat;Importjava.util.Date; Public classDemo { Public Static voidMain (string[] args) {Date date=NewDate ();//Current Time//Print Current time (Wed Dec 16:58:29 CST)System.out.println (date); //Print Current time (local mode) (2014-12-16 16:58:29)System.out.println (date.tolocalestring ()); //Format PrintSimpleDateFormat DateFormat=NewSimpleDateFormat (); //Dateformat.applypattern ("Yyyy-mm-dd");//Print result 2014-12-16//Dateformat.applypattern ("Yyyy/mm/dd");//Print result 2014/12/16//dateformat.applypattern ("yyyy mm month DD day");//Printing Results December 16, 2014Dateformat.applypattern ("yyyy mm month DD Day HH:MM:SS E a");//Print Results December 16, 2014 17:02:31 Wednesday afternoon//converts a date type to a string with the specified patternString str =Dateformat.format (date); System.out.println (str); }}
The string is converted to the date type:
Importjava.text.ParseException;ImportJava.text.SimpleDateFormat;Importjava.util.Date; Public classDemo2 { Public Static voidMain (string[] args) {String str= "2015-12-16 17:02:31";//Time StringSimpleDateFormat DateFormat=NewSimpleDateFormat (); Dateformat.applypattern ("Yyyy-mm-dd HH:mm:ss");//Time Format//converts a string to a date type object according to the pattern that is developed. If the conversion fails, the parseexception exception is reported. Try{Date Date=dateformat.parse (str); SYSTEM.OUT.PRINTLN (date); } Catch(ParseException e) {e.printstacktrace (); } }}
Util package Knowledge point (i) Date processing dates