Java Java Tutorial Java Methods Java Classes Java File Handling Java Reference



Java Date

Java Date is a class in the Java programming language that represents a specific instant in time. It is used to work with dates and times in Java applications. The Java Date class is part of the java.util package and provides methods to perform various operations on dates and times.

The Java Date class is a legacy class that has been replaced by the newer java.time package in Java 8. However, it is still widely used in many Java applications and is supported in all versions of Java.

Creating a Java Date Object

To create a Java Date object, you can use the no-argument constructor, which creates a Date object representing the current date and time:


import java.util.Date;

public class Example {
  public static void main(String[] args) {
    Date currentDate = new Date();
    System.out.println(currentDate);
  }
}

This will output the current date and time in the default format:


Thu Sep 23 14:30:00 EDT 2021

You can also create a Date object representing a specific date and time by passing the year, month, day, hour, minute, and second values to the constructor:


import java.util.Date;

public class Example {
  public static void main(String[] args) {
    Date specificDate = new Date(121, 8, 23, 14, 30, 0);
    System.out.println(specificDate);
  }
}

This will output the specified date and time:


Tue Sep 23 14:30:00 EDT 2021

Working with Java Dates

The Java Date class provides various methods to perform operations on dates and times. Some of the commonly used methods are:

  • getTime(): Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this date.
  • before(Date when): Returns true if this date is before the specified date.
  • after(Date when): Returns true if this date is after the specified date.
  • compareTo(Date anotherDate): Compares two dates for ordering.

Here is an example that demonstrates the use of some of these methods:


import java.util.Date;

public class Example {
  public static void main(String[] args) {
    Date currentDate = new Date();
    Date specificDate = new Date(121, 8, 23, 14, 30, 0);
    
    System.out.println("Current date: " + currentDate);
    System.out.println("Specific date: " + specificDate);
    
    if (currentDate.after(specificDate)) {
      System.out.println("Current date is after specific date");
    } else {
      System.out.println("Current date is before specific date");
    }
    
    long difference = currentDate.getTime() - specificDate.getTime();
    System.out.println("Difference in milliseconds: " + difference);
  }
}

This will output:


Current date: Thu Sep 23 14:30:00 EDT 2021
Specific date: Tue Sep 23 14:30:00 EDT 2021
Current date is after specific date
Difference in milliseconds: 172800000

Formatting Dates

The Java Date class provides a toString() method that returns the date and time in a default format. However, you can also format the date and time using the SimpleDateFormat class:


import java.text.SimpleDateFormat;
import java.util.Date;

public class Example {
  public static void main(String[] args) {
    Date currentDate = new Date();
    SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    String formattedDate = formatter.format(currentDate);
    System.out.println("Formatted date: " + formattedDate);
  }
}

This will output the current date and time in the specified format:


Formatted date: 23/09/2021 14:30:00

Conclusion

The Java Date class is a useful class for working with dates and times in Java applications. It provides various methods to perform operations on dates and times, and can be formatted using the SimpleDateFormat class. While it has been replaced by the newer java.time package in Java 8, it is still widely used in many Java applications.

References

Activity