so i have this homework and this is the given problem:
Per-minute charging offers at 75 cents a minute from 5:01 am to 5:00 pm, and 90 cents a minute from 5:01 pm to 5:00 am.
this is my code:
double charge = 0;
Date date = new Date();
if(date.getHours() >= 5 && date.getHours() <= 17)
{
charge += 0.75 * date.getMinutes();
System.out.println(charge);
}
else
{
charge += 0.9 * date.getMinutes();
System.out.println(charge);
}
my problem is that the date.getMinutes() only gets the current minute of the current hours, it supposed to be the number of minutes from 5:01am up to the current time and the else statement likewise. any idea guys? thx alot!
Well, for this you can do
totalHours = date.getHours() - 5;
totalMinutes = (totalHours * 60) + date.getMinutes();
Similarly, you could write it for the other part.
As it has been pointed out that the above methods have been deprecated, here is how you can do it with the Calendar
Date date = new Date();
Calendar calendar = GregorianCalendar.getInstance();
calendat.setTime(date);
if(calendar.get(Calendar.HOUR_OF_DAY) > 5 && calendar.get(Calendar.HOUR_OF_DAY) < 17)
{
int totalHours = calendar.get(Calendar.HOUR_OF_DAY) - 5;
int totalMinutes = (totalHours * 60) + calendar.get(Calendar.MINUTE);
}
else
{
totalHours = 24 - calendar.get(HOUR_OF_DAY) + 5;
totalMinutes = (totalHours * 60) - (60 - calendar.get(Calendar.MINUTE);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.