In Java8, we can use following classes to find date time differences : Period Duration ChronoUnit Period We can use Period class to calculate difference in year, month, days between two Dates. Here is an example of using Period class methods getYears(), getMonths() and getDays() to calculate age of a person. package com.topjavatutorial; import […]
Category: Date Time API
How to Convert Date to LocalDateTime and Vice versa
How to Convert Date to LocalDateTime in Java 8 To convert Date to LocalDatetime, we can use following approaches : Obtain a ZonedDateTime from the date and use its method toLocalDateTime() to get the LocalDateTime Use LocalDateTime’s ofInstant() factory method Option 1 : Convert Date to LocalDateTime Here is the sample code to convert […]
How to Convert Date to LocalDate and LocalDate to Date in Java 8
A Date object represents a specific day and time, whereas a LocalDate object only contains date without any time information. So, conversion between Date and LocalDate is possible if we only care about the date and not the time information. How to Convert Date to LocalDate in Java 8 If you want to convert […]
Java 8 – How to convert LocalDate to java.sql.Date and sql Date to LocalDate
In this article, we will see examples to convert LocalDate to java.sql.Date and sql Date to LocalDate in Java 8. Java 8 introduced java.time.LocalDate to represent a Date without time. With the Java 8 version, java.sql.Date was also updated to provide support for LocalDate, including toLocalDate and valueOf(LocalDate) methods. Convert java.time.LocalDate to java.sql.Date We […]
Java 8 Date Time api
In this article, we will discuss the new Date Time api introduced in JDK 8. Java 8 Date Time api A new Joda Time api was introduced in JDK 8 with the package java.time Here are some important classes in java.time package : LocalDate The LocalDate class represents a date in year-month-day […]