This article has shared the Java cutting date and time period code for your reference. The specific content is as follows
/** * @author dy * @since 2016-09-18 & JDK 1.8.0_91 */public class DateCalculate { static Logger logger = LoggerFactory.getLogger(DateCalculate.class); /** * Cutting time period* * @param dateType Transaction type M/D/H/N --> Monthly/day/hour/min* @param start yyyy-MM-dd HH:mm:ss * @param end yyyy-MM-dd HH:mm:ss * @return */ public static List<String> cutDate(String dateType, String start, String end) { try { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date dBegin = sdf.parse(start); Date dEnd = sdf.parse(end); return findDates(dateType, dBegin, dEnd); } catch (Exception e) { logger.error(e.getMessage(), e); } return null; } public static List<String> findDates(String dateType, Date dBegin, Date dEnd) throws Exception { List<String> listDate = new ArrayList<>(); Calendar calBegin = Calendar.getInstance(); calBegin.setTime(dBegin); Calendar calEnd = Calendar.getInstance(); calEnd.setTime(dEnd); while (calEnd.after(calBegin)) { switch (dateType) { case "M": calBegin.add(Calendar.MONTH, 1); break; case "D": calBegin.add(Calendar.DAY_OF_YEAR, 1);break; case "H": calBegin.add(Calendar.HOUR, 1);break; case "N": calBegin.add(Calendar.SECOND, 1);break; } if (calEnd.after(calBegin)) listDate.add(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calBegin.getTime())); else listDate.add(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calEnd.getTime())); } return listDate; } public static void main(String[] args) { String start = "2016-02-01 00:00:00"; String end = "2016-03-02 00:00:00"; List<String> list = cutDate("D", start, end); for (String str :list){ System.out.println(str); } }}The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.