java 获取当前时间 short year ; byte mouth; byte day; byte hour; byte minute; byte second; short millisecond;
时间: 2024-03-26 08:40:10 浏览: 42
可以使用 Java 的 `java.time` 包中的 `LocalDateTime` 类来获取当前时间,并从中提取所需的年、月、日、小时、分钟、秒和毫秒。
以下是获取当前时间并提取所需信息的示例代码:
```java
import java.time.LocalDateTime;
public class Main {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
short year = (short) now.getYear();
byte month = (byte) now.getMonthValue();
byte day = (byte) now.getDayOfMonth();
byte hour = (byte) now.getHour();
byte minute = (byte) now.getMinute();
byte second = (byte) now.getSecond();
short millisecond = (short) now.getNano() / 1_000_000; // Convert nanoseconds to milliseconds
System.out.printf("Current time: %d-%02d-%02d %02d:%02d:%02d.%03d",
year, month, day, hour, minute, second, millisecond);
}
}
```
输出示例:
```
Current time: 2022-12-31 23:59:59.999
```
阅读全文