xxxxxxxxxx
// Import required packages
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
// Convert LocalDateTime to Date
LocalDateTime localDateTime = LocalDateTime.now();
Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
xxxxxxxxxx
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/MM/yyyy");
String date = "16/08/2016";
//convert String to LocalDate
LocalDate localDate = LocalDate.parse(date, formatter);
xxxxxxxxxx
// Localdate to Date
Date.from(localdate.atStartOfDay(ZoneId.systemDefault()).toInstant())
xxxxxxxxxx
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
public class Main {
public static void main(String[] args) {
// Example LocalDate
LocalDate localDate = LocalDate.now();
// Convert LocalDate to Date
Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
// Print the converted Date
System.out.println(date);
}
}
xxxxxxxxxx
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
public class Main {
public static void main(String[] args) {
// Create a LocalDateTime object
LocalDateTime localDateTime = LocalDateTime.now();
// Convert LocalDateTime to Date
Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
System.out.println("LocalDateTime: " + localDateTime);
System.out.println("Date: " + date);
}
}
xxxxxxxxxx
Date yourDate = new Date(); // this will be your java.util.Date instance to convert
LocalDate date = yourDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
xxxxxxxxxx
import java.time.LocalDateTime;
// https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html
public class LocalDateTimeExample {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
int year = now.getYear();
int month = now.getMonthValue();
int day = now.getDayOfMonth();
int hour = now.getHour();
int minute = now.getMinute();
int second = now.getSecond();
System.out.println("yr:" + year
+ " mth:" + month
+ " day:" + day
+ " hr:" + hour
+ " min:" + minute
+ " sec:" + second);
}
}
xxxxxxxxxx
public LocalDateTime convertToLocalDateTimeViaMilisecond(Date dateToConvert) {
return Instant.ofEpochMilli(dateToConvert.getTime())
.atZone(ZoneId.systemDefault())
.toLocalDateTime();
}
xxxxxxxxxx
public LocalDateTime convertToLocalDateTimeViaInstant(Date dateToConvert) {
return dateToConvert.toInstant()
.atZone(ZoneId.systemDefault())
.toLocalDateTime();
}