| | |
| | | return dateInfo;
|
| | | }
|
| | |
|
| | | public static String dateDiff4(Date startTime, Date endTime) throws Exception {
|
| | |
|
| | | String datatime = "0分";
|
| | |
|
| | | long nm = 1000 * 60;// 一分钟的毫秒数
|
| | | long nh = 1000 * 60 * 60;// 一小时的毫秒数
|
| | | long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
|
| | |
|
| | | // 获得两个时间的毫秒时间差异
|
| | | long diff = endTime.getTime() - startTime.getTime();
|
| | | long day = diff / nd;// 计算差多少天
|
| | | long hour = diff % nd / nh;// 计算差多少小时
|
| | | long min = diff % nd % nh / nm;// 计算差多少分钟
|
| | |
|
| | | if (day > 0) {
|
| | | datatime = day + "天";
|
| | | } else {
|
| | | if (hour > 0) {
|
| | | datatime = hour + "时";
|
| | | } else {
|
| | | if (min < 0)
|
| | | min = 0;
|
| | | datatime = min + "分";
|
| | | }
|
| | | }
|
| | | return datatime;
|
| | | }
|
| | |
|
| | | public static String dateDiff5(Date startTime, Date endTime) throws Exception {
|
| | | long nm = 1000 * 60;// 一分钟的毫秒数
|
| | | long nh = 1000 * 60 * 60;// 一小时的毫秒数
|
| | | long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
|
| | |
|
| | | // 获得两个时间的毫秒时间差异
|
| | | long diff = endTime.getTime() - startTime.getTime();
|
| | | long day = diff / nd;// 计算差多少天
|
| | | long hour = diff % nd / nh;// 计算差多少小时
|
| | | long min = diff % nd % nh / nm;// 计算差多少分钟
|
| | | long second = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60); // 计算差多少秒
|
| | |
|
| | | String datatime = "";
|
| | | if (day > 0) {
|
| | | datatime = day + "天";
|
| | | } else if (hour > 0) {
|
| | | datatime = hour + "小时";
|
| | | } else if (min > 0) {
|
| | | datatime = min + "分钟";
|
| | | } else if (second > 0) {
|
| | | datatime = second + "秒";
|
| | | }
|
| | | return datatime;
|
| | | }
|
| | |
|
| | | public static long dateDiffMin(Date startTime, Date endTime) throws Exception {
|
| | | long nm = 1000 * 60;// 一分钟的毫秒数
|
| | | long nh = 1000 * 60 * 60;// 一小时的毫秒数
|
| | | long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
|
| | |
|
| | | // 获得两个时间的毫秒时间差异
|
| | | long diff = endTime.getTime() - startTime.getTime();
|
| | | long day = diff / nd;// 计算差多少天
|
| | | long hour = diff % nd / nh;// 计算差多少小时
|
| | | long min = diff % nd % nh / nm + day * 24 * 60 + hour * 60;// 计算差多少分钟
|
| | | return min;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 通过时间秒毫秒数判断两个时间的间隔
|
| | | *
|
| | |
| | | * @return
|
| | | * @throws ParseException
|
| | | */
|
| | | public static Date plusDayReturnDate(int num, String nowDate) throws ParseException {
|
| | | SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
|
| | | Date currdate = format.parse(nowDate);
|
| | | Calendar ca = Calendar.getInstance();
|
| | | ca.setTime(currdate);
|
| | | ca.add(Calendar.DATE, num);
|
| | | return ca.getTime();
|
| | | }
|
| | |
|
| | | /**
|
| | | * 指定日期加上天数后的日期
|
| | | * |
| | | * @param num 增加的天数
|
| | | * @param nowDate 创建时间
|
| | | * @return
|
| | | * @throws ParseException
|
| | | */
|
| | | public static String plusDay(int num, Date currdate) throws ParseException {
|
| | | SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
|
| | |
|
| | |
| | | ca.add(Calendar.DATE, num);
|
| | | return ca.getTime();
|
| | | }
|
| | | |
| | |
|
| | | /**
|
| | | * 加上月份
|
| | | * |
| | | * @param currdate
|
| | | * @param num
|
| | | * @return
|
| | |
| | | ca.add(Calendar.MONTH, num);
|
| | | return ca.getTime();
|
| | | }
|
| | | |
| | |
|
| | | /**
|
| | | * 加上年份
|
| | | * |
| | | * @param currdate
|
| | | * @param num
|
| | | * @return
|
| | |
| | | return ca.getTime();
|
| | | }
|
| | |
|
| | | /**
|
| | | * 减去月份
|
| | | * |
| | | * @param currdate
|
| | | * @param num
|
| | | * @return
|
| | | */
|
| | | public static Date reduceMonth(Date currdate, int num) {
|
| | | Calendar ca = Calendar.getInstance();
|
| | | ca.setTime(currdate);
|
| | | ca.add(Calendar.MONTH, -num);
|
| | | return ca.getTime();
|
| | | }
|
| | | |
| | | public static Date reduceDay(Date currdate, int num) {
|
| | | Calendar ca = Calendar.getInstance();
|
| | | ca.setTime(currdate);
|
| | | ca.add(Calendar.DATE, -num);
|
| | | return ca.getTime();
|
| | | }
|
| | |
|
| | | /**
|
| | | * 指定日期减去天数后的日期
|
| | |
| | | public static String reduceDay(int num, String nowDate) throws ParseException {
|
| | | SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
|
| | | Date currdate = format.parse(nowDate);
|
| | |
|
| | | Calendar ca = Calendar.getInstance();
|
| | | ca.setTime(currdate);
|
| | | ca.add(Calendar.DATE, -num); // 日期减 如果不够减会将月变动
|
| | |
|
| | | return format.format(ca.getTime());
|
| | | }
|
| | |
|
| | | /**
|
| | | * 指定日期减去天数后的日期
|
| | | * |
| | | * @param num 减去的天数
|
| | | * @param nowDate 创建时间
|
| | | * @return
|
| | | * @throws ParseException
|
| | | */
|
| | | public static String reduceDay2(int num, Date date) throws ParseException {
|
| | | // 设置要获取到什么样的时间
|
| | | SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
|
| | | // 获取String类型的时间
|
| | | String date_str = format.format(date);
|
| | | Date currdate = format.parse(date_str);
|
| | |
|
| | | Calendar ca = Calendar.getInstance();
|
| | | ca.setTime(currdate);
|
| | |
| | | * 计算两时间月差
|
| | | *
|
| | | * @param startDate <String>
|
| | | * @param endDate <String>
|
| | | * @param endDate <String>
|
| | | * @return int
|
| | | * @throws ParseException
|
| | | */
|
| | | public static int getMonthSpace(String startDate, String endDate) throws ParseException {
|
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
|
| | | return getMonthSpace(sdf.parse(startDate), sdf.parse(endDate));
|
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
|
| | | return getMonthSpace(sdf.parse(startDate), sdf.parse(endDate));
|
| | | }
|
| | |
|
| | | /**
|
| | | * 计算两时间月差
|
| | | *
|
| | | * @param startDate <String>
|
| | | * @param endDate <String>
|
| | | * @param endDate <String>
|
| | | * @return int
|
| | | * @throws ParseException
|
| | | */
|
| | |
| | | start.setTime(startDate);
|
| | | end.setTime(endDate);
|
| | | int result = end.get(Calendar.MONTH) - start.get(Calendar.MONTH);
|
| | | int month = (end.get(Calendar.YEAR) - start.get(Calendar.YEAR)) * 12;
|
| | | int month = (end.get(Calendar.YEAR) - start.get(Calendar.YEAR)) * 12;
|
| | | return Math.abs(month + result);
|
| | | }
|
| | | |
| | |
|
| | | public static Date getTodayStartTime() {
|
| | | Calendar todayStart = Calendar.getInstance();
|
| | | todayStart.set(Calendar.HOUR_OF_DAY,0);
|
| | | todayStart.set(Calendar.MINUTE,0);
|
| | | todayStart.set(Calendar.SECOND,0);
|
| | | todayStart.set(Calendar.MILLISECOND,0);
|
| | | todayStart.set(Calendar.HOUR_OF_DAY, 0);
|
| | | todayStart.set(Calendar.MINUTE, 0);
|
| | | todayStart.set(Calendar.SECOND, 0);
|
| | | todayStart.set(Calendar.MILLISECOND, 0);
|
| | | return todayStart.getTime();
|
| | | }
|
| | | |
| | |
|
| | | public static Date getTodayEndTime() {
|
| | | Calendar todayEnd = Calendar.getInstance();
|
| | | todayEnd.set(Calendar.HOUR_OF_DAY,23);
|
| | | todayEnd.set(Calendar.MINUTE,59);
|
| | | todayEnd.set(Calendar.SECOND,59);
|
| | | todayEnd.set(Calendar.MILLISECOND,999);
|
| | | todayEnd.set(Calendar.HOUR_OF_DAY, 23);
|
| | | todayEnd.set(Calendar.MINUTE, 59);
|
| | | todayEnd.set(Calendar.SECOND, 59);
|
| | | todayEnd.set(Calendar.MILLISECOND, 999);
|
| | | return todayEnd.getTime();
|
| | | }
|
| | | |
| | |
|
| | | /**
|
| | | * 获取指定年月的第一天
|
| | | * |
| | | * @param yearMonth
|
| | | * @return
|
| | | */
|
| | | public static String getFirstDayOfMonth(String yearMonth) {
|
| | | int year = Integer.parseInt(yearMonth.split("-")[0]); // 年
|
| | | int month = Integer.parseInt(yearMonth.split("-")[1]); // 月
|
| | | return getFirstDayOfMonth(year, month);
|
| | | }
|
| | |
|
| | | /**
|
| | | * 获取指定年月的第一天
|
| | | * |
| | | * @param year
|
| | | * @param month
|
| | | * @return
|
| | | */
|
| | | public static String getFirstDayOfMonth(int year, int month) {
|
| | | Calendar cal = Calendar.getInstance();
|
| | | // 设置年份
|
| | | cal.set(Calendar.YEAR, year);
|
| | | // 设置月份
|
| | | cal.set(Calendar.MONTH, month - 1);
|
| | | // 获取某月最小天数
|
| | | int firstDay = cal.getMinimum(Calendar.DATE);
|
| | | // 设置日历中月份的最小天数
|
| | | cal.set(Calendar.DAY_OF_MONTH, firstDay);
|
| | | // 格式化日期
|
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
| | | return sdf.format(cal.getTime());
|
| | | }
|
| | |
|
| | | /**
|
| | | * 获取指定年月的最后一天
|
| | | * |
| | | * @param yearMonth
|
| | | * @return
|
| | | */
|
| | | public static String getLastDayOfMonth(String yearMonth) {
|
| | | int year = Integer.parseInt(yearMonth.split("-")[0]); // 年
|
| | | int month = Integer.parseInt(yearMonth.split("-")[1]); // 月
|
| | | return getLastDayOfMonth(year, month);
|
| | | }
|
| | |
|
| | | /**
|
| | | * 获取指定年月的最后一天
|
| | | * |
| | | * @param year
|
| | | * @param month
|
| | | * @return
|
| | | */
|
| | | public static String getLastDayOfMonth(int year, int month) {
|
| | | Calendar cal = Calendar.getInstance();
|
| | | // 设置年份
|
| | | cal.set(Calendar.YEAR, year);
|
| | | // 设置月份
|
| | | cal.set(Calendar.MONTH, month - 1);
|
| | | // 获取某月最大天数
|
| | | int lastDay = cal.getActualMaximum(Calendar.DATE);
|
| | | // 设置日历中月份的最大天数
|
| | | cal.set(Calendar.DAY_OF_MONTH, lastDay);
|
| | | // 格式化日期
|
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
| | | return sdf.format(cal.getTime());
|
| | | }
|
| | |
|
| | | public static void main(String[] args) throws ParseException {
|
| | | System.out.println(getMonthSpace("2012-02", "2012-02")); |
| | | }
|
| | | System.out.println(getMonthSpace("2012-02", "2012-02"));
|
| | | }
|
| | |
|
| | | } |