How can I get 4 days 3 hours 45 minutes from 4.46875?
I get the answer in this form, I need to convert to the number of days, hours, minutes ...
2 answers
For example :
double rawDays = 4.46875; int days = (int)Math.floor(rawDays); double rawRestHours = (rawDays - days) * 24; int hours = (int)Math.floor(rawRestHours); double rawRestMinutes = (rawRestHours - hours) * 60; int minutes = (int)Math.floor(rawRestMinutes); double restSeconds = (rawRestMinutes - minutes) * 60;
PS: In Java 8 there will be a data type representing a time period.
- are you serious? such a way in java? - Gorets 6:01 pm
- @Gorets: Is there a better option? (without using Joda Time) - VladD
- one@Gorets: Not so wrong.
Calendar
represents a point in time, not a period . Therefore, there may be fun, for example, during the transition to summer time or there in leap years. (If our time period is several years long.) - VladD - one@VladD
floor(0.46875*24)=11
hours - as it does not walk out ... - Barmaley - one@Barmaley: why not dance? 4.46 days is a little less than 4 and a half days, half a day is just 12 hours. - VladD
|
System.out.println(String.format( "%1$te дня %1$tH час(а/ов) %1$tM минут(а/ы)", new GregorianCalendar(0, 0, 0, 0, 0, (int) (4.46875 * 24 * 3600))));
- oneWrong: ideone.com/1qpXP4 - VladD
|
return "4 дня 3 часа 45 минут";
, about the other values of the argument says nothing . - VladD