最近项目的需求,微博征信贷中首单模型在开发过程中用到了部分时间差特征和一些与订单相关的信息来提高模型效果,时间差要求保留两位小数的天数,代码如下:
public static float decimalFormat(String pattern, double value) {
return Float.parseFloat(new DecimalFormat(pattern).format(value));
}
/**
* 通过时间秒毫秒数判断两个时间的间隔
*
* @param date1
* @param date2
* @return
*/
public static float differentDaysByMillisecond(Date date1, Date date2) {
long diffSeconds = date2.getTime() - date1.getTime();
long diffHour = diffSeconds / (1000 * 3600);
System.out.println("获得小时:" + diffHour);
float diffDay = (float) diffHour / 24;
float floatDay = decimalFormat("0.00", diffDay);
System.out.println(decimalFormat("0", diffDay));
return floatDay;
}
public static void main(String[] args) {
String dateStr = "2008-1-1 1:21:28";
String dateStr2 = "2008-2-1 2:21:28";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date date2 = format.parse(dateStr2);
Date date = format.parse(dateStr);
System.out.println("两个日期的差距:" + differentDaysByMillisecond(date, date2));
} catch (ParseException e) {
e.printStackTrace();
}
}