时间转化天时分

package com;

import java.util.Calendar;
import java.util.Date;

public class DateUtils {

/**
* 计算传入的时间与当前时间相差的几天几时几分
* @param date 时间
* @return
*/
public static String dateConvertDHMByDate(Date date) {
if (date == null)
return null;
long currTime = System.currentTimeMillis() / 1000;
long day = (currTime - date.getTime() / 1000) / (3600 * 24);
long hour = ((currTime - date.getTime() / 1000) % (3600 * 24)) / 3600;
long minu = (((currTime - date.getTime() / 1000) % (3600 * 24)) % 3600) / 60;
return day + "天" + hour + "时" + minu + "分";
}

/**
* 根据传入的小时数转为化几天几时几分
* @param hours 小时数
* @return
*/
public static String dateConvertHmsByHours(double hours) {
hours = hours * 60 * 60;
long day = (long) hours / (3600 * 24);
long hour = (long) (hours % (3600 * 24)) / 3600;
long minu = (long) ((hours % (3600 * 24)) % 3600) / 60;
return day + "天" + hour + "时" + minu + "分";
}

public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
// cal.set(Calendar.DAY_OF_MONTH, -1);
cal.set(Calendar.HOUR_OF_DAY, -1);
cal.set(Calendar.MINUTE, -50);
Date date = cal.getTime();
String dateStr = dateConvertDHMByDate(date);
System.out.println(dateStr);

String dateStr2 = dateConvertHmsByHours(23);
System.out.println(dateStr2);
}
}

你可能感兴趣的:(时间)