-->
Skip to main content

Posts

Showing posts with the label LocalDate

Convert between Localdate to sql Date in Java [2021]

Convert between Localdate to SQL Date in Java [2021]  In this tutorial, we will see Java 8 examples to convert from java.time.LocalDate to SQL Date and vice versa. Contents java.time.LocalDate to java.sql.Date java.sql.Date to java.time.LocalDate Conclusion References java.time.LocalDate to java.sql.Date To Get java.sql.Date from LocalDate we will use java.sql.Date.valueOf(LocalDate localDate) method.  Example: import java.time.LocalDate; //LocalDate to sql Date public class LocalDateToSqlDateEx { public static void main(String[] args) { // LocalDate instance LocalDate localDate = LocalDate.now(); System.out.println("LocalDate : " + localDate); // Get sql Date from LocalDate instance java.sql.Date sqlDate = java.sql.Date.valueOf(localDate); System.out.println("LocalDate to sql Date : " + sqlDate); } } Output: LocalDate : 2020-07-17 LocalDate to sql Date : 2020-07-17 java.sql.Date to java.time.LocalDate To get LocalDa

LocalDate parse string example | Convert String to LocalDate [2021]

LocalDate parse string example | Convert String to LocalDate [2021]  In this tutorial, we will see LocalDate parse string example and show how to convert string to localdate in java. There are two types of parse methods in the LocalDate Java class. LocalDate parse(CharSequence text) LocalDate parse(CharSequence text, DateTimeFormatter formatter) LocalDate Java class is an immutable and thread-safe class introduced in new Date and Time API in Java8 and date is represented in format YYYY-MM-dd. Contents Localdate Parse String to LocalDate example LocalDate Parse Multiple Formats 1. LocalDate Parse MM/dd/yyyy 2. LocalDate parse dd-MMM-yyyy 3. LocalDate parse dd/MM/yyyy 4. LocalDate parse E, MMM dd yyyy 5. LocalDate parse EEEE, MMM dd, yyyy HH:mm:ss a 6. LocalDate parse using DateTimeFormatter.ISO_LOCAL_DATE_TIME 7. LocalDate parse using DateTimeFormatter.ISO_DATE_TIME 8. LocalDate parse UTC Timezone Conclusion References Localdate Parse String

ZonedDateTime to Date Conversion [2021]

