java Joda-Time,没有日期的时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15459001/
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
Joda-Time, Time without date
提问by Jedi Knight
I want a Class that only stores the time and not the date or day. Is there a class for this in Joda-Time ? or do I have to use a Date time and convert only the time part into a string and then use that part ?
我想要一个只存储时间而不是日期或日期的类。Joda-Time 有这方面的课程吗?还是我必须使用日期时间并仅将时间部分转换为字符串然后使用该部分?
回答by Bozho
There's the LocalTime
class for that purpose.
有LocalTime
这个目的的类。
Read more about partials here. E.g.:
LocalTime time = new LocalTime(12, 20);
String formatted = time.toString("HH:mm");
回答by Rahul Tripathi
回答by Stephan
Since JodaTime 2.0, it's also possible to instanciate a time without date like this:
从 JodaTime 2.0 开始,也可以像这样实例化一个没有日期的时间:
LocalTime time = LocalTime.parse( //
"12h20", //
DateTimeFormatter.forPattern("HH'h'mm") //
);