Reputation: 35
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".
How do I format the datetime to yyyy/mm/dd hh:mm and add 8 hours in the time display? After this I need to getHours()
for some compare function.
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)
{
...
}
Upvotes: 1
Views: 141
Reputation: 1110
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());
Upvotes: 1