import java.util.Calendar; import java.util.Date; public class CalendarExample { public static void main(String[] args) { Calendar calendar = Calendar.getInstance(); Date date = calendar.getTime(); System.out.println(date); } }
import java.util.Calendar; import java.util.Date; public class CalendarExample { public static void main(String[] args) { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.YEAR, 2022); calendar.set(Calendar.MONTH, Calendar.JUNE); calendar.set(Calendar.DAY_OF_MONTH, 10); calendar.set(Calendar.HOUR_OF_DAY, 15); calendar.set(Calendar.MINUTE, 30); calendar.set(Calendar.SECOND, 0); Date date = calendar.getTime(); System.out.println(date); } }This code sets the date and time to June 10, 2022, at 3:30 PM, using the set() method of the Calendar class, and then retrieves the resulting Date object using the getTime() method. The Calendar class is part of the java.util package library in Java.