我正在尝试将Date
实例转换为LocalTime
实例.
// Create the Date
Date date = format.parse("2011-02-18 05:00:00.0");
// Convert to Calendar
Calendar cal = Calendar.getInstance();
cal.setTime(date);
// Convert Calendar to LocalTime
Instant instant = Instant.ofEpochMilli(cal.getTimeInMillis());
LocalTime convert = LocalDateTime.ofInstant(instant, ZoneId.systemDefault()).toLocalTime();
Run Code Online (Sandbox Code Playgroud)
我没有得到编译器错误,但LocalTime
实例值是错误的.
我认为它不起作用,因为Date
存储时间fasttime
而不是cdate
.
我正在使用JDK 1.8版.
我正在创建我的JavaFX应用程序,每次创建新的列表单元格时我都需要使用时间标签.我需要将带有当前时间的字符串HH:MM
直接放入Label构造函数中,该构造函数String
作为参数.
我发现并使用了java.util.Date
:
Label timeLabel = new Label(new SimpleDateFormat("HH:MM").format(new Date()));
Run Code Online (Sandbox Code Playgroud)
但它显示错误的时区,所以我将使用java.time
和LocalTime
上课.
有没有办法在一行中获得相同的字符串结果?谢谢您的帮助 :)