ZonedDateTime to Date Conversion [2021] To convert ZonedDateTime to Date we have to simply convert ZonedDateTime to Instant instance and then instant instance to Date instance. ZonedDateTime to Date Conversion can be done using Date.from(Instant instant) method provided by java.util.Date class . Contents Example 1: ZonedDateTime to Date Example 2: Convert ZonedDateTime to LocalDate Example 3: Convert ZonedDateTime to UTC Example 4: Convert ZonedDateTime to LocalDateTime Conclusion References Example 1: ZonedDateTime to Date In this example, we will take ZonedDateTime instance and convert that to Date instance using Date.from(Instant instant) method. import java.time.Instant; import java.time.ZonedDateTime; import java.util.Date; public class ZonedDateTimetoDateEx1 { public static void main(String[] args) { //Creating ZonedDateTime instance ZonedDateTime time = ZonedDateTime.now(); System.out.println("ZonedDateTime : " + time);

LocalDate to ZonedDateTime Conversion [2021]

LocalDate to ZonedDateTime Conversion [2021] To Convert LocalDate to ZonedDateTime we must add time and zoneId with LocalDate as ZonedDateTime  has time and ZoneId added with the date. LocalDate to ZonedDateTime conversion can be done using  LocalDate. atStartOfDay() method which will first convert localdate to localdatetime   and then localdatetime to ZonedDatetime using atZone(ZoneId zone) method on localdatetime instance. Contents Example 1: LocalDate to ZonedDateTime Example 2: LocalDate to ZonedDateTime UTC Example 3: LocalDate to ZonedDateTime change timezone Example 4: string to ZonedDateTime Conclusion References Example 1: LocalDate to ZonedDateTime In this example, we are going to create a localdate instance and then convert that to ZonedDateTime instance. import java.time.LocalDate; import java.time.ZoneId; import java.time.ZonedDateTime; public class LocalDateToZonedDateTimeEx1 { public static void main(String[] args) { // Create LocalDate i

Convert Java LocalDate to Timestamp [2021]

Convert Java LocalDate to Timestamp [2021] To Convert Java  LocalDate  to Timestamp we have one way Using  Timestamp.ValueOf(LocalDateTime time)  to convert first LocalDate to LocalDateTime and then using LocalDateTime in method ValueOf to convert to Timestamp. we come across these cases usually where we need to convert timestamp to localdate ,  timestamp to localdatetime ,  localdatetime to timestamp ,  localdate to localdatetime ,  localdatetime to date , localdate to instant , etc. In this tutorial, we are covering a similar case to convert  java localdate to timestamp using Timestamp.valueOf( LocalDateTime time ) method. Contents 1. Java LocalDate to TImestamp 1.1 Timestamp.valueOf() method Example Conclusion References Java LocalDate to Timestamp 1. public static Timestamp valueOf(LocalDateTime dateTime) This method returns an instance of java.sql.Timestamp from a LocalDateTime instance using the same year, month, day of month, hours, minutes, seconds an

Convert Timestamp to LocalDate in Java [2021]

Convert Timestamp to LocalDate in Java [2021] To Convert Timestamp to LocalDate in Java we have two strategies Using Timestamp.toLocalDateTime() to convert first to LocalDateTime and then using LocalDateTime method toLocalDate() to convert to LocalDate. Using Timestamp.toInstant().atZone(ZoneId zone) to convert first to ZonedDateTime instance and then using toLocalDate( ) method of ZonedDateTime to get LocalDate. we come across these cases usually where we need to convert timestamp to localdatetime , localdatetime to timestamp ,  localdate to localdatetime , localdatetime to date , etc. In this tutorial, we are covering a similar case to convert  timestamp to localdate. Contents 1. Timestamp to LocalDate 1.1 Using Timestamp.toLocalDateTime() 1.2 Using Timestamp.toInstant().atZone(ZoneId zone) Complete Example Conclusion References Here are the examples for 2 strategies Timestamp to LocalDate 1. Using Timestamp.toLocalDateTime() In this scenar

Localdate Add Days in Java [2021]

Localdate Add Days in Java [2021] We can add days in LocalDate using three methods provided by LocalDate Class Add days using LocalDate.plusDays(long daysToAdd) Add days Using LocalDate.plus(TemporalAmount amountToAdd) Add days Using LocalDate.plus(long amountToAdd, TemporalUnit unit) In recent post we have covered  now() method of LocalDate  Java LocalDate now() Method Example Contents LocalDate Add Days Examples in Java Add days using LocalDate.plusDays(long daysToAdd) Add days Using LocalDate.plus(TemporalAmount amountToAdd) Add days Using LocalDate.plus(long amountToAdd, TemporalUnit unit) Conclusion References LocalDate Add Days Examples: 1. Add days using LocalDate.plusDays(long daysToAdd) In this method, we just need to pass noOfDays to add in LocalDate today date and a new date with added number will be there in new LocalDate instance. Example: import java.time.LocalDate; public class LocalDateAddDaysEx1 { public static void main(String[] args) {

Java LocalDate now() Method Example [2021]

Java LocalDate now() Method Example [2021]  Java LocalDate now() is an overloaded method in LocalDate  class with three variants public static LocalDate now() public static LocalDate now(ZoneId zone) public static LocalDate now(Clock clock) We have also covered LocalDateTIme.now() in earlier tutorials. In this tutorial, we will see each method in action one by one  Java LocalDate now 1.  public static LocalDate now() This method gets the current date from the system clock in the default time-zone. This method restricts us to use an alternate clock for testing as the clock is hard-coded internally. Returns: the current date using the system clock with default time-zone which is not null. Example: import java.time.LocalDate; public class LocalDateNowEx1 { public static void main(String[] args) { //Java LocalDate now() Example LocalDate now = LocalDate.now(); System.out.println("LocalDate now() : " + now); } } Output: LocalDate now() : 2020-07-08 2.

Convert Instant to LocalDate in Java [2021]

Convert Instant to LocalDate in Java [2021]  Instant to LocalDate can be converted using 3 methods Using Instant .atZone() Using Instant .atOffset() Using LocalDateTime.ofInstant() In the recent tutorial, we have seen how to  Convert LocalDate to Instant in Java . Here are the examples for all the three methods Convert Instant to LocalDate 1. Using Instant .atZone() //Using Instant.atZone() Instant instant1 = Instant.now(); LocalDate localDate1 = instant1.atZone(ZoneId.systemDefault())                               .toLocalDate(); System.out.println(localDate1); //2020-07-07 2.  Using Instant .atOffset() //Using Instant.atOffset() Instant instant2 = Instant.now(); LocalDate localDate2 = instant2.atOffset(ZoneOffset.UTC)                               .toLocalDate(); System.out.println(localDate2);//2020-07-07 3. Using LocalDateTime.ofInstant() //Using LocalDateTime.ofInstant() Instant instant3 = Instant.now(); LocalDate localDate3= LocalDateTime.ofInstant(instant3, Zo

Convert LocalDate to Instant in Java [2021]

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 L ocalDate.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())                          .toInsta

Date to LocalDate Java Conversion [2021]

Date to LocalDate Java Conversion[2021]  Date to LocalDate Java Conversion can be done using 3 ways, There are many cases where we need to convert Date to LocalDate in our projects to make new API and old API Compatible. In the earlier tutorial, we had seen how to  Convert LocalDate to Date in Java . Here are the 3 ways to Convert Date to LocalDate Java instance By creating Instant from Date with the date.toInstant() method and then using ZonedDateTime.toLocalDate() method to get LocalDate instance. By creating Instant from Instant.ofEpochMilli() method  and then using ZonedDateTime.toLocalDate() method to get LocalDate instance. By Creating java.sql.Date new Constructo r with java.sql.Date class toLocalDate() method to get LocalDate instance. Let's see each way one by one Date to LocalDate Java 1. By creating Instant from Date with the  date.toInstant()  method and then using  ZonedDateTime.toLocalDate()  method to get LocalDate instance. Example: import java.time.Instant; im