Best LocalDate examples with a tutorial in Java8
Learn the java.util.LocalDate
class with the top 10 examples.
What is java.time.LocalDate class in java8
LocalDate is introduced in java8 language as part of Date
and Time
API enhancement. It represents Date
without timezone
information in ISO format
ie YYYY-MM-DD
.
It is used to store dates like birthday
and holidays
.
It contains the only year, Month, and Day.
java.util.Localdate
implements the following interfaces.
- Serializable
Comparable<ChronoLocalDate>
- ChronoLocalDate
- Emporal
- TemporalAccessor
- TemporalAdjuster
LocalDate features
- It stores month year and day
- It does not store time or time zone information
- It stores birthdays and holidays
- It is immutable class and thread safety
java8 LocalDate Example
We are going to see the many use cases of the LocalDate
class.
How to get Today, Yesterday, and Tomorrow’s date?
LocalDate has the following methods.
LocalDate.now()
method is used to get current date of a default time zone.LocalDate.plus()
method is to add given date with adding one withChronoUnit.DAYS
parameterLocalDate.minusDays()
method is to subtract days from the given date .
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
public class LocalDateExamples {
public static void main(String[] args) throws Exception{
LocalDate todayDate = LocalDate.now();
System.out.println(todayDate);
LocalDate tomorrowDate = todayDate.plus(1, ChronoUnit.DAYS);
System.out.println(tomorrowDate);
LocalDate yesterdayDate = tomorrowDate.minusDays(2);
System.out.println(yesterdayDate);
}
}
Output:
2018-08-27
2018-08-28
2018-08-26
How to create a LocalDate Object in java?
There are two ways to create a LocalDate
object. The first way, create an Object using the LocalDate.now()
method which returns current dates Second way, LocalDate.of()
method is to create a LocalDate
using year
, month
and Day
.
import java.time.LocalDate;
public class LocalDateObject {
public static void main(String[] args) throws Exception{
LocalDate giveDate = LocalDate.of(2011,05,5);
System.out.println(giveDate);
LocalDate todayDate = LocalDate.now();
System.out.println(todayDate);
}
}
Output:
2011-05-05
2018-08-27
How to get a Year, Month, and Day of Week, Month, Year from the current date?
It is an example of for below methods in the LocalDate class.
- getYear(): return current year
- getMonthValue(): returns current number of a year
- getDayOfYear(): return day of a Year
- getDayOfMonth(): return day of a Month
- getDayOfWeek(): return day of a Week
import java.time.LocalDate;
public class Test {
public static void main(String[] args) throws Exception{
LocalDate todayDate = LocalDate.now();
System.out.println("Date: "+todayDate);
System.out.println("Year: "+todayDate.getYear());
System.out.println("Month: "+todayDate.getMonthValue());
System.out.println("Day of year : "+todayDate.getDayOfYear());
System.out.println("Day of Month : "+todayDate.getDayOfMonth());
System.out.println("Day of Week : "+todayDate.getDayOfWeek());
}
}
Output:
Date: 2018-08-27
Year: 2018
Month: 8
Day of year : 239
Day of Month : 27
Day of Week : MONDAY
How to convert java.sql.timestamp to LocalDate in java8?
`java.sql.Timestamp’s class is required to have date and time values used in JDBC API.
The timestamp is a combination of Date and time values. It has toLocalDateTime()
returns LocalDateTime
, and Convert this to LocalDate
using toLocalDate()
method.
Here is an example
import java.sql.Timestamp;
public class Test {
public static void main(String[] args) throws Exception{
long time = System.currentTimeMillis();
Timestamp timestamp=new Timestamp(time);
System.out.println(timestamp);
System.out.println(timestamp.toLocalDateTime().toLocalDate());
}
}
Output:
2018-08-27 14:50:58.689
2018-08-27
How to Convert Localdate to Instant Object?
java.time.Instant
class used to represent instant time in a timeline that has time zone information, and localDate has no time zone information.
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
public class Test {
public static void main(String[] args) throws Exception{
LocalDate localDate = LocalDate.now();
Instant instantDate = localDate.atStartOfDay(ZoneId.systemDefault()).toInstant();
System.out.println(instantDate);
}
}
Output:
2018-08-26T18:30:00Z
How to compare LocalDate instances in java8?
LocalDate
has three different methods to do a comparison.
isEqual
method: returns true if this date is equal to a given dateisBefore
method: returns true if this date is before the given dateisAfter
method: returns true if this date is given.
import java.time.LocalDate;
import java.time.Month;
public class Test {
public static void main(String[] args) throws Exception{
LocalDate localDateNow = LocalDate.now();
LocalDate localDate2016 = LocalDate.of(2016, Month.AUGUST, 10);
LocalDate localDate2015 = LocalDate.of(2015, Month.AUGUST, 10);
LocalDate localDate2014 = LocalDate.of(2014, Month.AUGUST, 10);
System.out.println("now - equals - 2016 " + localDateNow.isEqual(localDate2016));
System.out.println("2015 - After -2016 " + localDate2015.isAfter(localDate2016));
System.out.println("2014 - After -2015 " + localDate2014.isAfter(localDate2015));
System.out.println("2014 - Before -2015 " + localDate2014.isBefore(localDate2015));
}
}
Output:
now - equals - 2016 false
2015 - After -2016 false
2014 - After -2015 false
2014 - Before -2015 true
How to Convert String to LocalDate Object with example in java8?
LocalDate
provides parse()
method. DateTimeFormatter
is a class for formatting date-time objects The below examples have a date in string and convert to different formats ISO_LOCAL_DATE and other formats
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class Test {
public static void main(String[] args) throws Exception{
System.out.println(LocalDate.parse("2018-08-27", DateTimeFormatter.ISO_LOCAL_DATE));
System.out.println(LocalDate.parse("2018/08/27",DateTimeFormatter.ofPattern("yyyy/MM/dd")));
System.out.println(LocalDate.parse("27-08-2018",DateTimeFormatter.ofPattern("dd-MM-yyy")));
System.out.println(LocalDate.parse("27-Aug-2018",DateTimeFormatter.ofPattern("dd-MMM-yyy")));
System.out.println(LocalDate.parse("Aug 27, 2018",DateTimeFormatter.ofPattern("MMM dd, yyyy")));
}
}
Output:
2018-08-27
2018-08-27
2018-08-27
2018-08-27
2018-08-27
date is unable to parse it, Compiler throws Exception in thread “main” java.time.format.DateTimeParseException: Text ‘28/08/2018’ could not be parsed at index 0
How to Convert LocalDate to String Object example in java8?
LocalDate
provides format()
method which takes DateTimeFormatter
object.
DateTimeFormatter holds output date in string format.
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class Test {
public static void main(String[] args) throws Exception{
System.out.println(LocalDate.now().format(DateTimeFormatter.ISO_LOCAL_DATE));
System.out.println(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd")));
System.out.println(LocalDate.now().format(DateTimeFormatter.ofPattern("dd-MM-yyy")));
System.out.println(LocalDate.now().format(DateTimeFormatter.ofPattern("dd-MMM-yyy")));
System.out.println(LocalDate.now().format(DateTimeFormatter.ofPattern("MMM dd, yyyy")));
}
}
Output:
2018-08-27
2018/08/27
27-08-2018
27-Aug-2018
Aug 27, 2018
How to convert LocalDate to java.sql.Date in java8?
LocalDate
provides valueOf()
method which returns java.sql.Date
object._
import java.sql.Date;
import java.time.LocalDate;
public class Test {
public static void main(String[] args) throws Exception{
LocalDate currentDate = LocalDate.now();
Date sqlDate = Date.valueOf(currentDate);
System.out.println(sqlDate);
}
}
Output:
2018-08-27
How to convert java.util.Date to LocalDate?
java.util.Date
is to specify the time on the timeline which includes date and time with time zone information.
First, convert to Instant
object using toInstant
. To convert to LocalDate
, use default timezone ZoneId.systemDefault()
and finally call toLocalDate()
to convert it.
import java.sql.Date;
import java.time.LocalDate;
import java.time.ZoneId;
public class Test {
public static void main(String[] args) throws Exception{
Date utilDate = new Date();
System.out.println(utilDate); // Mon Aug 27 15:47:04 IST 2018
LocalDate localDate = utilDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
System.out.println(localDate); //2018-08-27
}
}
Output:
Mon Aug 27 15:47:04 IST 2018
2018-08-27
Conclusion
In this post, We learned about a new class - java.util.LocalDate
introduced in java8 and examples using this class.