Java:getMinutes 和 getHours
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/907170/
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
Java: getMinutes and getHours
提问by
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.
从那以后Date.getHours
,您如何获得小时和分钟并被Date.getMinutes
弃用?我在 Google 搜索中找到的示例使用了已弃用的方法。
回答by Mark
From the Javadoc for Date.getHours
来自 Date.getHours 的 Javadoc
As of JDK version 1.1, replaced by Calendar.get(Calendar.HOUR_OF_DAY)
As of JDK version 1.1, replaced by Calendar.get(Calendar.HOUR_OF_DAY)
So use
所以用
Calendar rightNow = Calendar.getInstance();
int hour = rightNow.get(Calendar.HOUR_OF_DAY);
and the equivalent for getMinutes.
和 getMinutes 的等价物。
回答by James
First, import java.util.Calendar
一、导入java.util.Calendar
Calendar now = Calendar.getInstance();
System.out.println(now.get(Calendar.HOUR_OF_DAY) + ":" + now.get(Calendar.MINUTE));
回答by Tobias Langner
Try Calender. Use getInstance to get a Calender-Object. Then use setTime to set the required Date. Now you can use get(int field) with the appropriate constant like HOUR_OF_DAY or so to read the values you need.
试试日历。使用 getInstance 获取日历对象。然后使用 setTime 设置所需的日期。现在您可以使用带有适当常量(如 HOUR_OF_DAY 左右)的 get(int field) 来读取您需要的值。
http://java.sun.com/javase/6/docs/api/java/util/Calendar.html
http://java.sun.com/javase/6/docs/api/java/util/Calendar.html
回答by alamar
I would recommend looking ad joda time. http://www.joda.org/joda-time/
我会建议寻找 ad joda 时间。 http://www.joda.org/joda-time/
I was afraid of adding another library to my thick project, but it's just easy and fast and smart and awesome. Plus, it plays nice with existing code, to some extent.
我害怕在我的繁重项目中添加另一个库,但它简单、快速、智能且很棒。另外,在某种程度上,它可以很好地与现有代码配合使用。
回答by Juha Syrj?l?
Try using Joda Timeinstead of standard java.util.Date classes. Joda Time library has much better API for handling dates.
尝试使用Joda Time而不是标准的 java.util.Date 类。Joda Time 库有更好的 API 来处理日期。
DateTime dt = new DateTime(); // current time
int month = dt.getMonth(); // gets the current month
int hours = dt.getHourOfDay(); // gets hour of day
See this questionfor pros and cons of using Joda Time library.
有关使用 Joda Time 库的利弊,请参阅此问题。
Joda Time may also be included to some future version of Java as a standard component, see JSR-310.
Joda Time 也可能作为标准组件包含在 Java 的某些未来版本中,请参阅JSR-310。
If you must use traditional java.util.Date and java.util.Calendar classes, see their JavaDoc's for help (java.util.Calendarand java.util.Date).
如果您必须使用传统的 java.util.Date 和 java.util.Calendar 类,请参阅它们的 JavaDoc 以获得帮助(java.util.Calendar和java.util.Date)。
You can use the traditional classes like this to fetch fields from given Date instance.
您可以使用像这样的传统类从给定的 Date 实例中获取字段。
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!
回答by skaffman
While I wouldn't recommend doing so, I think it's worth pointing out that although many methods on java.util.Date have been deprecated, they do still work. In trivial situations, it may be OK to use them. Also, java.util.Calendar is pretty slow, so getMonth and getYear on Date might be be usefully quicker.
虽然我不建议这样做,但我认为值得指出的是,虽然 java.util.Date 上的许多方法已被弃用,但它们仍然有效。在琐碎的情况下,使用它们可能没问题。此外,java.util.Calendar 非常慢,因此 getMonth 和 getYear on Date 可能会更快一些。
回答by Benjamin
import java.util.*
You can gethour and minute using calendar and formatter class. Calendar cal = Calendar.getInstance()
and Formatter fmt=new Formatter()
and set a format for display hour and minute fmt.format("%tl:%M",cal,cal)
and print System.out.println(fmt)
output shows like 10:12
import java.util.*
您可以使用日历和格式化程序类获取小时和分钟。Calendar cal = Calendar.getInstance()
并Formatter fmt=new Formatter()
设置显示小时和分钟的格式fmt.format("%tl:%M",cal,cal)
,打印System.out.println(fmt)
输出显示如下10:12
回答by J.D.
java.time
时间
While I am a fan of Joda-Time, Java 8introduces the java.timepackage which is finally a worthwhile Java standard solution! Read this article, Java SE 8 Date and Time, for a good amount of information on java.time outside of hours and minutes.
虽然我的粉丝乔达时,Java的8引入了java.time包,它是最后一个值得Java标准的解决方案!阅读这篇Java SE 8 Date and Time文章,了解有关 java.time 的大量时间和分钟之外的信息。
In particular, look at the LocalDateTime
class.
特别是看LocalDateTime
班级。
Hours and minutes:
小时和分钟:
LocalDateTime.now().getHour();
LocalDateTime.now().getMinute();
回答by Nouran El Kassas
int hr=Time.valueOf(LocalTime.now()).getHours();
int minutes=Time.valueOf(LocalTime.now()).getMinutes();
These functions will return int values in hours and minutes.
这些函数将以小时和分钟为单位返回 int 值。
回答by Huey
public static LocalTime time() {
LocalTime ldt = java.time.LocalTime.now();
ldt = ldt.truncatedTo(ChronoUnit.MINUTES);
System.out.println(ldt);
return ldt;
}
This works for me
这对我有用