转化为日期格式
public String formatTimeHMS(long time) {
return new SimpleDateFormat("HH:mm:ss").format(time);
}
public static String formatTimeYMD(long time) {
return new SimpleDateFormat("yyyy-MM-dd").format(time);
}
public static String formatTimeYMDHMSSSS(long time) {
return new SimpleDateFormat("yyyyMMddHHmmssSSS").format(time);
}
public static String formatTimeYMD_HMS_SSS(long time) {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(time);
}
获取past天之前的日期
public String getPastDate(int past) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
Date today = calendar.getTime();
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
return format.format(today);
}
将毫秒 换算成(天 时 分 秒 毫秒)
public static String formatTime(Long ms) {
int ss = 1000;
int mi = ss * 60;
Integer hh = mi * 60;
Integer dd = hh * 24;
Long day = ms / dd;
Long hour = (ms - day * dd) / hh;
long minute = (ms - day * dd - hour * hh) / mi;
long second = (ms - day * dd - hour * hh - minute * mi) / ss;
long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;
StringBuilder sb = new StringBuilder();
if(day > 0) {
sb.append(day).append("天");
}
if(hour > 0) {
sb.append(hour).append("小时");
}
if(minute > 0) {
sb.append(minute).append("分");
}
if(second > 0) {
sb.append(second).append("秒");
}
if(milliSecond > 0) {
sb.append(milliSecond).append("毫秒");
}
return sb.toString();
}