In this article, we will discuss about newly introduced LocalDate class in Java 1.8 version for dealing with date in program with ease and convenience.
Prior to introducing LocalDate and LocalTime in Java 1.8 version, we have to deal with java.util.Date, java.util.Calendar, java.sql.Timestamp, java.sql.Time, java.util.TimeZone for date/time handling in Java which isn’t easy & straight-forward and there are few issues as mentioned below,
- Thread-safety :- Existing Date/Time classes and its methods aren’t thread-safe and hence it’s not convenient to handle in concurrent/parallel environment
- Not so-easy API design :- Existing Date/Time classes’ methods aren’t convenient to use in day-to-day programmer’s coding or development
- Time-zone settings :- Developers or Programmer’s life becomes difficult while dealing with time zone settings in programs
Let’s move forward and discuss about java.time.LocalDate introduced in Java 1.8 version
1. LocalDate :
- There are 3 ways to get/form a LocalDate,
- First is to get current system date using static factory method now()
- Second is to form a LocalDate using static factory method of() passing year, month and day information
- Third and final is to parse date in String-form to LocalDate using static factory method parse()
- The fully qualified package/class name of LocalDate is java.time.LocalDate i.e.; LocalDate is present under java.time package
- Class declaration for LocalDate is as follows,
package java.time;
public final class LocalDate
implements Temporal, TemporalAdjuster, ChronoLocalDate, Serializable {
}
2. LocalDate methods or APIs :
Important LocalDate method details,
- now() – get current date from the system clock in the default time-zone
- of() – get an instance of
LocalDate
from a year, month and day passed - parse() – get an instance of
LocalDate
from a text string in yyyy-MM-dd format only - getYear() – get the Year field from LocalDate
- getMonthValue() – get the month-of-year field from 1 to 12 from LocalDate
- getMonth() – get the month-of-year field using the
Month
enum from LocalDate - getDayOfMonth() – get the day-of-month field from LocalDate
- getDayOfWeek() – get the day-of-week field, which is an enum
DayOfWeek
from LocalDate - getDayOfYear() – get the day-of-year field from LocalDate
- lengthOfMonth() – get the length of the month represented by this date/LocalDate
- lengthOfYear() – get the length of the year represented by this date/LocalDate
- isLeapYear() – Checks if the year is a leap year or not, according to the ISO proleptic calendar system rules
- plusDays() – Returns a copy of invoking
LocalDate
with the specified number of days added - plusWeeks() – Returns a copy of invoking
LocalDate
with the specified number of weeks added - plusMonths() – Returns a copy of invoking
LocalDate
with the specified number of months added - plusYears() – Returns a copy of invoking
LocalDate
with the specified number of years added - minusDays() – Returns a copy of invoking
LocalDate
with the specified number of days subtracted - minusWeeks() – Returns a copy of invoking
LocalDate
with the specified number of weeks subtracted - minusMonths() – Returns a copy of invoking
LocalDate
with the specified number of months subtracted - minusYears() – Returns a copy of invoking
LocalDate
with the specified number of years subtracted - format() – Formats invoking LocalDate using the specified date formatter
- withDayOfMonth() – Returns a copy of this
LocalDate
with the day-of-month altered - withMonth() – Returns a copy of this
LocalDate
with the month-of-year altered - withYear() – Returns a copy of this
LocalDate
with the year altered - isAfter(ChronoLocalDate otherLocalDate) – Checks if the invoking LocalDate is after the specified LocalDate
- isBefore(ChronoLocalDate otherLocalDate) – Checks if the invoking LocalDate is before the specified LocalDate
- atStartOfDay() – Combines invoking LocalDate with the time of midnight to create a
LocalDateTime
at the start of this date - atStartOfDay(ZoneId) – Returns a zoned date-time from invoking LocalDate at the earliest valid time according to the rules in the time-zone
- atTime(OffsetTime) – Combines invoking LocalDate with an offset time to create an
OffsetDateTime
- atStartOfDay().toInstant() – This 2 methods combines to convert LocalDate to Instant
- isSupported(TemporalField) – checks if the specified field is supported by invoking LocalDate and returns boolean value true/false
- isSupported((TemporalUnit) – checks if the specified unit is supported by invoking LocalDate and returns boolean value true/false
3. LocalDate examples :
- LocalDate.now() – get current date from system clock
- LocalDate.of() – form LocalDate passing Year, Month and Day fields
- LocalDate.parse() – parse the date in String-form to LocalDate
- Adding day, week, month and year to LocalDate using plusDays(), plusWeeks(), plusMonths() and plusYears() methods respectively
- Subtracting day, week, month and year from LocalDate using minusDays(), minusWeeks(), minusMonths() and minusYears() methods respectively
- Formatting LocalDate in different formats using DateTimeFormatter class
- Altering day, month and year fields with LocalDate using withDayOfMonth(), withMonth() and withYear() methods respectively
- Check LocalDate is Before/After another LocalDate using below methods,
- isBefore(ChronoLocalDate) – checks if the invoking LocalDate is before the specified LocalDate
- isAfter(ChronoLocalDate) – Checks if invoking LocalDate is after the specified LocalDate
- Convert LocalDate to LocalDateTime/ZonedDateTime/OffsetDateTime/Instant
- Check Temporal Field supported by LocalDate using isSupported() method
- Check Temporal Unit supported by LocalDate using isSupported() method
3.1 LocalDate.now() method – get Current System Date
- LocalDate.now() method helps to get current system date which will be in the yyyy-MM-dd format
- We can get year, month and day field/part from LocalDate using different methods mentioned above and then we can form our own format as required like dd.MM.yyyy
- Read more in below articles,
LocalDateExampleUsingNowMethod.java
package in.bench.resources.localdate.sorting;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
public class LocalDateExampleUsingNowMethod {
public static void main(String[] args) {
// 1. get current system date
LocalDate localDate = LocalDate.now();
System.out.println("Current System LocalDate is = " + localDate);
// 1.1 get YEAR part from current system date
int year = localDate.getYear();
System.out.println("\nYear is : " + year);
// 1.2 get MONTH part from current system date
int month = localDate.getMonthValue();
System.out.println("Month is : " + month);
// 1.3 get MONTH part from current system date
Month monthInWords = localDate.getMonth();
System.out.println("Month in Words is : " + monthInWords);
// 1.4 get DAY part from current system date
int day = localDate.getDayOfMonth();
System.out.println("Day is : " + day);
// 1.5 get DAY part from current system date
DayOfWeek dayOfWeek = localDate.getDayOfWeek();
System.out.println("Day of Week is : " + dayOfWeek);
// 1.6 get DAY part from current system date
int dayOfYear = localDate.getDayOfYear();
System.out.println("Day of Year is : " + dayOfYear);
// 1.7 get Length Of current Month part from current system date
int lengthOfMonth = localDate.lengthOfMonth();
System.out.println("Length Of current Month is : " + lengthOfMonth);
// 1.8 get Length Of current Year part from current system date
int lengthOfYear = localDate.lengthOfYear();
System.out.println("Length Of current Year is : " + lengthOfYear);
// 1.9 Whether current Year is leap year or Not ?
boolean isLeapYear = localDate.isLeapYear();
System.out.print("Whether current Year is leap year ? : " + isLeapYear);
}
}
Output:
Current System LocalDate is = 2022-08-02
Year is : 2022
Month is : 8
Month in Words is : AUGUST
Day is : 2
Day of Week is : TUESDAY
Day of Year is : 214
Length Of current Month is : 31
Length Of current Year is : 365
Whether current Year is leap year ? : false
3.2 LocalDate.of() method – form Date
- Passing year, month and day field/part to LocalDate.of() method returns LocalDate which will be in the yyyy-MM-dd format
- We can extract different fields like year, month and day from LocalDate and then form our own format as required like dd/MM/yyyy
- Read more in below articles,
LocalDateExampleUsingOfMethod.java
package in.bench.resources.localdate.sorting;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
public class LocalDateExampleUsingOfMethod {
public static void main(String[] args) {
// 1. form Republic Day date
LocalDate republicDate = LocalDate.of(1950, Month.JANUARY, 26);
System.out.println("Republic Day date is = " + republicDate);
// 1.1 get YEAR part from Republic Day date
int year = republicDate.getYear();
System.out.println("\nYear is : " + year);
// 1.2 get MONTH part from Republic Day date
int month = republicDate.getMonthValue();
System.out.println("Month is : " + month);
// 1.3 get MONTH part from Republic Day date
Month monthInWords = republicDate.getMonth();
System.out.println("Month in Words is : " + monthInWords);
// 1.4 get DAY part from Republic Day date
int day = republicDate.getDayOfMonth();
System.out.println("Day is : " + day);
// 1.5 get DAY part from Republic Day date
DayOfWeek dayOfWeek = republicDate.getDayOfWeek();
System.out.println("Day of Week is : " + dayOfWeek);
// 1.6 get DAY part from Republic Day date
int dayOfYear = republicDate.getDayOfYear();
System.out.println("Day of Year is : " + dayOfYear);
// 1.7 get Length Of Republic Day date Month
int lengthOfMonth = republicDate.lengthOfMonth();
System.out.println("Length Of Republic Day Month is : " + lengthOfMonth);
// 1.8 get Length Of Republic Day date Year
int lengthOfYear = republicDate.lengthOfYear();
System.out.println("Length Of Republic Day Year is : " + lengthOfYear);
// 1.9 Whether republic day Year is leap year or Not ?
boolean isLeapYear = republicDate.isLeapYear();
System.out.print("Whether Republic Year is leap year ? : " + isLeapYear);
}
}
Output:
Republic Day date is = 1950-01-26
Year is : 1950
Month is : 1
Month in Words is : JANUARY
Day is : 26
Day of Week is : THURSDAY
Day of Year is : 26
Length Of Republic Day Month is : 31
Length Of Republic Day Year is : 365
Whether Republic Year is leap year ? : false
3.3 LocalDate.parse() method – get Date in String-form
- Sometimes, we need to parse date passed in String-form to LocalDate, for that we can use LocalDate.parse() method which will return LocalDate in yyyy-MM-dd format
- While parsing Date value in String-form should be in yyyy-MM-dd format only, otherwise java.time.format.DateTimeParseException will be thrown
- Read more in below articles,
LocalDateExampleUsingParseMethod.java
package in.bench.resources.localdate.sorting;
import java.time.LocalDate;
public class LocalDateExampleUsingParseMethod {
public static void main(String[] args) {
// 1. Republic-Day date
String republicDayDate = "1950-01-26";
// 1.1 convert/parse to dateInString to LocalDate
LocalDate republicDate = LocalDate.parse(republicDayDate);
System.out.println("1. Parsed Republic-Day date is :- \n"
+ republicDate);
// 2. Independence-Day date
String independenceDayDate = "1947-08-15";
// 2.1 convert/parse to dateInString to LocalDate
LocalDate independenceDate = LocalDate.parse(independenceDayDate);
System.out.print("\n2. Parsed Independence-Day date is :- \n"
+ independenceDate);
}
}
Output:
1. Parsed Republic-Day date is :-
1950-01-26
2. Parsed Independence-Day date is :-
1947-08-15
3.4 Adding Day/Week/Month/Year to LocalDate :
- Add 5 Days to current system LocalDate using plusDays() method
- Add 2 Weeks to current system LocalDate using plusWeeks() method
- Add 3 Months to current system LocalDate using plusMonths() method
- Add 1 Year to current system LocalDate using plusYears() method
- Read Java 8 – How to add Year, Month and Day fields to LocalDate ? for more details and examples
AddToLocalDate.java
package in.bench.resources.localdate.sorting;
import java.time.LocalDate;
public class AddToLocalDate {
public static void main(String[] args) {
// 1. get current system date
LocalDate localDate = LocalDate.now();
System.out.println("Current System Date is - " + localDate);
// 1.1 add 5 days with current system date
LocalDate add_5_Days = localDate.plusDays(5);
System.out.println("\n1. After adding 5 Days to Current System Date is - "
+ add_5_Days);
// 1.2 add 2 weeks to current system date
LocalDate add_2_Weeks = localDate.plusWeeks(2);
System.out.println("2. After adding 2 Weeks to Current System Date is - "
+ add_2_Weeks);
// 1.3 add 3 months to current system date
LocalDate add_3_Months = localDate.plusMonths(3);
System.out.println("3. After adding 3 Months to Current System Date is - "
+ add_3_Months);
// 1.4 add 1 year to current system date
LocalDate add_1_Year = localDate.plusYears(1);
System.out.print("4. After adding 1 Year to Current System Date is - "
+ add_1_Year);
}
}
Output:
Current System Date is - 2022-08-02
1. After adding 5 Days to Current System Date is - 2022-08-07
2. After adding 2 Weeks to Current System Date is - 2022-08-16
3. After adding 3 Months to Current System Date is - 2022-11-02
4. After adding 1 Year to Current System Date is - 2023-08-02
3.5 Subtracting Day/Week/Month/Year from LocalDate :
- Subtract 5 Days from current system LocalDate using minusDays() method
- Subtract 2 Weeks from current system LocalDate using minusWeeks() method
- Subtract 3 Months from current system LocalDate using minusMonths() method
- Subtract 1 Year from current system LocalDate using minusYears() method
- Read Java 8 – How to subtract Year, Month and Day fields from LocalDate ? for more details and examples
SubtractFromLocalDate.java
package in.bench.resources.localdate.sorting;
import java.time.LocalDate;
public class SubtractFromLocalDate {
public static void main(String[] args) {
// 1. get current system date
LocalDate localDate = LocalDate.now();
System.out.println("Current System Date is - " + localDate);
// 1.1 subtract 5 days from current system date
LocalDate subtract_5_Days = localDate.minusDays(5);
System.out.println("\n1. After subtracting 5 Days from Current System Date is - "
+ subtract_5_Days);
// 1.2 subtract 2 weeks from current system date
LocalDate subtract_2_Weeks = localDate.minusWeeks(2);
System.out.println("2. After subtracting 2 Weeks from Current System Date is - "
+ subtract_2_Weeks);
// 1.3 subtract 3 months from current system date
LocalDate subtract_3_Months = localDate.minusMonths(3);
System.out.println("3. After subtracting 3 Months from Current System Date is - "
+ subtract_3_Months);
// 1.4 subtract 1 year from current system date
LocalDate subtract_1_Year = localDate.minusYears(1);
System.out.print("4. After subtracting 1 Year from Current System Date is - "
+ subtract_1_Year);
}
}
Output:
Current System Date is - 2022-08-02
1. After subtracting 5 Days from Current System Date is - 2022-07-28
2. After subtracting 2 Weeks from Current System Date is - 2022-07-19
3. After subtracting 3 Months from Current System Date is - 2022-05-02
4. After subtracting 1 Year from Current System Date is - 2021-08-02
3.6 Formatting LocalDate using DateTimeFormatter:
- We can convert default ISO_LOCAL_DATE LocalDate format yyyy-MM-dd to any formats using LocalDate.format() method by passing DateTimeFormatter class as argument with required pattern in String-form
- In this illustration, we are using 4 different custom formats as mentioned below,
- DateTimeFormatter.ofPattern(“dd.MM.yyyy“)
- DateTimeFormatter.ofPattern(“dd-MMM-yyyy“)
- DateTimeFormatter.ofPattern(“dd.M.yy“)
- DateTimeFormatter.ofPattern(“dd/MMM/yy“)
- Read more in the below articles,
FormattingLocalDateUsingFormatMethod.java
package in.bench.resources.localdate.sorting;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class FormattingLocalDateUsingFormatMethod {
public static void main(String[] args) {
// 1. get current system date
LocalDate localDate = LocalDate.now();
System.out.println("Today's date in ISO_LOCAL_DATE format is = " + localDate);
// 1.1 format to dd.MM.yyyy
String formattedDate = localDate.format(DateTimeFormatter.ofPattern("dd.MM.yyyy"));
System.out.println("\n1. LocalDate in (dd.MM.yyyy) format is = " + formattedDate);
// 1.2 format to dd-MMM-yyyy
String formattedDate2 = localDate.format(DateTimeFormatter.ofPattern("dd-MMM-yyyy"));
System.out.println("2. LocalDate in (dd-MMM-yyyy) format is = " + formattedDate2);
// 2. form LocalDate using of() passing year, month and day
LocalDate localDate2 = LocalDate.of(2022, 05, 27);
System.out.println("\nLocalDate in ISO_LOCAL_DATE format is = " + localDate2);
// 1.1 format to dd.M.yy
String formattedDate3 = localDate2.format(DateTimeFormatter.ofPattern("d.M.y"));
System.out.println("\n1. LocalDate in (d.M.y) format is = " + formattedDate3);
// 1.2 format to dd/MMM/yy
String formattedDate4 = localDate2.format(DateTimeFormatter.ofPattern("dd/MMM/yy"));
System.out.print("2. LocalDate in (dd/MMM/yy) format is = " + formattedDate4);
}
}
Output:
Today's date in ISO_LOCAL_DATE format is = 2022-08-03
1. LocalDate in (dd.MM.yyyy) format is = 03.08.2022
2. LocalDate in (dd-MMM-yyyy) format is = 03-Aug-2022
LocalDate in ISO_LOCAL_DATE format is = 2022-05-27
1. LocalDate in (d.M.y) format is = 27.5.2022
2. LocalDate in (dd/MMM/yy) format is = 27/May/22
3.7 Altering Day/Month/Year fields with LocalDate:
- Altering Day, Month and Year field/part of LocalDate is possible with the help below methods,
- withDayOfMonth() – This method alters day-of-month part/field of the invoking LocalDate
- withMonth() – This method alters month-of-year part/field of the invoking LocalDate
- withYear() – This method alters year part/field of the invoking LocalDate
- Read Java 8 – How to alter Year, Month and Day fields of LocalDate ? for more details and examples
AlterLocalDate.java
package in.bench.resources.localdate.sorting;
import java.time.LocalDate;
public class AlterLocalDate {
public static void main(String[] args) {
// 1. get current system Date
LocalDate localDate = LocalDate.now();
System.out.println("Current System Date in ISO_LOCAL_DATE format is = "
+ localDate);
// 1.1 alter day part to current system Date
LocalDate dateAltered = localDate.withDayOfMonth(15);
System.out.println("\n1. Day (15) altered in Current System Date is = "
+ dateAltered);
// 1.2 alter month part to current system Date
LocalDate monthAltered = localDate.withMonth(8);
System.out.println("2. Month (8) altered in Current System Date is = "
+ monthAltered);
// 1.3 alter year part to current system Date
LocalDate yearAltered = localDate.withYear(2023);
System.out.print("3. Year (2023) altered in Current System Date is = "
+ yearAltered);
}
}
Output:
Current System Date in ISO_LOCAL_DATE format is = 2022-07-30
1. Day (15) altered in Current System Date is = 2022-07-15
2. Month (8) altered in Current System Date is = 2022-08-30
3. Year (2023) altered in Current System Date is = 2023-07-30
3.8 Check LocalDate is Before/After another LocalDate :
- isBefore(ChronoLocalDate) – checks if the invoking LocalDate is before the specified LocalDate
- Read Java 8 – How to check whether a LocalDate is Before another LocalDate ? for more details and examples
- isAfter(ChronoLocalDate) – checks if invoking LocalDate is after the specified LocalDate
- Read Java 8 – How to check whether a LocalDate is After another LocalDate ? for more details and examples
- Read Java 8 – How to compare two LocalDate instances ? to learn how to compare 2 instances of LocalDate
Compare2LocalDate.java
package in.bench.resources.localdate.sorting;
import java.time.LocalDate;
import java.time.Month;
public class Compare2LocalDate {
public static void main(String[] args) {
// 1. get today's LocalDate
LocalDate todaysLocalDate = LocalDate.of(2022, Month.AUGUST, 2);
System.out.println("1. Today's LocalDate is = " + todaysLocalDate);
// 2. form past LocalDate
LocalDate pastLocalDate = LocalDate.of(2022, Month.JANUARY, 1);
System.out.println("2. Past LocalDate is = " + pastLocalDate);
// 3. form future LocalDate
LocalDate futureLocalDate = LocalDate.of(2022, Month.DECEMBER, 31);
System.out.println("3. Future LocalDate is = " + futureLocalDate);
// 4. isBefore() - LocalDate comparison
System.out.println("\n\n4. Comparison with isBefore() method :- \n");
// 4.1 check whether todaysLocalDate isBefore futureLocalDate
boolean isBefore = todaysLocalDate.isBefore(futureLocalDate);
System.out.println("4.1 Today's LocalDate (" + todaysLocalDate
+ ") is Before Future LocalDate (" + futureLocalDate + ") ? "
+ isBefore);
// 4.2 check whether todaysLocalDate isBefore pastLocalDate
boolean isBefore2 = todaysLocalDate.isBefore(pastLocalDate);
System.out.println("4.2 Today's LocalDate (" + todaysLocalDate
+ ") is Before Past LocalDate (" + pastLocalDate + ") ? "
+ isBefore2);
// 5. isAfter() - LocalDate comparison
System.out.println("\n\n5. Comparison with isAfter() method :- \n");
// 5.1 check whether todaysLocalDate isAfter pastLocalDate
boolean isAfter = todaysLocalDate.isAfter(pastLocalDate);
System.out.println("5.1 Today's LocalDate (" + todaysLocalDate
+ ") is After Past LocalDate (" + pastLocalDate + ") ? "
+ isAfter);
// 5.2 check whether todaysLocalDate isAfter futureLocalDate
boolean isAfter2 = todaysLocalDate.isAfter(futureLocalDate);
System.out.print("5.2 Today's LocalDate (" + todaysLocalDate
+ ") is After Future LocalDate (" + futureLocalDate + ") ? "
+ isAfter2);
}
}
Output:
1. Today's LocalDate is = 2022-08-02
2. Past LocalDate is = 2022-01-01
3. Future LocalDate is = 2022-12-31
4. Comparison with isBefore() method :-
4.1 Today's LocalDate (2022-08-02) is Before Future LocalDate (2022-12-31) ? true
4.2 Today's LocalDate (2022-08-02) is Before Past LocalDate (2022-01-01) ? false
5. Comparison with isAfter() method :-
5.1 Today's LocalDate (2022-08-02) is After Past LocalDate (2022-01-01) ? true
5.2 Today's LocalDate (2022-08-02) is After Future LocalDate (2022-12-31) ? false
3.9 LocalDate to LocalDateTime/ZonedDateTime or OffsetDateTime/Instant :
- atStartOfDay() – Combines invoking LocalDate with the time of midnight to create a
LocalDateTime
at the start of this date- Read Java 8 – How to convert LocalDate to LocalDateTime ? for more details and examples
- atStartOfDay(ZoneId) – Returns a zoned date-time from invoking LocalDate at the earliest valid time according to the rules in the time-zone
- Read Java 8 – How to convert LocalDate to ZonedDateTime ? for more details and examples
- atTime(OffsetTime) – Combines invoking LocalDate with an offset time to create an
OffsetDateTime
- Read Java 8 – How to convert LocalDate to an OffsetDateTime ? for more details and examples
- atStartOfDay().toInstant() – This 2 methods combines to convert LocalDate to Instant
- Read Java 8 – How to convert LocalDate to an Instant ? for more details and examples
- Read below articles for more conversion,
- Java 8 – How to convert LocalDate to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.Date and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.Calendar and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.GregorianCalendar and vice-versa ?
- Java 8 – How to convert LocalDate to javax.xml.datatype.XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalDate in different ways ?
ConvertLocalDate.java
package in.bench.resources.localdate.sorting;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
public class ConvertLocalDate {
public static void main(String[] args) {
// get current system date
LocalDate localDate = LocalDate.now();
System.out.println("Current System LocalDate is :- \n" + localDate);
// 1. convert LocalDate to LocalDateTime
LocalDateTime localDateTime = localDate.atStartOfDay();
System.out.println("\n1. LocalDate to LocalDateTime is :- \n" + localDateTime);
// 2. convert LocalDate to LocalDateTime
ZoneId zoneId = ZoneId.systemDefault();
ZonedDateTime zonedDateTime = localDate.atStartOfDay(zoneId);
System.out.println("\n2. LocalDate to ZonedDateTime is :- \n" + zonedDateTime);
// 3. convert LocalDate to OffsetDateTime
OffsetTime offsetTime = OffsetTime.now();
OffsetDateTime offsetDateTime = localDate.atTime(offsetTime);
System.out.println("\n3. LocalDate to OffsetDateTime is :- \n" + offsetDateTime);
// 4. convert LocalDate to Instant
ZoneOffset zoneOffset = ZoneOffset.UTC;
Instant instant = localDate.atStartOfDay().toInstant(zoneOffset);
System.out.print("\n4. LocalDate to an Instant is :- \n" + instant);
}
}
Output:
Current System LocalDate is :-
2022-08-03
1. LocalDate to LocalDateTime is :-
2022-08-03T00:00
2. LocalDate to ZonedDateTime is :-
2022-08-03T00:00+05:30[Asia/Calcutta]
3. LocalDate to OffsetDateTime is :-
2022-08-03T16:07:36.525453500+05:30
4. LocalDate to an Instant is :-
2022-08-03T00:00:00Z
3.10 Check Temporal Fields supported by LocalDate :
- isSupported(TemporalField) – checks if the specified field is supported by invoking LocalDate and returns boolean value true/false
- Read Java 8 – What are all the Temporal Fields supported by LocalDate ? for more details and examples
CheckLocalDateIsSupportedUsingTemporalField.java
package in.bench.resources.localdate.sorting;
import java.time.LocalDate;
import java.time.temporal.ChronoField;
public class CheckLocalDateIsSupportedUsingTemporalField {
public static void main(String[] args) {
// get current system date
LocalDate localDate = LocalDate.now();
System.out.println("Current LocalDate is = \n" + localDate);
// 1. check ChronoField.DAY_OF_WEEK field supported ?
System.out.println("\n1. LocalDate.isSupported(ChronoField.DAY_OF_WEEK) ? " +
localDate.isSupported(ChronoField.DAY_OF_WEEK));
// 2. check ChronoField.DAY_OF_MONTH field supported ?
System.out.println("\n2. LocalDate.isSupported(ChronoField.DAY_OF_MONTH) ? " +
localDate.isSupported(ChronoField.DAY_OF_MONTH));
// 3. check ChronoField.YEAR field supported ?
System.out.println("\n3. LocalDate.isSupported(ChronoField.YEAR) ? " +
localDate.isSupported(ChronoField.YEAR));
// 4. check ChronoField.MINUTE_OF_HOUR field supported ?
System.out.print("\n4. LocalDate.isSupported(ChronoField.MINUTE_OF_HOUR) ? " +
localDate.isSupported(ChronoField.MINUTE_OF_HOUR));
}
}
Output:
Current LocalDate is =
2022-08-07
1. LocalDate.isSupported(ChronoField.DAY_OF_WEEK) ? true
2. LocalDate.isSupported(ChronoField.DAY_OF_MONTH) ? true
3. LocalDate.isSupported(ChronoField.YEAR) ? true
4. LocalDate.isSupported(ChronoField.MINUTE_OF_HOUR) ? false
3.11 Check Temporal Units supported by LocalDate :
- isSupported((TemporalUnit) – checks if the specified unit is supported by invoking LocalDate and returns boolean value true/false
- Read Java 8 – What are all the Temporal Units supported by LocalDate ? for more details and examples
CheckLocalDateIsSupportedUsingTemporalUnit.java
package in.bench.resources.localdate.sorting;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
public class CheckLocalDateIsSupportedUsingTemporalUnit {
public static void main(String[] args) {
// get current system date
LocalDate localDate = LocalDate.now();
System.out.println("Current LocalDate is = \n" + localDate);
// 1. check ChronoUnit.DAYS field supported ?
System.out.println("\n1. LocalDate.isSupported(ChronoUnit.DAYS) ? " +
localDate.isSupported(ChronoUnit.DAYS));
// 2. check ChronoUnit.YEARS field supported ?
System.out.println("\n2. LocalDate.isSupported(ChronoUnit.YEARS) ? " +
localDate.isSupported(ChronoUnit.YEARS));
// 3. check ChronoUnit.DECADES field supported ?
System.out.println("\n3. LocalDate.isSupported(ChronoUnit.DECADES) ? " +
localDate.isSupported(ChronoUnit.DECADES));
// 4. check ChronoUnit.MINUTES field supported ?
System.out.print("\n4. LocalDate.isSupported(ChronoUnit.MINUTES) ? " +
localDate.isSupported(ChronoUnit.MINUTES));
}
}
Output:
Current LocalDate is =
2022-08-07
1. LocalDate.isSupported(ChronoUnit.DAYS) ? true
2. LocalDate.isSupported(ChronoUnit.YEARS) ? true
3. LocalDate.isSupported(ChronoUnit.DECADES) ? true
4. LocalDate.isSupported(ChronoUnit.MINUTES) ? false
Related Articles:
- Java 8 – LocalDate with method details and examples
- Java 8 – LocalTime with method details and examples
- Java 8 – LocalDateTime with method details and examples
- Java 8 – ZonedDateTime with method details and examples
- Java 8 – Display all Zones and its Offset using ZoneId and ZoneOffset
- Java 8 – OffsetDateTime with method details and examples
- Java 8 – Instant with method details and examples
- Java 8 – How to display Zones for particular Offset ?
- Java 8 – Clock with method details and examples
- Java 8 – OffsetTime with method details and examples
- Java 8 – How to get current Date and Time ?
- Java 8 – How to sort List by java.util.Date in different ways ?
- Java 8 – How to sort List by java.time.LocalDate in different ways ?
- Java 8 – How to sort List by java.time.LocalDateTime in different ways ?
- Java 8 – How to sort List by java.time.ZonedDateTime in different ways ?
- Java 8 – How to sort List by java.time.OffsetDateTime in different ways ?
- Java 8 – More Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/chrono/ChronoLocalDate.html
- https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/ZoneId.html
- https://docs.oracle.com/javase/8/docs/api/java/time/ZoneOffset.html
- https://docs.oracle.com/javase/8/docs/api/java/time/OffsetDateTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/OffsetTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html
- https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Date.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html
- https://docs.oracle.com/javase/7/docs/api/java/sql/Timestamp.html
- https://docs.oracle.com/javase/7/docs/api/java/sql/Time.html
Happy Coding !!
Happy Learning !!