Date and Time :
In Java, managing date and time involves several classes from the java.time package, introduced in Java 8. The package includes many date and time classes.
- LocalDate
- LocalTime
- LocalDateTime
- DateTimeFormatter
Display Current Date :
- To display the current date, import the java.time.LocalDate class, and use its now() method.
- Represents a date (year, month, day (yyyy-MM-dd)).
import java.time.LocalDate; // import the LocalDate class
public class LocalDateExample {
public static void main(String[] args) {
LocalDate date = LocalDate.now(); // Create a date object
System.out.println(date); // Display the current date
}
}
- Imported the LocalDate class from the java.time package. This allows you to use LocalDate in your program without fully qualifying it every time (i.e., java.time.LocalDate).
- In the main method, LocalDate date = LocalDate.now(); creates a new LocalDate object named date that represents the current date. The now() method of LocalDate class returns the current date based on the system clock in the default time-zone.
- Finally, printed the value of date to the console. Since LocalDate overrides the toString() method to provide a standard ISO-8601 format (yyyy-MM-dd), it prints the current date in that format.
Display Current Time :
- To display the current time (hour, minute, second, and nanoseconds), import the java.time.LocalTime class, and use its now() method.
- Represents a time (hour, minute, second and nanoseconds (HH-mm-ss-ns)).
import java.time.LocalTime; // import the LocalTime class
public class LocalTimeExample {
public static void main(String[] args) {
LocalTime time = LocalTime.now();
System.out.println(time);
}
}
Output :
14:02:22.218770100
- Imported the LocalTime class from the java.time package. This allows you to use LocalTime in your program without fully qualifying it every time (i.e., java.time.LocalTime).
- In the main method, LocalTime time = LocalTime.now(); creates a new LocalTime object named time that represents the current time. The now() method of LocalTime class returns the current time based on the system clock in the default time-zone.
- Finally, prints the value of time to the console. Since LocalTime overrides the toString() method to provide a standard ISO-8601 format (HH:mm.sss), it prints the current time in that format.
Display Current Date and Time :
- To display the current date and time, import the java.time.LocalDateTime class, and use its now() method.
- Represents both a date and a time (yyyy-MM-dd-HH-mm-ss-ns).
import java.time.LocalDateTime; // import the LocalDateTime class
public class LocalDateTimeExample {
public static void main(String[] args) {
LocalDateTime dateTime = LocalDateTime.now();
System.out.println(myObj);
}
}
Output :
2024-07-12T14:15:25.770661100
- Imported the LocalDateTime class from the java.time package. This allows you to use LocalDateTime in your program without fully qualifying it every time (i.e., java.time.LocalDateTime).
- In the main method, LocalDateTime dateTime = LocalDateTime.now(); creates a new LocalDateTime object named dateTime that represents the current date and time. The now() method of LocalDateTime class returns the current date and time based on the system clock in the default time-zone.
- Finally, printed the value of dateTime to the console. Since LocalDateTime overrides the toString() method to provide a standard ISO-8601 format (yyyy-MM-ddTHH:mm:ss.sss), it prints the current date and time in that format.
Formatting Date and Time :
The "T" in the displayed in LocalDateTime 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.
import java.time.LocalDateTime; // Import the LocalDateTime class
import java.time.LocalDateTime; // Import the LocalDateTime class
import java.time.format.DateTimeFormatter; // Import the DateTimeFormatter class
public class FormattingDateTimeExample {
public static void main(String[] args) {
LocalDateTime dateTime = LocalDateTime.now();
System.out.println("Before formatting: " + dateTime);
DateTimeFormatter formatDateTime = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
String formattedDate = dateTime.format(formatDateTime);
System.out.println("After formatting: " + formattedDate);
}
}
Output :
Before formatting: 2024-07-12T15:02:24.344718300
After formatting: 12-07-2024 15:02:24
- Imported java.time.LocalDateTime; imports the LocalDateTime class from the java.time package.
- Imported java.time.format.DateTimeFormatter; imports the DateTimeFormatter class from the java.time.format package. This class is used to format and parse dates and times.
- In the main method, LocalDateTime formatDateTime = LocalDateTime.now(); creates a new LocalDateTime object named formatDateTime that represents the current date and time. The now() method of LocalDateTime class returns the current date and time based on the system clock in the default time-zone.
- System.out.println("Before formatting: " + dateTime); prints the original LocalDateTime object dateTime before formatting. By default, LocalDateTime prints in ISO-8601 format (yyyy-MM-ddTHH:mm:ss).
- DateTimeFormatter formatDateTime = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss"); creates a DateTimeFormatter object formatDateTime with a specific pattern "dd-MM-yyyy HH:mm:ss". This pattern specifies how the date and time should be formatted.
- String formattedDate = dateTime.format(formatDateTime); formats the LocalDateTime object dateTime into a string formattedDate using the specified DateTimeFormatter.
- System.out.println("After formatting: " + formattedDate); prints the formatted date and time string formattedDate.