Home >>Java Tutorial >Java Date and Time
Java date and time is the function that deals with the programming related to the date and time constants. Java does not possess a built-in Date class but if the programmer needs to use that that they can import the java.time package in order to work with the date and time API.
This package in Java consists of many date and time classes. For instance:
Class | Description |
---|---|
LocalDate | This class generally represents a date (year, month, day (yyyy-MM-dd)) |
LocalTime | This class generally represents a time (hour, minute, second and milliseconds (HH-mm-ss-zzz)) |
LocalDateTime | This class generally represents both a date and a time (yyyy-MM-dd-HH-mm-ss.zzz) |
DateTimeFormatter | This class is generally as a formatter for displaying and parsing date-time objects |
In order to display the current date the programmer have to import the java.time.LocalDate class and then use its now() method that is as depicted in the example below:
import java.time.LocalDate; //First import LocalDate class public class CurrDate { public static void main(String[] args) { LocalDate CurrDate = LocalDate.now(); System.out.println(CurrDate); // Print the current date } }
In order to display the current time (hour, minute, second, and milliseconds) the programmer have to import the java.time.LocalTime class, and use its now() method that is as depicted in the example below:
import java.time.LocalTime; //First need to import LocalTime class public class CurrTime{ public static void main(String[] args) { LocalTime CurTime = LocalTime.now(); System.out.println(CurTime); } }
In order to display the current date and time the programmer have to import the java.time.LocalDateTime class, and use its now() method that is as depicted in the example below:
import java.time.LocalDateTime; //First need to import LocalDateTime class public class CurrDateTime { public static void main(String[] args) { LocalDateTime CurDateTime = LocalDateTime.now(); System.out.println(CurDateTime); } }
The programmers can use the DateTimeFormatter class along with the ofPattern() method in the exact same package in order to format or parse the date-time objects. The example that is depicted below will remove both the "T" and milliseconds from the date-time, please have a thorough look at this example:
//First we have to Import the LocalDateTime class import java.time.LocalDateTime; //After importing of local Date Time Class Import DateTimeFormatter class import java.time.format.DateTimeFormatter; public class Main{ public static void main(String[] args) { LocalDateTime myDateTimeObj = LocalDateTime.now(); System.out.println("Display Date time Before formatting: " + myDateTimeObj); DateTimeFormatter myDateTimeFormatObj = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss"); String formattedDateTime = myDateTimeObj.format(myDateTimeFormatObj); System.out.println("Display Date time After formatting: " + formattedDateTime); } }
If the programmers wish to display the date and time in a varying format then the ofPattern() method is used that is known for accepting all sort of values. Here is an example that will depict everything regarding this method:
Value | Example |
---|---|
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" |
In order to specify the time format a time pattern string is used and in this pattern all the letters that are mentioned are all ASCII letters and are reserved as pattern letters. Here is a table that is depicting how they are defined:
Character | Description | Example |
---|---|---|
G | This generally depicts the era designator | AD |
Y | This generally depicts the year in four digits | 2001 |
M | This generally depicts the month in year | July or 07 |
D | This generally depicts the day in month | 10 |
H | This generally depicts the hour in A.M./P.M. (1~12) | 12 |
H | This generally depicts the hour in day (0~23) | 22 |
M | This generally depicts the minute in hour | 30 |
S | This generally depicts the second in minute | 55 |
S | This generally depicts the millisecond | 234 |
E | This generally depicts the day in week | Monday |
D | This generally depicts the day in year | 360 |
F | This generally depicts the day of week in month | 2 (second Wed. in July) |
W | This generally depicts the week in year | 40 |
W | This generally depicts the week in month | 1 |
A | This generally depicts the A.M./P.M. marker | PM |
K | This generally depicts the hour in day (1~24) | 24 |
K | This generally depicts the hour in A.M./P.M. (0~11) | 10 |
Z | This generally depicts the time zone | Eastern Standard Time |
' | This generally depicts the escape for text | Delimiter |
" | This generally depicts the single quote |