不多说,直接上干货!
这是yyyy-MM-dd HH:mm:ss.S
GetIntervalDays.java
package zhouls.bigdata.DataFeatureSelection.sim; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class GetIntervalDays { public static void main(String[] args) throws ParseException { String s1 = "2017-01-16 22:28:11.0"; String s2 = "2017-04-03 16:49:57.0"; Date d1 = parseDate(s1);//解析成Date类型 Date d2 = parseDate(s2);//解析成Date类型 System.out.println(d1);//Mon Jan 16 22:28:11 CST 2017 System.out.println(d2);//Mon Apr 03 16:49:57 CST 2017 System.out.println(getIntervalDays(d1,d2));//相差的天数76 } /** * 把字符串解析成日期 * @param s * @return d * @throws ParseException */ public static Date parseDate(String s) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S"); Date d = sdf.parse(s); return d; } /** * 相差天数(隔24小时为相差1天,否则为0天) * @param fDate * @param oDate * @return d */ public static int getIntervalDays(Date fDate, Date oDate) { if (null == fDate || null == oDate) { return -1; } long intervalMilli = oDate.getTime() - fDate.getTime(); return (int) (intervalMilli / (24 * 60 * 60 * 1000)); } }