bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/Java/Java Classes
Java•Java Classes

Java Date and Time

Java Dates

Java does not have a built-in Date class, but we can import the java.time package to work with the date and time API. The package includes many date and time classes. For example:

ClassDescription
LocalDateRepresents a date (year, month, day (yyyy-MM-dd))
LocalTimeRepresents a time (hour, minute, second and nanoseconds (HH-mm-ss-ns))
LocalDateTimeRepresents both a date and a time (yyyy-MM-dd-HH-mm-ss-ns)
DateTimeFormatterFormatter for displaying and parsing date-time objects

If you don't know what a package is, read our Java Packages Tutorial .

Display Current Date

To display the current date, import the java.time.LocalDate class, and use its now() method:

Example

import java.time.LocalDate; // import the LocalDate class
public class Main {
  public static void main(String[] args) {
    LocalDate myObj = LocalDate.now(); // Create a date object
    System.out.println(myObj); // Display the current date
  }
}

Display Current Time

To display the current time (hour, minute, second, and nanoseconds), import the java.time.LocalTime class, and use its now() method:

Example

import java.time.LocalTime; // import the LocalTime class
public class Main {
  public static void main(String[] args) {
    LocalTime myObj = LocalTime.now();
    System.out.println(myObj);
  }
}

Display Current Date and Time

To display the current date and time, import the java.time.LocalDateTime class, and use its now() method:

Example

import java.time.LocalDateTime; // import the LocalDateTime class
public class Main {
  public static void main(String[] args) {
    LocalDateTime myObj = LocalDateTime.now();
    System.out.println(myObj);
  }
}

Formatting Date and Time

The "T" in the example above is used to separate the date from the time. You can use the DateTimeFormatter class with the ofPattern() method in the same package to format or parse date-time objects. The following example will remove both the "T" and nanoseconds from the date-time:

Example

import java.time.LocalDateTime; // Import the LocalDateTime class
import java.time.format.DateTimeFormatter; // Import the DateTimeFormatter class
public class Main {
  public static void main(String[] args) {
    LocalDateTime myDateObj = LocalDateTime.now();
    System.out.println("Before formatting: " + myDateObj);
    DateTimeFormatter myFormatObj = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
    String formattedDate = myDateObj.format(myFormatObj);
    System.out.println("After formatting: " + formattedDate);
  }
}

The ofPattern() method accepts all sorts of values, if you want to display the date and time in a different format. For example:

ValueExampleTryit
yyyy-MM-dd"1988-09-29"
dd/MM/yyyy"29/09/1988"
dd-MMM-yyyy"29-Sep-1988"
E, MMM dd yyyy"Thu, Sep 29 1988"

Previous

Java User Input (Scanner)

Next chapter

Java Errors

Start with Java Errors

This chapter

Overview
21

Lessons

51m

Read time

1. Java OOP2. Java Classes and Objects3. Java Class Attributes4. Java Class Methods5. Java Constructors6. Java this7. Java Modifiers8. Java Non-Access Modifiers9. Java Encapsulation10. Java Packages11. Java Inheritance12. Java Polymorphism13. Java super14. Java Inner Classes15. Java Abstraction16. Java Interface17. Java Anonymous Class18. Java Enums19. Java Enum Constructor20. Java User Input (Scanner)21. Java Date and Time

On this page

Java DatesDisplay Current DateDisplay Current TimeDisplay Current Date and TimeFormatting Date and Time