| | |
| | | } |
| | | |
| | | return 0; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取当前月的开始时间 |
| | | * @param time 时间 |
| | | * @return 返回时间 格式yyyy-MM-dd 00:00:00 |
| | | */ |
| | | public static Date getMonthStart(Date time) { |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(time); |
| | | calendar.set(Calendar.DAY_OF_MONTH, 1); |
| | | calendar.set(Calendar.HOUR_OF_DAY, 0); |
| | | calendar.set(Calendar.MINUTE, 0); |
| | | calendar.set(Calendar.SECOND, 0); |
| | | return calendar.getTime(); |
| | | } |
| | | |
| | | /** |
| | |
| | | calendar.set(Calendar.SECOND, 0); |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA); |
| | | return sdf.format(calendar.getTime()); |
| | | } |
| | | |
| | | /** |
| | | * 获取当前月的结束时间 |
| | | * |
| | | * @param date 0拿取当月,正代表后,负代表前,值为几个(月) |
| | | * @return 返回时间 格式yyyy-MM-dd 23:59:59 |
| | | */ |
| | | public static Date getMonthEnd(Date date) { |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(date); |
| | | calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); |
| | | calendar.set(Calendar.HOUR_OF_DAY, 23); |
| | | calendar.set(Calendar.MINUTE, 59); |
| | | calendar.set(Calendar.SECOND, 59); |
| | | return calendar.getTime(); |
| | | } |
| | | |
| | | /** |
| | |
| | | return sdf.format(calendar.getTime()); |
| | | } |
| | | |
| | | /**获取月份的天数 |
| | | * @param date 时间 |
| | | * @return 月份的天数 |
| | | */ |
| | | public static int getMonthDays(Date date) { |
| | | Calendar cal = Calendar.getInstance(); |
| | | cal.setTime(date); |
| | | cal.set(Calendar.DATE, 1); |
| | | cal.roll(Calendar.DATE, -1); |
| | | return cal.getActualMaximum(Calendar.DATE); |
| | | } |
| | | /**获取月份的天数 |
| | | * @param year 年份 |
| | | * @param month 月份 |
| | | * @return 月份的天数 |
| | | */ |
| | | public static int getMonthDays(int year, int month) { |
| | | Calendar cal = Calendar.getInstance(); |
| | | cal.set(Calendar.YEAR, year); |
| | | cal.set(Calendar.MONTH, (month - 1)); |
| | | cal.set(Calendar.DATE, 1); |
| | | cal.roll(Calendar.DATE, -1); |
| | | return cal.getActualMaximum(Calendar.DATE); |
| | | } |
| | | |
| | | /**获取月份的天数 |
| | | * @param yearMonth 年月 |
| | | * @param format 时间格式 |
| | | * @return |
| | | */ |
| | | public static int getMonthDays(String yearMonth,String format) { |
| | | SimpleDateFormat sdf = new SimpleDateFormat(format); |
| | | Calendar calendar = Calendar.getInstance(); |
| | | try { |
| | | calendar.setTime(sdf.parse(yearMonth)); |
| | | } catch (ParseException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | calendar.getActualMaximum(Calendar.DAY_OF_MONTH); |
| | | return calendar.getActualMaximum(Calendar.DAY_OF_MONTH); |
| | | } |
| | | } |