求出字符“2013-9-8”与“2013-10-1”相差的天数
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- 将夜www.jiangyea.com
- public class DayCount {
- //lday为小日期bday为大时间
- public static int daysBetween(String lday,String bday) throws ParseException{
- SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
- Calendar cal = Calendar.getInstance();
- cal.setTime(sdf.parse(lday));
- long time1 = cal.getTimeInMillis();
- cal.setTime(sdf.parse(bday));
- long time2 = cal.getTimeInMillis();
- //除一天的毫秒数转换成天
- long betweenDays=(time2-time1)/(1000*3600*24);
- return Integer.parseInt(String.valueOf(betweenDays));
- }
- public static void main(String[] args) throws ParseException {
- SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- Date d1=sdf.parse("2013-09-08 10:10:10");
- Date d2=sdf.parse("2013-10-01 00:00:00");
- System.out.println(daysBetween("2013-09-08 10:10:10","2013-10-01 00:00:00"));
- }
- }
重生之大文豪 www.dwhao.com