本文实例讲述了Android编程实现计算两个日期之间天数并打印所有日期的方法。分享给大家供大家参考,具体如下:
以下代码是计算两个日期之间的天数,并打印所有日期
注:开始时,增加天数时,一天的毫秒数直接用24 * 60 * 60 * 1000来逐步增加天数,再测试时发现,当两个日期之间的天数超过24天时,打印的日期反而在开始日期之前了,(如打印2016/12/18-2017/1/23,打印的日期反而有2016/12/1),后来发现原因在于24 * 60 * 60 * 1000是一个int值,int值的取值范围在2的31次方:+/- 2147483648,当超过最大数时,就会变成最小的数,这样反而导致日期变小,将24 * 60 * 60 * 1000变长为类型的值即可:私人长静态最终>/* * *计算两个日期之间的日期 * @param开始时间 * @param endTime */私人空间betweenDays (mills_select endTime开始时间长,长,长,int代码){ 日期date_start=new日期(开始时间); 日期date_end=new日期(endTime);//计算日期从开始时间于结束时间的0时计算 日历fromCalendar=Calendar.getInstance (); fromCalendar.setTime (date_start); fromCalendar.set(日历。HOUR_OF_DAY, 0); fromCalendar.set(日历。分钟,0); fromCalendar.set(日历。第二,0); fromCalendar.set(日历。毫秒,0); 日历toCalendar=Calendar.getInstance (); toCalendar.setTime (date_end); toCalendar.set(日历。HOUR_OF_DAY, 0); toCalendar.set(日历。分钟,0); toCalendar.set(日历。第二,0); toCalendar.set(日历。毫秒,0); int s=(int) ((toCalendar.getTimeInMillis ()——fromCalendar.getTimeInMillis ())/(ONE_DAY_MS)); 如果(s> 0) { for (int i=0; i<=s;我+ +){ 长todayDate=fromCalendar.getTimeInMillis () + i *>/* * *格式化传入的时间 * * @param时间需要格式化的时间 * @param formatStr格式化的格式 * @return */公共静态字符串getCustonFormatTime(长时间,字符串formatStr) { SimpleDateFormat格式=new SimpleDateFormat (formatStr); 日期d1=new日期(时间); 返回format.format (d1); } >之前
http://tools.jb51.net/jisuanqi/date_jisuanqi
http://tools.jb51.net/bianmin/wannianli
http://tools.jb51.net/bianmin/yinli2yangli
http://tools.jb51.net/code/unixtime
更多关于Android相关内容感兴趣的读者可查看本站专题:《Android日期与时间操作技巧总结》,《Android开发入门与进阶教程》、《Android基本组件用法总结》,《Android视图视图技巧总结》,《Android布局布局技巧总结》及《安卓控件用法总结》
希望本文所述对大家Android程序设计有所帮助。