import java.util.Calendar; class Main { public static void main(String[] args) { Calendar calendar = Calendar.getInstance(); System.out.println("Current date and time: " + calendar.toString()); } }
import java.util.Calendar; class Main { public static void main(String[] args) { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, 16); calendar.set(Calendar.MINUTE, 30); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); System.out.println("Specified date and time: " + calendar.getTime().toString()); } }In this example, we create a Calendar object using getInstance() method of Calendar class. Then we set a specific time using set() method of Calendar class. Finally, we get the date and time using getTime() method and print it using toString() method. The package of Calendar class is java.util.