如何取的指定年月日时分秒后的时间

 因为需要计算当前日期之后日期的一天0点,到一年之后的今天这个时间点。

所以用到了下面的方式。主要用到的是Calendar的add()方法,可以增减年月日,甚至时分秒。所以把代码贴出来

public class DateUtils {
    public static Map<String, Date> getBeginWhithEndDay(Date date){
        //如果小于, 则将起保日期向后退1天到0点 同时过保日期也要变
        SimpleDateFormat startDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = startDateFormat.format(date);

        Date parseDate = null;
        try {
            parseDate = startDateFormat.parse(dateString);
        } catch (ParseException ex) {
            throw new RuntimeException(ex);
        }
        Calendar c = Calendar.getInstance();
        c.setTime(parseDate);
        c.add(Calendar.DAY_OF_MONTH, 1);
        Date startDate = c.getTime();

        Calendar calendar = new GregorianCalendar();
        calendar.setTime(startDate);
        calendar.add(Calendar.YEAR, 1);// 把日期往后增加一年.整数往后推,负数往前移动
        Date time = calendar.getTime();

        calendar = new GregorianCalendar();
        calendar.setTime(time);
        calendar.add(Calendar.SECOND, -1);// 把日期往前减一秒
        Date endDate = calendar.getTime();//取得一年后的今天23:59:59

        Map<String, Date> map = new HashMap<>();
        map.put("startDate", startDate);
        map.put("endDate", endDate);
        return map;
    }
}
发布了62 篇原创文章 · 获赞 40 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/qq_31840023/article/details/104617392
今日推荐