继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

计算两个日期之间相隔天数

python玄
关注TA
已关注
手记 3
粉丝 4
获赞 26

直接上传代码:

import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.text.ParseException;
public class CalendarDemo {
    public static void main(String[] args) throws ParseException {
        Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);
        int second = c.get(Calendar.SECOND);
        System.out.println("今天是:"+year+"年"+month+"月"+day+"日"+" "
                            +hour+":"+minute+":"+second);
        String hDay = "2017/09/10 12:00:00";
        String qDay = "2016/08/17 13:00:14";
        long days = calcDays(hDay, qDay);
        System.out.println(days);
    }
    public static long calcDays(String date1,String date2) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/ss HH:mm:ss");
        Calendar c = Calendar.getInstance();
        c.setTime(sdf.parse(date1));
        long time1 = c.getTimeInMillis();
        c.setTime(sdf.parse(date2));
        long time2 = c.getTimeInMillis();       
        return (time1-time2)/(24*3600*1000);
    }
}
打开App,阅读手记
3人推荐
发表评论
随时随地看视频慕课网APP

热门评论

不同月份的天数问题,系统已经帮助解决了

用Period几行代码搞定

查看全部评论