/** * 获取某年第一天日期 * @param year 年份 * @return Date */ public static Date getYearFirst(String year){ int thisYear =Integer.parseInt(year); Calendar calendar = Calendar.getInstance(); calendar.clear(); calendar.set(Calendar.YEAR, thisYear); Date currYearFirst = calendar.getTime(); return currYearFirst; } /** * 获取某年最后一天日期 * @param year 年份 * @return Date */ public static Date getYearLast(String year){ int thisYear =Integer.parseInt(year); Calendar calendar = Calendar.getInstance(); calendar.clear(); calendar.set(Calendar.YEAR, thisYear); calendar.roll(Calendar.DAY_OF_YEAR, -1); Date currYearLast = calendar.getTime(); return currYearLast; } /** * 获取某年月的第一天 2019-12 * @param yearAndMonth * @return */ public static Date getFisrtDayOfMonth(String yearAndMonth) { String[] yearAndMonths =yearAndMonth.split("-"); int thisYear = Integer.parseInt(yearAndMonths[0]); int thisMonth =Integer.parseInt(yearAndMonths[1]); Calendar cal = Calendar.getInstance(); //设置年份 cal.set(Calendar.YEAR, thisYear); //设置月份 cal.set(Calendar.MONTH, thisMonth - 1); //获取某月最小天数 int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH); //设置日历中月份的最小天数 cal.set(Calendar.DAY_OF_MONTH, firstDay); Date currYearMonthFirst = cal.getTime(); return currYearMonthFirst; } /** *获取某年月的最后一天 2019-12 * @param yearAndMonth * @return */ public static Date getLastDayOfMonth(String yearAndMonth) { String[] yearAndMonths =yearAndMonth.split("-"); int thisYear = Integer.parseInt(yearAndMonths[0]); int thisMonth =Integer.parseInt(yearAndMonths[1]); Calendar cal = Calendar.getInstance(); //设置年份 cal.set(Calendar.YEAR,thisYear); //设置月份 cal.set(Calendar.MONTH, thisMonth-1); //获取某月最大天数 int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH); //设置日历中月份的最大天数 cal.set(Calendar.DAY_OF_MONTH, lastDay); Date currYearMonthLast= cal.getTime(); return currYearMonthLast; } /** * 时间戳转日期 * @param timestamp * @param type * @return */ public String getStringTime(String timestamp,String type) { Date date = new Date(Long.valueOf(timestamp)*1000L); String str =""; if ("month".equals(type)){ str = new SimpleDateFormat("yyyy-MM").format(date.getTime()); // 获取只有年月的时间 }else if ("day".equals(type)){ str = new SimpleDateFormat("yyyy-MM-dd").format(date.getTime()); // 获取只有年月日的时间 } return str; }
日期Utils
最新推荐文章于 2023-12-13 10:10:36 发布