First,java.util.Date
Date represents a specific moment, accurate to milliseconds, with a subclass of date, time, and Timestap. By default, the output Date object is: Mon Oct 17:48:47 CST 2014, which is generally not what we want, we can call some of the methods of date to get the date format we want, but unfortunately most of the methods in the date class have been deprecated, as shown in 1:
Figure 1 Date Expiration method diagram
Where public boolean after (date time): Tests whether this date is after the specified date, and only if the moment represented by this date object is later than the instant represented by the when to return true or return false .
Public boolean before(date time): Tests whether this date is before the specified date, and only if the instant represented by this date object is earlier than the instant represented by the When before returning true ;false。
public int compareTo(date anotherdate): Returns a value if the parameter date equals this date, or a 0 value less than if this date precedes the date parameter 0 ; if this date After the Date parameter, the value greater than 0 is returned.
public boolean equals(Object obj): The result is only if and only if the argument is not and null is an object that represents the same point in time (to milliseconds) as this object Date true .
Example 1:
Public Static voidMain (string[] args) {//the specified number of milliseconds since the date (long date) date is January 1, 1970 00:00:00 GMT)Date firstdate =NewDate (125); //parameterless constructor, returns the current time, accurate to millisecondsDate seconddate =NewDate (); intOrder =Firstdate.compareto (seconddate); String Str= ""; if(Order = = 0) {str= "Same"; } Else{str= order > 0? "Firstdate in the rear": "Seconddate in the rear"; } System.out.println (Firstdate+ "\ T" +seconddate); System.out.println ("Firstdate after seconddate: \ t" +Firstdate.after (seconddate)); System.out.println ("Firstdate before seconddate: \ t" +Firstdate.before (seconddate)); System.out.println ("Firstdate and Seconddate date order: \ t" +str); System.out.println ("Firstdate and seconddate are equal: \ T" +firstdate.equals (seconddate)); }
Result 1:
Thu Jan 08:00:00 CST 1970 Mon Oct 18:33:33 CSTfirstdate whether after seconddate: falsefi Rstdate before Seconddate: truefirstdate and seconddate The order of the dates: seconddate in the rear firstdate and Seconddate is equal: false
Example 2:
New Date (System.currenttimemillis ()); // parameterless constructor, returns the current time, accurate to milliseconds New Date (); + "\ T" + Seconddate.gettime ()); System.out.println ("firstdate and seconddate are equal: \ T" + firstdate.equals (seconddate));
Result 2:
1413199559364 1413199559364 #getTime () values equal the equals ratio of two date objects is truefirstdate and seconddate are equal: True
Second,java.sql.Date
Java.sql.Date is a subclass of Java.util.Date, with only settime(Long Date),valueOf(String s) in its own method, ToString() is not obsolete. Where the ToString method outputs a date format of: YYYY-MM-DD format. ValueOf (string s) converts a string of JDBC date-escaped form to a date value s -An object that represents a day in the form of "Yyyy-mm-dd" String .
Example:
Public Static void Main (string[] args) { = new java.sql.Date (system.currenttimemillis ()); SYSTEM.OUT.PRINTLN (date); Date.settime (2222222222222L); SYSTEM.OUT.PRINTLN (date); = Java.sql.Date.valueOf ("2014-12-12"// 形参必须是: "YYYY-MM-DD" format: System.out.println ( date1); }
Results:
2014-10-132040-06-022014-12-12
Third,Timestamp
Java.sql.Timestamp derives the public void Setnanos(int n) method on the basis of date to get Timestamp the fractional seconds component of the object.
Example:
Timestamp Timestamp = timestamp.valueof ("2014-10-12 12:12:12.123"); // the value of Timestamp.getnanos () is: 123000000 Timestamp TIMESTAMP2 = timestamp.valueof ("2014-10-12 12:12:12"); // the value of Timestamp.getnanos () is: 0
It is important to note that the toString return format for Timestamp is: yyyy-mm-dd hh:mm:ss.fffffffff .
Example:
Public Static void Main (string[] args) { // get system Current time new Timestamp ( System.currenttimemillis ()); SYSTEM.OUT.PRINTLN (timestamp); // a String object that uses the YYYY-MM-DD hh:mm:ss.fffffffff format. The value of the last ffffff can not // Timestamp2.getnanos () is: 123000000 Timestamp timestamp2 = Timestamp.valueof ("2014-10-12 12:12:12.123"); System.out.println (TIMESTAMP2); System.out.println (Timestamp2.getnanos ()); }
Results:
2014-10-13 19:15:48.7572014-10-12 12:12:12.123123000000
Iv.Java.util.Calendar
Java.util.Calendar is an abstract class that provides methods for converting a particular moment to a set of calendar fields such as year, MONTH, Day_of_month, HOUR, and so on. and provides some methods for manipulating calendar fields, such as getting the date of the next week.
CalendarProvides a class method getInstance to obtain a generic object of this type. Calendar getInstance method returns an Calendar object whose Calendar field has been initialized by the current date and time: Calendar RightNow = Calendar.getinstance (); the Get () method can be used to obtain the date, time, and seconds of the month.
Example:
Public Static voidMain (string[] args) {Calendar Calendar=calendar.getinstance (); GETYYYYMMDD (Calendar); Calendar.set (Calendar.year,2016); // set year to 2016 years calendar.set (Calendar.day_of_month,30); // set to number 30th GETYYYYMMDD (calendar); Calendar.add (Calendar.year,2); // in the original year plus 2 years, the actual application can be user inquiries this year, next year, such as Calendar.add (Calendar.month,2); //Add 2 getyyyymmdd (Calendar) to the original month ; } Public Static voidGETYYYYMMDD (Calendar calendar) {System.out.println (Calendar.get (calendar.year)+ "-" + (Calendar.get (calendar.month) + 1) + "-" + calendar.get (calendar.day_of_month) + "" + Calendar.get (calendar.hour_ Of_day) + ":" + calendar.get (Calendar.minute) + ":" +Calendar.get (Calendar.second)); }
Results:
2014-10-16 9:3:492016-10-30 9:3:492018-12-30 9:3:49
Java Basic Date class use (i)