Saturday, April 4, 2020

Java date class

Java Date Class


The class Date(java.util.Date) represents an exact moment in time, with millisecond precision. The Date class specifies the constructors and methods to work with date and time. Till the JDK version 1.1 this class also allowed the formatting and parsing of date strings. However, the API for these functions was not adaptable for the purpose of internationalization. We can use Calendar and DateFormat classes for this purpose now. This class belongs to java.util package and implements Serializable, Cloneable and Comparable interface. 

Date class Constructors 


Date()

Used to create a Date object and initializes with the current instant of time at which it was allocated, measured to the nearest millisecond.

Date(long milliseconds)

Used to create a Date object and initializes for the given milliseconds since January 1, 1970, 00:00:00 GMT.

Date(int year, int month, int date)-deprecated

Date(int year, int month, int date, int hours, int minutes) -deprecated

Date(int year, int month, int date, int hours, int minutes, int seconds) -deprecated

Date(String str) -deprecated


import java.util.Date;
public class DateDemo {

     public static void main(String[] args) {
          //Constructors
     Date date1=new Date();
     System.out.println(date1.toString());
     Date date2=new Date(1617756100002L);
     System.out.println(date2);
     }
}
/*
Output:
Sat Apr 04 14:31:19 IST 2020
Wed Apr 07 06:11:40 IST 2021
*/

Date class methods


Few important methods are available in java.util.Date class are,

int compareTo(Date anyDate)

The method compares two Dates for lexicographical ordering. This method is deprecated.

boolean equals(Object obj)

This method is used to Compares two date objects for equality.

int getDate()

This method is deprecated. We can replace it by Calendar.get(Calendar.DAY_OF_MONTH).

int getDay()

This method is deprecated. We can replace it by Calendar.get(Calendar.DAY_OF_WEEK).

int getHours()

This method is deprecated. We can replace it by Calendar.get(Calendar.HOUR_OF_DAY).

int getMinutes()
This method is deprecated. We can replace it by Calendar.get(Calendar.MINUTE).

int getMonth()

This method is deprecated. We can replace it by Calendar.get(Calendar.MONTH).

int getSeconds()

This method is deprecated. We can replace it by Calendar.get(Calendar.SECOND).

long getTime()

This method returns total milliseconds(long) since January 1, 1970, 00:00:00 GMT depicted by this Date object.

Boolean after(Date when)

We can tests if this date is after the input date.

boolean before(Date when)

We can tests if this date is before the input date.

Object clone()

This method returns the clone of this object.



import java.util.Date;
public class DateDemo {

     public static void main(String[] args) {
          //Constructors
     Date date1=new Date();
     System.out.println(date1.toString());
     Date date2=new Date(1617756100002L);
     System.out.println(date2);
        //Methods
     System.out.println(date1.after(date2));
     System.out.println(date1.before(date2));
     System.out.println(date1.compareTo(date2));
     System.out.println(date1.getDate());
     System.out.println(date1.getMonth());
     System.out.println(date1.getYear());
     System.out.println(date1.getTime());
     System.out.println(date1.getHours());
     System.out.println(date1.getMinutes());
     System.out.println(date1.getSeconds());
     System.out.println(date1.toGMTString());
    
     }
}
/*
Output:
Sat Apr 04 14:40:11 IST 2020
Wed Apr 07 06:11:40 IST 2021
false
true
-1
4
3
120
1585991411771
14
40
11
4 Apr 2020 09:10:11 GMT
*/ */