Java program to get current system date and time

How to get current date and time in Java

Java program to get and display current System date and time.
 
Java 8 introduced new clases for date and time. This new classes resolve some issues of existing time and date classes Date and Calendar.
Java 8 introduced LocalDate, LocalTime and LocalDateTime classes.
To create an instance of current local date we can use LocalDate.now() static method.
LocaDate class represents a local date object whithout time.
Example: How to get current local date according to the system clock. The program prints current year, month and day.
LocalDate now = LocalDate.now();
System.out.println(now);
int day = now.getDayOfMonth();
int month = now.getMonthValue();                                                                                  
int year = now.getYear();
System.out.println("Day: " + day);
System.out.println("Month: " + month);
System.out.println("Year: " + year);
Output:
2021-03-01     
Day: 1
Month: 3
Year: 2021
To create an instance of current local time we can use LocalTime.now() static method.
LocalTime class represents a local time object without date or timezone component. 
Example: How to get current local time according to the system clock. The program prints current hour, minute and second.
LocalTime now = LocalTime.now();
System.out.println(now);
int hour = now.getHour();
int minute = now.getMinute();
int second = now.getSecond();
System.out.println("Hour: " + hour);
System.out.println("Minute: " + minute);                                                                          
System.out.println("Second: " + second);
Output:
20:12:08.595     
Hour: 20
Minute: 12
Second: 8
To create an instance of current local date and time we can use LocalDateTime.now() static method.
LocalDateTime class represents a date-time object without timezone component.
Example: How to get current local date and time according to the system clock. The program prints current year, month, day, hour, minute and second.
LocalDateTime now = LocalDateTime.now();
System.out.println(now);
int day = now.getDayOfMonth();
int month = now.getMonthValue();
int year = now.getYear();
int hour = now.getHour();
int minute = now.getMinute();
int second = now.getSecond();
System.out.println("Day: " + day);
System.out.println("Month: " + month);                                                                            
System.out.println("Year: " + year);
System.out.println("Hour: " + hour);
System.out.println("Minute: " + minute);
System.out.println("Second: " + second);
Output:
2021-03-03T20:44:05.407     
Day: 1
Month: 3
Year: 2021
Hour: 20
Minute: 44
Second: 5
How to get current local date and time according to the system clock using Calendar and GregorianCalendar classes
Calendar date = new GregorianCalendar();
//the get method gets the values from date
int year = date.get(Calendar.YEAR);
int month = date.get(Calendar.MONTH);
int day = date.get(Calendar.DAY_OF_MONTH);
int hour = date.get(Calendar.HOUR_OF_DAY);
int minute = date.get(Calendar.MINUTE);
int second = date.get(Calendar.SECOND);
System.out.println("Current date: " + day + "/" + (month + 1) + "/" + year);
System.out.printf("Current time: %02d:%02d:%02d %n", hour, minute, second);                                       
The program displays the current date and time:
Current date: 1/3/2021     
Current time: 20:56:54
Calendar.MONTH is an integer value in the range [0, 11] 0 for january, 1 for February, ... that is why we must add 1 to this value to display the correct value for the month.


No comments :

Post a Comment