Convert LocalDate to Instant in Java [2021]
LocalDate to Instant Conversion can be done using 4 methods :
- Using LocalDate.atStartOfDay().atZone()
- Using LocalDate.atStartOfDay(ZoneId.systemDefault())
- Using LocalDate.atTime()
- Using LocalDate.atTime(LocalTime.now()).atZone()
In the recent tutorial, we have seen how to Convert Instant to LocalDate in Java.
Here are the examples for all the four methods
Convert LocalDate to Instant
1. Using LocalDate.atStartOfDay().atZone()
//Using LocalDate.atStartOfDay().atZone()
LocalDate localDate = LocalDate.now();
Instant instant1 = localDate.atStartOfDay()
.atZone(ZoneId.of("US/Eastern")).toInstant();
System.out.println(instant1);//2020-07-07T04:00:00Z
2. Using LocalDate.atStartOfDay(ZoneId.systemDefault())
//Using LocalDate.atStartOfDay(ZoneId.systemDefault())
LocalDate localDate1 = LocalDate.now();
Instant instant2 = localDate1.atStartOfDay(ZoneId.systemDefault())
.toInstant();
System.out.println(instant2);//2020-07-06T18:30:00Z
3. Using LocalDate.atTime()
//Using LocalDate.atTime()
LocalDate localDate2 = LocalDate.now();
Instant instant3 = localDate2.atTime(LocalTime.now())
.toInstant(ZoneOffset.UTC);
System.out.println(instant3);//2020-07-07T16:22:49.339Z
4. Using LocalDate.atTime(LocalTime.now()).atZone()
//Using LocalDate.atTime(LocalTime.now()).atZone()
LocalDate localDate3 = LocalDate.now();
Instant instant4 = localDate3.atTime(LocalTime.now())
.atZone(ZoneId.systemDefault()).toInstant();
System.out.println(instant4);//2020-07-07T10:52:49.340Z
One thing to highlight here is that each method first convert LocalDate to LocalDateTime and then we are creating Instant from LocalDateTime instance.Example:
Here is the full Example for all the four methods to Convert LocalDate to Instant:
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
public class LocalDateToInstantEx1 {
public static void main(String[] args) {
//Using LocalDate.atStartOfDay().atZone()
LocalDate localDate = LocalDate.now();
Instant instant1 = localDate.atStartOfDay()
.atZone(ZoneId.of("US/Eastern")).toInstant();
System.out.println("Using LocalDate.atStartOfDay().atZone() : "+instant1);
//Using LocalDate.atStartOfDay(ZoneId.systemDefault())
LocalDate localDate1 = LocalDate.now();
Instant instant2 = localDate1.atStartOfDay(ZoneId.systemDefault())
.toInstant();
System.out.println("Using LocalDate.atStartOfDay(ZoneId.systemDefault()) : "+ instant2);
//Using LocalDate.atTime()
LocalDate localDate2 = LocalDate.now();
Instant instant3 = localDate2.atTime(LocalTime.now())
.toInstant(ZoneOffset.UTC);
System.out.println("Using LocalDate.atTime() : "+ instant3);
//Using LocalDate.atTime(LocalTime.now()).atZone()
LocalDate localDate3 = LocalDate.now();
Instant instant4 = localDate3.atTime(LocalTime.now())
.atZone(ZoneId.systemDefault()).toInstant();
System.out.println("Using LocalDate.atTime(LocalTime.now()).atZone() : "+ instant4);
}
}
Output:
Using LocalDate.atStartOfDay().atZone() : 2020-07-07T04:00:00Z
Using LocalDate.atStartOfDay(ZoneId.systemDefault()) : 2020-07-06T18:30:00Z
Using LocalDate.atTime() : 2020-07-07T16:22:49.339Z
Using LocalDate.atTime(LocalTime.now()).atZone() : 2020-07-07T10:52:49.340Z
Conclusion:
In this tutorial, we have covered how to convert LocalDate to Instant in Java using 4 methods
- Using LocalDate.atStartOfDay().atZone()
- Using LocalDate.atStartOfDay(ZoneId.systemDefault())
- Using LocalDate.atTime()
- Using LocalDate.atTime(LocalTime.now()).atZone()
Comments
Post a Comment
If you have any doubts, Please let me know.