论坛首页 入门技术论坛

一些关于日期的操作方法(一)

浏览 1563 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-12-12   最后修改:2008-12-12

1、

/**
	 * 计算两个日期之间的间隔天数
	 * 
	 * @param dateStr1
	 * @param dateStr2
	 * @return 间隔天数
	 */
	public static long calDaysBetweenTwoDay(String dateStr1, String dateStr2) {
                                java.text.DateFormat shortFormat = new java.text.SimpleDateFormat(
			"yyyy-MM-dd");
		long ret = 0;
		try {
			if (dateStr1 != null && !dateStr1.equals("")
					&& dateStr1.length() >= 10 && dateStr2 != null
					&& !dateStr2.equals("") && dateStr2.length() >= 10) {
				Date date1 = shortFormat.parse(dateStr1.substring(0, 10));
				Date date2 = shortFormat.parse(dateStr2.substring(0, 10));
				ret = ((date2.getTime() - date1.getTime()) / (1000 * 60 * 60 * 24));
			}

		} catch (Exception e) {
			log.info("helper:calDaysBetweenTwoDay proces error!");
			e.printStackTrace();
		}
		return ret;
	}

 2.

/**
	 * 获取当前日期所在月的天数
	 * 
	 * @param date
	 * @return
	 */
	public static int getDaysOfMonth(Date date) {
		int ret = 0;
		try {
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			ret = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		} catch (Exception e) {
			// TODO: handle exception
			log.error("helper:get days of month process error!");
			e.printStackTrace();
		}
		return ret;
	}

 

论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics