import org.joda.time.DateTime; import java.time.LocalDate; public class Example1 { public static void main(String[] args) { DateTime dateTime = new DateTime(1609474922000L); // 2021-01-01T00:02:02.000+08:00 LocalDate localDate = dateTime.toLocalDate(); // 2021-01-01 System.out.println(localDate); } }
import org.joda.time.DateTime; import java.time.LocalDate; public class Example2 { public static void main(String[] args) { DateTime dateTime = DateTime.now(); // Current date and time LocalDate localDate = dateTime.toLocalDate(); // LocalDate representation System.out.println(localDate); } }This example is very similar to the first example, except this time we are using the now() method to create an instance of DateTime representing the current date and time. Then, we use the toLocalDate() method to convert it to LocalDate. In both examples, we use the org.joda.time package library for the DateTime class.