Java:getMinutes和getHours [英] Java: getMinutes and getHours
问题描述
从 Date.getHours
和 Date.getMinutes
已被弃用,您如何获得小时和分钟?我在Google搜索中发现的示例使用了已弃用的方法。
How do you get Hours and Minutes since Date.getHours
and Date.getMinutes
got deprecated? The examples that I found on Google search used the deprecated methods.
推荐答案
尝试使用 Joda Time ,而不是标准的java.util.Date类。 Joda Time库具有更好的处理日期的API。
Try using Joda Time instead of standard java.util.Date classes. Joda Time library has much better API for handling dates.
DateTime dt = new DateTime(); // current time
int month = dt.getMonth(); // gets the current month
int hours = dt.getHourOfDay(); // gets hour of day
看到这个问题使用的利弊Joda时间库。
See this question for pros and cons of using Joda Time library.
Joda时间也可能包含在Java的未来版本中作为标准组件,请参阅 JSR-310 。
Joda Time may also be included to some future version of Java as a standard component, see JSR-310.
如果您必须使用传统的java.util.Date和java.util.Calendar类,请参阅他们的JavaDoc的帮助( java.util.Calendar 和 java.util.Date )。
If you must use traditional java.util.Date and java.util.Calendar classes, see their JavaDoc's for help (java.util.Calendar and java.util.Date).
您可以使用这样的传统类从给定的Date实例获取字段。
You can use the traditional classes like this to fetch fields from given Date instance.
Date date = new Date(); // given date
Calendar calendar = GregorianCalendar.getInstance(); // creates a new calendar instance
calendar.setTime(date); // assigns calendar to given date
calendar.get(Calendar.HOUR_OF_DAY); // gets hour in 24h format
calendar.get(Calendar.HOUR); // gets hour in 12h format
calendar.get(Calendar.MONTH); // gets month number, NOTE this is zero based!
这篇关于Java:getMinutes和getHours的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!