Time importance in java
As software engineers we need to aware how to deal with date and time because there might be problems may occur due to different time zones and daylight savings . So first of off we focus on Time zones and daylight savings , then we move to date time classes used in java .
Time zones
According to the above image time zone is different in several countries for example :Time is different in Sri Lanka and United states ( we are having 4.13PM at Sri Lanka and they are having 5:43 AM for same day ) . Coordinated Universal Time id the primary standard used by world to calculate the date and time .IST (Indian Standard Time) is calculated by adding 05:30 to UTC time.
Daylight Saving Time
This concept used to get more work done at day time increase the productive manner. The law says that people must set their clocks back to standard time at 2 a.m. on the first Sunday in November. That means turn back the clock extra one hour at 2 a.m. During the winter months, there will be a bit more light in the morning, but the sun will set earlier in the evening. In March clocks turned forward there 2.00 am actual time will represent as 3.00am in daylight savings time .
Handle date and time in java using java API
Previously we had java.util.time and java.util.calender for date and time handling in java after java 8 version they introduced new classes to handle java date and time .
Java 8 introduced localdate , localtime and localdatetime classes to implement date and local time in java . We should use them when no longer time zones required .
LocalDate Class
LocalDate class represents a date without time in the ISO-8601 format (YYYY-MM-DD).
1 .current date
LocalDate now = LocalDate.now();
2. specific day, month, and year
LocalDate localDate = LocalDate.of(2021, 12, 20);
3. string representation of ISO-8601 format to date
LocalDate localDate = LocalDate.parse(“2021- 12–20”);
4. plus and minus days
LocalDate tomorrow = LocalDate.now().plusDays(1);
LocalDate yesterday = LocalDate.now().minusDays(1);
5. obtain the day of the week and the day of the month
DayOfWeek day = LocalDate.now().getDayOfWeek(); //week
int month = LocalDate.parse(“2020–01–12”).getDayOfMonth(); //month
6. Leap year
boolean isLeap = LocalDate.now().isLeapYear();
7. Compare two local dates
isBefore()
isAfter()
8.get start of the day
LocalDateTime startOfDay = LocalDate.now().atStartOfDay();
9.get first day of the month
LocalDate firstDayOfMonth = LocalDate.parse(“2019–12–18”) .with(TemporalAdjusters.firstDayOfMonth());
LocalTime Class
The Localtime class represents a time without any date or timezone information in the ISO-8601 format.
LocalTime is also some what similar to LocalDate in create an instance ( current time , specify time , parse string , plus or minus hours minutes and seconds )
difference is
- get different unit of the time
int hour = LocalTime.now().getHour();
int minutes = LocalTime.now().getMinute();
int seconds = LocalTime.now().getSecond();
int nanoseconds = LocalTime.now().getNano();
2. constants to get maximum, minimum, or noon time
- Maximum time
LocalTime maximum = LocalTime.MAX;
- Minimum time
LocalTime minimum = LocalTime.MIN;
- The time of noon
LocalTime noon = LocalTime.NOON;
- The time of midnight
LocalTime midnight = LocalTime.MIDNIGHT;
LocalDateTime Class
LocalDateTime class is used to represent both local date and time without timezone information.
This class provides a range of utility methods for various date and time operations
- current date and time
LocalDateTime now = LocalDateTime.now();
2. string representation of ISO-8601 format to date
LocalDateTime datetime= LocalDateTime.parse(“2019–08–02T15:20”);
3. specify date and time units
LocalDateTime datetime = LocalDateTime.of(2019, Month.AUGUST, 2, 15, 20);
4. similar to LocalDate, LocalTime here also we can add and minus days and time
LocalDateTime minusoneday= LocalDateTime.now().minusDays(1);
LocalDateTime lastYear = LocalDateTime.parse(“2021–08–02T15:20”) .minus(1, ChronoUnit.YEARS); →minus 1 year from specified year
LocalDateTime nextHour = LocalDateTime.now().plusHours(4); → add 4 hours
5.getter methods to specific date and time units
Month month = LocalDateTime.now().getMonth();
int hour = LocalDateTime.now().getHour();
6.check if a given instance is after or before the other instance
isAfter();
isBefore();
Zoned Date & Time API
After java 8 new release ZonedDateTime and ZoneID classes were added to handle date and time in Java .
ZoneId Class
identifier that is used to identity different time zones
ex:
ZoneId colombo= ZoneId.of(“Asia/Colombo”);
ZonedDateTime Class
This class represents a date-time with a timezone in the ISO-8601 format (e.g. 2021–12–20T10:15:30+05:00 Asia/colombo)
ZonedDateTime now = ZonedDateTime.now(); →currentdate and time
as localdate and time class here also we can find date and time using .now() ,getMonth(),parse(),isafter(),isbefore() methods but here the difference is we can find the timezone also (2021–12–20T10:15:30+05:00 Asia/colombo)