@Slf4j public class DateTimeUtil { public static String DEFAULT_FORMAT = "yyyy-MM-dd"; public static void main(String[] args) { // 测试 int lastYear = getLastYear(); getYearFirst(lastYear); } public static Integer getLastYear() { SimpleDateFormat formats = new SimpleDateFormat("yyyy"); Calendar c = Calendar.getInstance(); c.add(Calendar.YEAR,-1); Date date = c.getTime(); // Date类型转String类型 String format = formats.format(date); // String类型转int类型 int parseInt = Integer.parseInt(format); log.info("去年为{}年",parseInt); return parseInt; } public static String formatDate(Date date){ SimpleDateFormat f = new SimpleDateFormat(DEFAULT_FORMAT); return f.format(date); } public static Date getCurrYearFirst(){ Calendar currCal=Calendar.getInstance(); int currentYear = currCal.get(Calendar.YEAR); return getYearFirst(currentYear); } public static Date getCurrYearLast(){ Calendar currCal=Calendar.getInstance(); int currentYear = currCal.get(Calendar.YEAR); return getYearLast(currentYear); } public static Date getYearFirst(int year){ Calendar calendar = Calendar.getInstance(); calendar.clear(); calendar.set(Calendar.YEAR, year); Date currYearFirst = calendar.getTime(); String nowTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(currYearFirst); log.info("去年第一天为{}",nowTime); return currYearFirst; } public static Date getYearLast(int year){ Calendar calendar = Calendar.getInstance(); calendar.clear(); calendar.set(Calendar.YEAR, year); calendar.roll(Calendar.DAY_OF_YEAR, -1); return calendar.getTime(); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)