在java中格式化本地时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44701422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Formatting local time in java
提问by Daniel Piskorz
I'm creating my JavaFX application and I need to use time label every time new list cell is created. I need to put the string with current time in HH:MM
format directly into Label constructor which takes String
as a parameter.
我正在创建我的 JavaFX 应用程序,每次创建新列表单元格时我都需要使用时间标签。我需要将具有当前时间HH:MM
格式的字符串直接放入 Label 构造函数中,该构造函数将其String
作为参数。
I found and used java.util.Date
's:
我发现并使用了java.util.Date
:
Label timeLabel = new Label(new SimpleDateFormat("HH:MM").format(new Date()));
but it shows the wrong time zone, so I'm going to use java.time
and LocalTime
class.
但它显示了错误的时区,所以我要使用java.time
和LocalTime
上课。
Is there any way to achieve same string result in one line? Thank You for your help :)
有没有办法在一行中实现相同的字符串结果?感谢您的帮助 :)
采纳答案by Manos Nikolaidis
It's probably better to use Java 8 types (java.time) in a new application. You can first create a DateTimeFormatter
:
在新应用程序中使用 Java 8 类型 (java.time) 可能更好。您可以先创建一个DateTimeFormatter
:
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm");
And then get the current time and format it:
然后获取当前时间并格式化:
Label timeLabel = new Label(LocalTime.now().format(dtf));
回答by Usagi Miyamoto
Try something like this:
尝试这样的事情:
Label timeLabel = new Label(String.format("%tR", LocalTime.now()));
See: http://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html
请参阅:http: //docs.oracle.com/javase/8/docs/api/java/util/Formatter.html
回答by Alexey Romanov
LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm"))
(this will use the default locale, see https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ofPattern-java.lang.String-for more).
(这将使用默认语言环境,请参阅https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ofPattern-java.lang.String-了解更多信息)。
But better practice would be to put the formatter into a static final
field. This way it's only created once instead of every time the line is exectuted. It nearly certainly doesn't really matter for this application, but it's better to use good habits from the beginning.
但更好的做法是将格式化程序放入一个static final
字段中。这样它只创建一次,而不是每次执行该行时。对于这个应用程序来说几乎可以肯定并不重要,但最好从一开始就养成良好的习惯。
回答by Kobi Lehrer
You can create a LocalTimeinstance in several ways. The first way is to create a LocalTime instance that represents the exact time of now. Here is how that looks:
您可以通过多种方式创建LocalTime实例。第一种方法是创建一个 LocalTime 实例来表示现在的确切时间。这是它的外观:
LocalTime localTime = LocalTime.now();
Another way to create a LocalTime object is to create it from a specific amount of hours, minutes, seconds and nanoseconds. Here is how that looks:
创建 LocalTime 对象的另一种方法是从特定数量的小时、分钟、秒和纳秒创建它。这是它的外观:
LocalTime localTime2 = LocalTime.of(21, 30, 59, 11001);
There are also other versions of the of() method that only takes hours and minutes, or hours, minutes and seconds as parameters.
还有其他版本的 of() 方法只需要小时和分钟,或小时、分钟和秒作为参数。
You can access the hours, minutes, seconds and nanosecond of a LocalTime object using these methods:
您可以使用以下方法访问 LocalTime 对象的小时、分钟、秒和纳秒:
- getHour()
- getMinute()
- getSecond()
- getNano()
- 获取时间()
- 获取分钟()
- 获取秒()
- 获取纳米()
check this out, it helped me alot- http://tutorials.jenkov.com/java-date-time/localtime.html
看看这个,它帮助了我很多 - http://tutorials.jenkov.com/java-date-time/localtime.html