码迷,mamicode.com
首页 > 其他好文 > 详细

获得指定日期上一天和下一天

时间:2017-05-13 09:53:20      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:ext   mat   public   exception   cal   print   trace   sim   16px   

日期的相关操作

一: 最近项目中用到了获取指定日期的上一天和下一天 ,今天有空记录下,以便以后不时之需

二: 获得指定日期下一天

/**
     * 获得指定日期下一天
     * @param specifiedDay
     * @return
     */
    public String getSpecifiedDayAfter(String specifiedDay) {
        Calendar c = Calendar.getInstance();
        Date date = null;
        try {
            date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.setTime(date);
        int day = c.get(Calendar.DATE);
        c.set(Calendar.DATE, day + 1);

        String dayAfter = new SimpleDateFormat("yyyy-MM-dd")
                .format(c.getTime());
        return dayAfter;
    }

三:同上获得指定日期上一天

/**
     * 获得指定日期上一天
     * @param specifiedDay
     * @return
     */
    public String getSpecifiedDayBefore(String specifiedDay) {
        Calendar c = Calendar.getInstance();
        Date date = null;
        try {
            date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.setTime(date);
        int day = c.get(Calendar.DATE);
        c.set(Calendar.DATE, day + 1);

        String dayBefore = new SimpleDateFormat("yyyy-MM-dd")
                .format(c.getTime());
        return dayBefore;
    }

 

获得指定日期上一天和下一天

标签:ext   mat   public   exception   cal   print   trace   sim   16px   

原文地址:http://www.cnblogs.com/tangbang/p/6847962.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!