There are six major time classes in Java, namely:
1, the Date class under the Java.util package,
2, the Date class under the java.sql package,
3. DateFormat class under Java.text package, (abstract class)
4, Java.text package of simple dateformaT class,
5. The Calendar class under the Java.util package (abstract class)
6, the time class under the java.sql package,
7, java.sql package under the TimeStamp class.
First,the date class under the Java.util package:
Date is used primarily to generate time, and date has two constructor methods: Date () and date (long date)
Second, theDateFormat class under the Java.text package, (abstract class):
DateFormat inherits from format.
The following two are implemented interfaces, two interfaces without any content:
DateFormat is an abstract class of time/date formatting subclasses, so there is no way to instantiate a constructor method, which can be instantiated with two static functions.
Getdateinstance ()------Returns a date
Getdatetimeinstance ()--------------------returns the time + date.
Third,the SimpleDateFormat class under the Java.text package:
SimpleDateFormat inherits from the DateFormat class,
The main function is: format conversion between completion dates.
Yyyy:mm:dd,hh:mm:ss:sss (SSS is the number of milliseconds)
Iv. theCalendar class under the Java.util package (abstract class):
There are two ways of instantiating this class:
1, Calendar time=new GregorianCalendar ();
2, Calendar time =calendar.getinstance ();
This class is related to calendar: "Year", "Month", "Day_of_month" Day, "Day_of_week" Week, "HOUR" hour. Such as:
V. the Date class under the java.sql package:
Used only for SQL statements, date date=new date ();(= 2017-01-01, no time part)
Six,the time class under the java.sql package ,
Seven, the TimeStamp class under the java.sql package.
Output Result:
Output Result:
Results:
Results:
Results:
1 Packagetest;2 3 ImportJava.text.DateFormat;4 Importjava.text.ParseException;5 ImportJava.text.SimpleDateFormat;6 ImportJava.util.Calendar;7 Importjava.util.Date;8 ImportJava.util.GregorianCalendar;9 Ten Public classTime { One Public Static voidMain (string[] args)throwsParseException { A //converts the current date object to a millisecond value -Date date1 =NewDate (); -Long time1 =date1.gettime (); theLong time2 = System.currenttimemillis ();//gets the millisecond value of the current time -Calendar cal=calendar.getinstance (); -Long caltime=Cal.gettimeinmillis (); -System.out.println ("Current date object converted to millisecond value:" + time2+ "Calendar class date converted to millisecond value:" +caltime); + - //convert millisecond values to date objects +Date Date2 =NewDate (); ALong Time3 =System.currenttimemillis (); at Date2.settime (time3); - - //Convert a date string into a Date object -DateFormat DF1 =NewSimpleDateFormat ("Yyyy/mm/dd"); -Date date3 = Df1.parse ("2017/01/12"); -System.out.println ("date string converted to Date object:" +date3); in -SimpleDateFormat DF =NewSimpleDateFormat ("Yyyy-mm-dd hh:mm:ss"); toDate date = Df.parse ("2009-06-12 02:06:37"); +System.out.println ("date string converted to date format:" +Df.format (date)); - the //Convert a Date object into a date string *Date datetime =NewDate (); $SimpleDateFormat SDF =NewSimpleDateFormat ("Yyyy/mm/dd");Panax NotoginsengString StringTime =Sdf.format (datetime); -System.out.println ("Date object converted to date string:" +stringtime); the +Date Date4 =NewDate (); ADateFormat df41 =dateformat.getdateinstance (dateformat.long); theDateFormat df42 =dateformat.getdatetimeinstance (Dateformat.long, dateformat.long); +String Time4 =Df41.format (date4); -String Time5 =Df42.format (date4); $System.out.println ("Getdateinstance time:" + time4 + "Getdatetimeinstance Time:" +time5); $ -Date d =NewDate (); -DateFormat df43 =dateformat.getdateinstance (dateformat.full); theDateFormat df44 =dateformat.getdateinstance (dateformat.short); -DateFormat df45 =dateformat.getdateinstance (dateformat.medium);WuyiString time43 =Df43.format (d); theString time44 =Df44.format (d); -String Time45 =Df45.format (d); WuSystem.out.println ("Full type Time:" +time43); -System.out.println ("Short type Time:" +time44); AboutSystem.out.println ("Medium type Time:" +time45); $ - //Calendar -Calendar time =calendar.getinstance (); - intYear =Time.get (calendar.year); A intmonth =Time.get (calendar.month); + intDay =Time.get (calendar.day_of_month); the intWeek =Time.get (calendar.day_of_week); - inthour =Time.get (calendar.hour); $System.out.println ("Current time is:" + year + "years" + month + "Month" + Day + "days, weeks" + Week + "," + hour + "points"); the } the}
1 The current date object is converted to a millisecond value: 1504089815431 Calendar class date converted to milliseconds: 15040898154412 date string converted to date object: Thu Jan 12 00:00:00 CST3 date string converted to date format: 2009-06-12 02:06:374 Date object converted to date string: 2017/08/305 Getdateinstance time: August 30, 2017 getdatetimeinstance time: August 30, 2017 06:43 P.M. 35 seconds 6 Full type Date: August 30, 2017 Wed 7 Short type time: 17-8-308 Medium type Time: 2017-8-309 The current time is: July 30, 2017, Week 4, 6 points
Time-type conversions in Java