Java HHMM 格式的时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20326619/
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
Time in HHMM format
提问by Sachin Verma
I want to use time as a HHMM format, like in the military's 1600 hrs for 4 PM. I was curious: Is there a way of getting it so by some dateformatter?
我想使用时间作为 HHMM 格式,就像军队的 1600 小时下午 4 点。我很好奇:有没有办法通过某些日期格式化程序来获得它?
Feel free to recommend any third-party library or pure Java API solution.
随意推荐任何第三方库或纯 Java API 解决方案。
采纳答案by René Link
Use the java.text.SimpleDateFormat
Date date = new Date();
DateFormat format = new SimpleDateFormat("HHmm");
System.out.println(format.format(date));
回答by Mr.G
Try this:
尝试这个:
System.out.println(new SimpleDateFormat("HHmm").format(new Date()));
回答by Pradeep Kr Kaushal
You also can use Joda-Time http://www.joda.org/joda-time/userguide.html
您也可以使用 Joda-Time http://www.joda.org/joda-time/userguide.html
It provides a quality replacement for the Java date and time classes.
它为 Java 日期和时间类提供了高质量的替代品。
DateTime dateTime = new DateTime();
String hhmm=dateTime.toString("hhmm"));
回答by disrvptor
I use the Apache Commons Lang FastDateFormatand DateFormatUtilsfor formatting dates and times. FastDateFormat
is a drop in replacement for SimpleDateFormat
that is fast and thread safe and DateFormatUtils
is great for formatting ISO8601 dates and times.
我使用 Apache Commons Lang FastDateFormat和DateFormatUtils来格式化日期和时间。 FastDateFormat
是SimpleDateFormat
快速和线程安全的替代品,DateFormatUtils
非常适合格式化 ISO8601 日期和时间。
回答by Illia Loshchinin
回答by Ayush Chaurasia
LocalTime
LocalTime
Current time using java.time.LocalTimeand formatting using java.time.format.DateTimeFormatter( java 8)
当前时间使用java.time.LocalTime并使用java.time.format.DateTimeFormatter( java 8 ) 进行格式化
LocalTime time = LocalTime.now();
String t = time.format(DateTimeFormatter.ofPattern("HH:mm"));
System.out.println(t);
//output
05:41