如何对date对象加一天呢?
下文笔者讲述对date对象加一天操作的方法分享,如下所示
实现思路: 1.将Date对象转换为Calendar对象 2.使用calendar.add()方法即可实现对日期加指定天数的操作例
public void test() { // 小时时间段 String start = "08:30"; String end = "00:00"; // 时间格式 SimpleDateFormat sdfOne = new SimpleDateFormat("yyyy-MM-dd HH:mm"); SimpleDateFormat sdfTwo = new SimpleDateFormat("yyyy-MM-dd "); Date date = new Date(); // 当前时间格式 String currentTimeFormat = sdfOne.format(date); // 时间段范围格式转换 String startTimeFormat = sdfTwo.format(date) + start; String endTimeFormat = sdfTwo.format(date) + end; // 解析成日期格式 Date currentDate = null; Date startDate = null; Date endDate = null; try { currentDate = sdfOne.parse(currentTimeFormat); startDate = sdfOne.parse(startTimeFormat); endDate = sdfOne.parse(endTimeFormat); if (end != null && end.startsWith("00")) { // 时间段结束以00 开头,日期时间加一天 Calendar calendar = Calendar.getInstance(); calendar.setTime(endDate); // 日期加一天 calendar.add(Calendar.DATE, 1); endDate = calendar.getTime(); } } catch (ParseException e) { e.printStackTrace(); } System.out.println(currentDate); System.out.println(startDate); System.out.println(endDate); }
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。