在java日期时间类型对象的应用中最常见也是最容易出错的应用是对日期和时间变量进行相互之间的计算操作,我们总结了一下,大概可以归结为以下操作类型。
1、计算两个日期之间的差(用毫秒、秒、分钟、小时、天表示)
2、在某个日期的基础上加上几天或者几小时后的日期
我们还是先看代码
- /**
- * 日期之间的求差的计算
- *
- */
- public static void diffDate(){
- Date currentDate;//当前系统日期
- Date firstDate; //用于比较的日期
- Calendar currentCalendar;
- Calendar firstCalendar;
- //创建日期对象
- currentDate = new Date();
- firstDate = getFormatString2Date("2009-05-29", "yyyy-MM-dd");
- //将Date对象转化成Calendar对象以便于计算
- currentCalendar = Calendar.getInstance();
- firstCalendar = Calendar.getInstance();
- currentCalendar.setTime(currentDate);
- firstCalendar.setTime(firstDate);
- long diffmills = currentCalendar.getTimeInMillis() - firstCalendar.getTimeInMillis();
- long diffm = diffmills/1000;
- long diffs = diffmills/(1000*60);
- long diffh = diffmills/(1000*60*60);
- long diffd = diffmills/(1000*60*60*24);
- System.out.println(diffmills + " 毫秒");
- System.out.println(diffm + " 秒");
- System.out.println(diffs + " 分钟");
- System.out.println(diffh + " 小时");
- System.out.println(diffd + " 天");
- }
- /**
- * 某个日期加上某个时间单元后的日期
- *
- */
- public static void addDate(){
- Date currentDate;//当前系统日期
- Calendar currentCalendar;
- //创建日期对象
- currentDate = new Date();
- //将Date对象转化成Calendar对象以便于计算
- currentCalendar = Calendar.getInstance();
- //currentDate加上100毫秒后到日期
- currentCalendar.setTime(currentDate);
- currentCalendar.add(Calendar.MILLISECOND, 100);
- Date addDateMills = currentCalendar.getTime();
- //currentDate加上50秒后到日期
- currentCalendar.setTime(currentDate);
- currentCalendar.add(Calendar.SECOND, 50);
- Date addDateS = currentCalendar.getTime();
- //currentDate加上20分钟后到日期
- currentCalendar.setTime(currentDate);
- currentCalendar.add(Calendar.MINUTE, 20);
- Date addDateM = currentCalendar.getTime();
- //currentDate加上一个小时后到日期
- currentCalendar.setTime(currentDate);
- currentCalendar.add(Calendar.HOUR_OF_DAY, 1);
- Date addDateH = currentCalendar.getTime();
- //currentDate加上一天后的日期
- currentCalendar.setTime(currentDate);
- currentCalendar.add(Calendar.DAY_OF_YEAR, 1);
- Date addDateDay = currentCalendar.getTime();
- //currentDate加上一周后的日期
- currentCalendar.setTime(currentDate);
- currentCalendar.add(Calendar.WEEK_OF_YEAR, 1);
- Date addDateWeek = currentCalendar.getTime();
- //currentDate加上一个月后的日期
- currentCalendar.setTime(currentDate);
- currentCalendar.add(Calendar.MONTH, 1);
- Date addDateMonth = currentCalendar.getTime();
- String currentDateString = getDate2FormatString(currentDate,"yyyy-MM-dd HH:mm:ss S");
- System.out.println("日期:"+currentDateString + " 加上" + " 100 毫秒 后的日期为 -> "+getDate2FormatString(addDateMills,"yyyy-MM-dd HH:mm:ss S"));
- System.out.println("日期:"+currentDateString + " 加上" + " 50 秒 后的日期为 -> "+getDate2FormatString(addDateS,"yyyy-MM-dd HH:mm:ss S"));
- System.out.println("日期:"+currentDateString + " 加上" + " 20 分钟 后的日期为 -> "+getDate2FormatString(addDateM,"yyyy-MM-dd HH:mm:ss S"));
- System.out.println("日期:"+currentDateString + " 加上" + " 1 小时 后的日期为 -> "+getDate2FormatString(addDateH,"yyyy-MM-dd HH:mm:ss S"));
- System.out.println("日期:"+currentDateString + " 加上" + " 1 天 后的日期为 -> "+getDate2FormatString(addDateDay,"yyyy-MM-dd HH:mm:ss S"));
- System.out.println("日期:"+currentDateString + " 加上" + " 1 周 后的日期为 -> "+getDate2FormatString(addDateWeek,"yyyy-MM-dd HH:mm:ss S"));
- System.out.println("日期:"+currentDateString + " 加上" + " 1 月 后的日期为 -> "+getDate2FormatString(addDateMonth,"yyyy-MM-dd HH:mm:ss S"));
- }
- /**
- * 格式化日期时间信息
- * @param userDate 日期对象
- * @param formatString 格式haunted字符串
- * @return
- */
- public static String getDate2FormatString(Date userDate, String formatString) {
- StringBuffer dateformatstring = new StringBuffer();
- // check formatString whether null
- if ((null == formatString) && (formatString.length() < 0))
- throw new DateUtilException(
- "Function getDate2FormatString error : not find date format");
- // check userDate whether
- if (null == userDate)
- return null;
- SimpleDateFormat dateFormat = new SimpleDateFormat(formatString);
- dateFormat.format(userDate, dateformatstring, new FieldPosition(0));
- return dateformatstring.toString();
- }
- /**
- * 将日期字符串转化为日期对象
- * @param str 日期字符串
- * @param dtfm 格式字符串
- * @return
- */
- public static Date getFormatString2Date(String str, String dtfm) {
- return new Date();
- }
通过上述代码我们可以发现,其实时间日期相关的增减操作不是很复杂,只要掌握的Calendar对象的一些基本用法,基本上能很快的计算出来,上述代码中getFormatString2Date方法留给大家自己实现,根据我们前面所学的知识应该很容易的就可以实现。
如果您对我的文章感兴趣的话,请点击这里加我为好友,让我们一起进步
http://student.youkuaiyun.com/invite.php?u=106708&c=2383a3846076c876