How to get Current Date in Java [Practical Examples]

Get current date in Java

In java, there are several ways to get the current date. Since Java 8, the class and methods of java.time package is used to get the current date and time. Whereas, in the earlier versions we can get the current date using class and methods of java.util package. The list below shows eight different ways to get current date in Java.

  • Using java.util.Date
  • Using java.util.Calendar
  • Using java.time.LocalDateTime
  • Using java.time.LocalDate
  • Using java.time.Year and java.time.MonthDay
  • Using java.time.OffsetDateTime
  • Using java.time.Instant
  • Using java.time.ZonedDateTime

 

Method-1: Using java.util.Date

In this approach, to get current date in Java we are using the Date class of java.util package. Here, we have to create an instance of the Date class. Thereafter, we are formatting the date using the format function of SimpleDateFormat class. This approach was used earlier to java 8 version.

Advertisement

Example :  In this example, we will simply create a object of Date class and use format function to format it in desired way.

// Import required Package
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    public static void main(String[] args) {
        // Setting the format
        SimpleDateFormat f = new SimpleDateFormat("dd/MM/yyyy");

        // Printing current date 
        System.out.println("Current date is " + f.format(new Date()));
    }
}

Output

Current date is 17/01/2022

 

Method-2: Using java.util.Calendar

In this approach, to get current date in Java we are using the Calendar class of java.util package. Here, we have to create an instance of the Calendar class. Thereafter, we are formatting the date using the format function of SimpleDateFormat class. This approach was used earlier to java 8 version.

Example : In this example, we will use getInstance() method to get an instance of the Calendar class and the getTime() method to get the current date and time.

// Importing package
import java.util.Calendar;
import java.text.SimpleDateFormat;

public class Main {
    // main method  
    public static void main(String[] argvs) {
        // Setting format    
        SimpleDateFormat f = new SimpleDateFormat("dd-MM-yyyy");

        // Creating calendar instance
        Calendar c = Calendar.getInstance();

        // Printing current date
        System.out.println("Current date is " + f.format(c.getTime()));
    }
}

Output

Current date is 17-01-2022

 

Method-3: Using java.time.LocalDateTime

From Java 8 onward, this approach is used to get current date in Java. Here, we are using the now() method of LocalDateTime class of java.time package to get the current date time without a time-zone, and format it with the DateTimeFormatter.

Advertisement

Example : In this example, we will call a static method now of a LocalDateTime class and format its result using object of DateTimeFormatter Class.

// Importing package
import java.time.format.DateTimeFormatter;
import java.time.LocalDateTime;
public class Main {
    public static void main(String[] args) {
        // Setting format 
        DateTimeFormatter d = DateTimeFormatter.ofPattern("dd/MM/yyyy");

        // Printing current date
        System.out.println(d.format("Current date is " + LocalDateTime.now()));
    }
}

Output

Current date is 17/01/2022

 

Method-4: Using java.time.LocalDate

From Java 8 onward, this approach is used to get current date in Java. Here, we are using the now() method of LocalDate class of java.time package to get the current date from the specified clock.

Example : In this example, we will call a static method now of a LocalDate class to get a current date.

// Importing package
import java.time.LocalDate;
public class Main {
    // main method  
    public static void main(String[] argvs) {
        // getting current date
        System.out.println("Current date is " + LocalDate.now());
    }
}

Output

Current date is 2022-01-17

 

Method-5: Using java.time.Year and java.time.MonthDay

From Java 8 onward, this approach is used to get current date in Java. Here, Year and MonthDay are class of java.time package. The Year.now() method returns the current year from the system clock in the default time-zone. The MonthDay.now() returns the current month-day from the specified clock.

Advertisement

Example : In this example, we will call a static method now of a Year class MonthDay class to get the year and month-day respectively.

// Importing package
import java.time.Year;
import java.time.MonthDay;

public class Main {
    // main method  
    public static void main(String[] argvs) {
        // Getting current date
        System.out.println("Current date is " + Year.now() + "" + MonthDay.now());
    }
}

Output

Current date is 2022-01-17

 

Method-6: Using java.time.OffsetDateTime

From Java 8 onward, this approach is used to get current date in Java. Here, OffsetDateTime is a class of java.time package that is used to get a date-time with an offset from UTC/Greenwich in the ISO-8601 calendar system.

Example : In this example, we will call a static method now of a OffsetDateTime class and use substring method to only print the date from the result.

// Importing package
import java.time.OffsetDateTime;

public class Main {
    // main method  
    public static void main(String[] argvs) {
        // Getting current date
        String s1 = OffsetDateTime.now().toString();

        // Printing a part of result using substring function
        System.out.println("Current date is " + s1.substring(0, 10));
    }
}

Output

Current date is 2022-01-17

 

Method-7: Using java.time.Instant

From Java 8 onward, this approach is used to get current date in Java. Here, Instant is a class of java.time package is used to get a current instant from the system clock.

Advertisement

Example : In this example, we will call a static method now of a Instant class and use sub string method to only print the date from the result.

// Importing package
import java.time.Instant;

public class Main {
    // main method  
    public static void main(String[] argvs) {
        // Getting current date 
        String s1 = Instant.now().toString();

        // Printing a part of result using substring function
        System.out.println("Current date is " + s1.substring(0, 10));
    }
}

Output

Current date is 2022-01-17

 

Method-8: Using java.time.ZonedDateTime

From Java 8 onward, this approach is used to get current date in Java. Here, ZonedDateTime is a class of java.time package is used to get the current date-time from the system clock in the default time-zone.

Example : In this example, we will call a static method now of a ZonedDateTime class and use sub string method to only print the date from the result.

// Importing package
import java.time.ZonedDateTime;

public class Main {
    // main method  
    public static void main(String[] argvs) {
        // Getting current date
        String s1 = ZonedDateTime.now().toString();

        // Printing a part of result using substring function
        System.out.println("Current date is " + s1.substring(0, 10));
    }
}

Output

Current date is 2022-01-17

 

Summary

The knowledge of how to get the current date is very useful. In many applications we need to store the system date like an application log, for a session tracking, perform certain transaction, and many more things. Here, We have covered eight different ways to get the current date in Java with example. All in all, this tutorial, covers everything that you need to know in order to get the current date in Java.

Advertisement

 

References

Date Class
Calendar Class
LocalDateTime Class
LocalDate Class
Year Class
MonthDay Class
OffsetDateTime Class
Instant Class
ZonedDateTime Class

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment

X