[英]need help on datetime function in java
I am trying to using the getHours()
function in the Date
class in Java, but it always shows "The method getHours()
from the type Date
is deprecated". 我试图在Java的
Date
类中使用getHours()
函数,但是它始终显示“不赞成使用Date
类型的getHours()
方法”。
How do I format the datetime to yyyy/mm/dd hh:mm and add 8 hours in the time display? 如何将日期时间格式化为yyyy / mm / dd hh:mm并在时间显示中增加8小时? After this I need to
getHours()
for some compare function. 此后,我需要
getHours()
进行一些比较功能。 My code is given below, but has some errors and doesn't work... 我的代码在下面给出,但是有一些错误,无法正常工作...
SimpleDateFormat Dateformat = new SimpleDateFormat( "yyyy/MM/dd HH:mm" );
Date dateA = new Date();
Date in8Hours = new Date( dateA.getTime() + 8L * Timer.ONE_HOUR );
String systimeNow = Dateformat.format( in8Hours ).toString();
Date systimeNowA = Dateformat.parse( systimeNow );
if (systimeNowA.getHours() >= 13)
{
...
}
Use Calendar instead, which is not deprecated: 请改用不建议使用的日历:
Calendar systimeNowA = Calendar.getInstance();
int hour = systimeNowA .get(Calendar.HOUR_OF_DAY);
How to get the format: 如何获取格式:
SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy/MM/dd HH:mm");
String formattedOutput= dateFormat.format(systimeNowA .getTime());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.