Get total of hours on a day

Vrian7 :

Working with Java, I'm parsing a integer DAYS to HOURS.

Looks like this:

float hours = myvardays*24;

It works, but constant 24 is a magic number, and best avoided.

Is there any constant on Calendar, Date or any other to get total hours in a day (24)? or maybe a method?

I am interested only in generic 24-hours, ignoring anomalies such as Daylight Saving Time (DST) that result in other day lengths.

drekbour :

Let's not go reinventing stuff, this is all baked into the JDK:

Java 8 and later

Use Duration.ofDays with toDays method.

long hours = Duration.ofDays(myvardays).toHours(); // Java 8+

Java 5, 6, & 7

Use the TimeUnit enum.

long hours = TimeUnit.DAYS.toHours(myvardays); // Java 5+

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=329751&siteId=1