In this section, we will be using Date class of java.util package, and it consists of date and time. It supports two constructors: Date() – It gives us current date and time Date(long miilsec) – This takes parameter of milliseconds which has been elapsed on January 1 1970 Boolean after(Date date) – It gives after date of the given date parameter Boolean before(Date date) – It gives the before date of the given date parameter Object clone() – It creates a copy of the date object IntcompareTo(Date date) – it compares the date class object with another one IntcompareTo(Object date) – it compares with the object class object with another one Boolean equals(Object date) – it checks whether two date objects are equal Long getTime() – it fetches the time InthashCode() – it fetches the hashcode of the given date Void setTime(long Time) – It sets the time of given date object String toString() – It converts into string object of date object. Example: In this example, we are fetching the current date and time using date object Explanation of the code: Code Line 11: We are using date object to fetch the current date and time. When you execute the above code, you get the following output
Output: We are getting current date and time