你如何得到小时和分钟,Date.getHours并被Date.getMinutes弃用?我在Google搜索中找到的示例使用了弃用的方法.
Juh*_*älä 164
尝试使用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
Run Code Online (Sandbox Code Playgroud)
有关使用Joda Time库的优缺点,请参阅此问题.
Joda Time也可能作为标准组件包含在Java的未来版本中,请参阅JSR-310.
如果必须使用传统的java.util.Date和java.util.Calendar类,请参阅JavaDoc的帮助(java.util.Calendar和java.util.Date).
您可以使用这样的传统类从给定的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!
Run Code Online (Sandbox Code Playgroud)
Mar*_*ark 88
来自Javadoc for Date.getHours
As of JDK version 1.1, replaced by Calendar.get(Calendar.HOUR_OF_DAY)
所以使用
Calendar rightNow = Calendar.getInstance();
int hour = rightNow.get(Calendar.HOUR_OF_DAY);
Run Code Online (Sandbox Code Playgroud)
和getMinutes的等价物.
J.D*_*.D. 52
虽然我的粉丝乔达时,Java的8引入了java.time包,它是最后一个值得Java标准的解决方案!阅读本文Java SE 8日期和时间,获取有关小时和分钟之外的java.time的大量信息.
特别是,看看LocalDateTime课程.
小时和分钟:
LocalDateTime.now().getHour();
LocalDateTime.now().getMinute();
Run Code Online (Sandbox Code Playgroud)
Jam*_*mes 36
首先,导入java.util.Calendar
Calendar now = Calendar.getInstance();
System.out.println(now.get(Calendar.HOUR_OF_DAY) + ":" + now.get(Calendar.MINUTE));
Run Code Online (Sandbox Code Playgroud)
ZonedDateTime.now().getHour()
Run Code Online (Sandbox Code Playgroud)
… 要么 …
LocalTime.now().getHour()
Run Code Online (Sandbox Code Playgroud)
ZonedDateTimeJD的答案是好的,但不是最佳的。该答案使用LocalDateTime该类。缺少任何时区或UTC偏移量的概念,该类不能表示一个时刻。
更好地使用ZonedDateTime。
ZoneId z = ZoneID.of( "America/Montreal" ) ;
ZonedDateTime zdt = ZonedDateTime.now( z ) ;
Run Code Online (Sandbox Code Playgroud)
如果忽略该ZoneId参数,则会在运行时使用JVM的当前默认时区隐式应用一个参数。
所以这:
ZonedDateTime.now()
Run Code Online (Sandbox Code Playgroud)
…与此相同:
ZonedDateTime.now( ZoneId.systemDefault() )
Run Code Online (Sandbox Code Playgroud)
最好明确一点,超过您希望的/预期的时区。默认值可以在运行时随时更改。
如果紧急,请与用户确认时区。
询问ZonedDateTime小时和分钟。
int hour = zdt.getHour() ;
int minute = zdt.getMinute() ;
Run Code Online (Sandbox Code Playgroud)
LocalTime如果您只需要没有时区的时间,请提取LocalTime。
LocalTime lt = zdt.toLocalTime() ;
Run Code Online (Sandbox Code Playgroud)
或ZonedDateTime完全跳过,直接转到LocalTime。
LocalTime lt = LocalTime.now( z ) ; // Capture the current time-of-day as seen in the wall-clock time used by the people of a particular region (a time zone).
Run Code Online (Sandbox Code Playgroud)

该java.time框架是建立在Java 8和更高版本。这些类取代麻烦的老传统日期时间类,如java.util.Date,Calendar,和SimpleDateFormat。
要了解更多信息,请参见Oracle教程。并在Stack Overflow中搜索许多示例和说明。规格为JSR 310。
现在处于维护模式的Joda-Time项目建议迁移到java.time类。
您可以直接与数据库交换java.time对象。使用与JDBC 4.2或更高版本兼容的JDBC驱动程序。不需要字符串,不需要类。java.sql.*
在哪里获取java.time类?
该ThreeTen-额外项目与其他类扩展java.time。该项目为将来可能在java.time中添加内容提供了一个试验场。你可能在这里找到一些有用的类,比如Interval,YearWeek,YearQuarter,和更多。
试试日历.使用getInstance获取Calender-Object.然后使用setTime设置所需的日期.现在你可以使用get(int field)和HOUR_OF_DAY之类的相应常量来读取你需要的值.
http://java.sun.com/javase/6/docs/api/java/util/Calendar.html
小智 5
另一种获取分钟和小时的方法是使用 SimpleDateFormat。
SimpleDateFormat formatMinutes = new SimpleDateFormat("mm")
String getMinutes = formatMinutes.format(new Date())
SimpleDateFormat formatHours = new SimpleDateFormat("HH")
String getHours = formatHours.format(new Date())
Run Code Online (Sandbox Code Playgroud)