Date and Time In Java
In order to advance our learning, today we will discuss Java Date.
Java Dates
In Java Date class is not built-in, but we can get access to the date and time API by importing the java.time package.
There are many classes in the package that deal with date and time.
For Instance:
Class | Overview |
---|
LocalDate | Dates are represented as (year-month-day (yyyy-MM-dd)) |
LocalTime | The time in hours, minutes, seconds, and nanoseconds (HH-mm-ss-ns) is represented. |
LocalDateTime | Time and date are represented by (yyyy-MM-dd-HH-mm-ss-ns). |
DateTimeFormatter | An interface to display and parse date-time objects. |
Display Current Date In Java
In Java Date you can use the now() method of the java.time.LocalDate class to display the current date:
import java.time.LocalDate; // The LocalDate class should be importedpublic class Main {public static void main(String[] args) {LocalDate myobj=LocalDate.now(); // A date object is createdSystem.out.println("Today's Date: "+myobj); // Shows the date of the current day
}
}
Execute example and we will get the following result:
We can also obtain a date of any day with similar pattern by providing the year,month and day of month:
import java.time.LocalDate; // The LocalDate class should be importedpublic class Main {public static void main(String[] args) {LocalDate myobj=LocalDate.of(2005,6,30); // Shows date according to year , month and day of monthSystem.out.println(" Date: "+myobj); // Shows the date of the Specific day
}
}
Display Current Time In Java
Import the java.time.LocalTime class, and use its now() method to display the current time (hour, minute, second, and nanoseconds):
import java.time.LocalTime; // The LocalTime class needs to be importedpublic class Main {
public static void main(String[] args) {LocalTime obj=LocalTime.now();
System.out.println("Current Time: "+obj);}
}
We can also add and subtract (Hours, Minutes, Seconds , etc ) to the current time by using the commands:
plusHours()
plusMinutes()
import java.time.LocalTime; // The LocalTime class needs to be importedpublic class Main {
public static void main(String[] args) {LocalTime obj=LocalTime.now();
System.out.println("Current Time: "+obj);
System.out.println("\nAfter Adding 5 hours and 40 minutes to the current time we get: ");
LocalTime obj2=obj.plusHours(5); // Adding 5 hours to the current times
LocalTime obj3=obj2.plusMinutes(40); // Adding 40 minutes to the new time (obj2)
System.out.println("\nNew Time : "+obj3);}
}
Display Current Date and Time
Using the now() method of the java.time.LocalDateTime class, you can display the current date and time:
import java.time.LocalDateTime; // The LocalDateTime class needs to be importedpublic class Main{
public static void main(String[] args) {LocalDateTime myObject=LocalDateTime.now();System.out.println("Current Date and Time is: "+myObject);}
}
We can also obtain the Local Time and date of any country by using the ZoneId class and by adding the continent name / country name while declaring the object of ZoneId.
import java.time.LocalDateTime; // The LocalDateTime class needs to be imported
import java.time.ZoneId; // The Package for ZoneId is importedpublic class Main{
public static void main(String[] args) {
ZoneId zoneid = ZoneId.of("Asia/Singapore"); // Object of ZoneId is created and the current time of singapore is obtained
LocalDateTime myObject=LocalDateTime.now(zoneid); // Using zoneid to obtain time zones of different countriesSystem.out.println("Current Date and Time in Singapore is: "+myObject);}
}
Dates and times are separated in the example above by the letter “T” as shown in the above example. The DateTimeFormatter class in the same package provides the ofPattern() method for formatting and parsing date-time objects.
Here is an example that removes both the “T” and nanoseconds from the date and time.
The following examples formats the date into day – month number – year , and time into Hours : minutes : seconds pattern.
import java.time.LocalDateTime; // The LocalDateTime class needs to be imported
import java.time.format.DateTimeFormatter; // The class for DataTimeFormatter is usedpublic class Main{
public static void main(String[] args) {LocalDateTime my_object1=LocalDateTime.now();System.out.println("Before Formatting Date and Time were displayed : "+my_object1);DateTimeFormatter D_T_F=DateTimeFormatter.ofPattern("dd – MM – yyyy HH : mm : ss");// New Date format = Day of Month – Month number – Year
// New Time format = Hours : Minutes : SecondsString new_data_time=my_object1.format(D_T_F);System.out.println("After formatting Data and Time are displayed as: "+new_data_time);}
}
We can also change the format of date and time as per our preference as shown in the example below:
import java.time.LocalDateTime; // The LocalDateTime class needs to be imported
import java.time.format.DateTimeFormatter; // The class for DataTimeFormatter is usedpublic class Main{
public static void main(String[] args) {LocalDateTime my_object1=LocalDateTime.now();System.out.println("Before Formatting Date and Time were displayed : "+my_object1);DateTimeFormatter D_T_F=DateTimeFormatter.ofPattern("MM – dd – yyyy mm : HH : ss");// Date format = Month – Day of Month – Year
// Time format = Minutes : Hours : SecondsString new_data_time=my_object1.format(D_T_F);System.out.println("After formatting Data and Time are displayed as: "+new_data_time);}
}
Using the ofPattern() method, you can display the time and date in different formats. Here are some examples:
Value | Example | Tryit |
---|
yyyy-MM-dd | “1973-05-11” | Execute |
dd/MM/yyyy | “21/04/1956” | Execute |
dd-MMM-yyyy | “12-Aug-1982” | Execute |
E, MMM dd yyyy | “Fri, Dec 18 1974” | Execute |