Java ---MyUtil工具类

1.比较一个yyyy-MM-dd类型的字符串日期与当前日期的时间差
public static int between(String s) throws ParseException{
// 我这里参数日期大于当前日期,否则计算式负数(你可以Math.abs(long)取绝对值)
   SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
   Calendar c = Calendar.getInstance();
   c.setTime(sdf.parse(s));
   long star = c.getTimeInMillis();
   Date d = new Date();
   c.setTime(sdf.parse(sdf.format(d)));
   long end =  c.getTimeInMillis();
   int a = Integer.parseInt(String.valueOf((star - end )/(1000*3600*24)));
   return a;
}

猜你喜欢

转载自mxl421204733.iteye.com/blog/2295771