我有几天的格式,如22.5天 我想使用java
将其转换为22天12小时在上述问题中需要帮助。 提前谢谢
答案 0 :(得分:6)
您可以使用SimpleDateFormat,但简单的数学可能更简单。
double days = 22.5;
long hours = Math.round(days * 24);
String text = hours/24 + " days, " + hours % 24 +" hours.";
添加分钟和秒即可。
double days = 22.5;
long seconds = Math.round(days * 86400);
String text = seconds/86400 + " days, "
+ seconds / 3600 % 24 + " hours, "
+ seconds / 60 % 60 + " minutes, "
+ seconds % 60 + " second.";
使用SimpleDateFormat
SimpleDateFormat SDF = new SimpleDateFormat("dd' days, 'hh' hours'");
SDF.setTimeZone(TimeZone.getTimeZone("GMT"));
String text = SDF.format(new Date(Math.round(days * 86400000)));
注意:SimpleDateFormat最多只能使用31天。 ;)
答案 1 :(得分:1)
这是一个模糊的答案,但只是为了证明你很简单:
int a=22.5,hr,days;
int days=22.5;
float t1=22.5;
(t1-days)*24=...
:)