Date now = new Date(); int currentHour = now.getHours(); System.out.println("Current hour is: " + currentHour);
Date date = new Date(121, 8, 15, 12, 30); // represents September 15, 2021 12:30 PM int hour = date.getHours(); System.out.println("Hour is: " + hour);This code creates a new Date object for September 15, 2021 at 12:30 PM. The getHours() method is then called to retrieve the hour value, which is printed to the console. The java.util.Date class is part of the Java standard library, and is included in the java.util package.