Time and Date Methods
The information on this page is for Java 8 or later only.

In the past, "time" could be accessed in Java using methods from the Date class and the Calendar class, which proved to be somewhat cumbersome and non-intuitive in design.

Thankfully, Java SE 8 has developed a new date and time library (package)
called java.time.

The important classes included in the java.time package are:
• LocalDate - deals with date only
• LocalTime
- deals with time only
• LocalDateTime
- deals with both data and time

Both LocalDate and LocalTime use the system clock in the time zone of the system running the application.
To obtain specific info:
Remember to import java.time.*;


Using LocalDate:

LocalDate will produce a year-month-day date without time.


LocalDate today = LocalDate.now();
System.out.println("Today's date: " + today);

Output: Today's local date: 2016-10-26 //notice the style of the printing

//Obtain only specific items in the date
LocalDate today = LocalDate.now();
int year = today.getYear();
int month = today.getMonthValue();
int day = today.getDayOfMonth();
System.out.format("Year : %d Month : %d day : %d \n", year, month, day);

Output: Year : 2014 Month : 1 day : 14


Using LocalTime:

LocalTime will produce a time of day.


LocalTime time = LocalTime.now();
System.out.println("local time now: "+ time);
Hours, Minutes, Seconds, Nanoseconds
1 hour = 60 minutes
1 minute = 60 seconds
1 second = 1000000000 nanoseconds
1 second = 1 x 109

local time now: 16:33:33.369
Printed in hours, minutes, seconds, nanoseconds.

//Obtain only specific items in the time
LocalTime time = LocalTime.now();
int hour = time.getHour();
int min = time.getMinute();
int sec = time.getSecond();
System.out.format("Hour : %d Minute : %d Second : %d \n", hour, min sec);

Output: Hour: 13 Minute : 32 Second: 43


Using LocalDateTime:

LocalDateTime will produce date and time.


LocalDateTime datetime = LocalDateTime.now();
System.out.println("local date and time now: + datetime);

local date and time now: 2016-10-30T10:56:47.023
Notice the "T" used to separate the date from the time


Measure time elapsed:

Due to the manner in which LocalTime represents time, finding the amount of elapsed time can be quite involved. There is an easy way to estimate elapsed time by using System.currentTimeMillis();
This method returns the current time (long integer) in milliseconds, which is the difference between the current time and midnight, January 1, 1970 (Universal Time).


long startTime = System.currentTimeMillis();

. . . more code here

long endTime = System.currentTimeMillis() - startTime;
long seconds = endTime/1000;
//convert milliseconds to seconds


Delay time:

The computer can print information very, very quickly to the screen. It may be necessary to slow down how quickly text is appearing so the user can digest what is happening before given more information. At times, you may even want to pause the display, if only for a few seconds.

//Pause the screen for 1 second (1000 milliseconds = 1 second)
Thread.sleep(1000); } catch(InterruptedException ex) { Thread.currentThread().interrupt(); }


//Press ENTER to continue option
System.out.println("Press ENTER to continue.");
Scanner pause = new Scanner(System.in);


Return to Unit Menu |  JavaBitsNotebook.com | MathBits.com | Terms of Use  | JavaMathBits.com