How does one retrieve a specific time in Java?
In Java, you can use the LocalDateTime class from the java.time package to get a specific time. You can use the following code to get a specific time:
import java.time.LocalDateTime;
public class Main {
public static void main(String[] args) {
int year = 2022;
int month = 9;
int day = 1;
int hour = 10;
int minute = 30;
int second = 0;
LocalDateTime dateTime = LocalDateTime.of(year, month, day, hour, minute, second);
System.out.println("指定时间:" + dateTime);
}
}
In the above code, we utilize the LocalDateTime.of() method to establish a new LocalDateTime object by passing specific year, month, day, hour, minute, and second values to the method. Finally, we print out the specified time by calling the toString() method.