java 日期的加减
- 转载:http://blog.csdn.net/damaolly/article/details/41448493
- package util;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- /***
- * 日期工具类
- *
- * @author damao
- *
- */
- public class DateAndTimeUtil {
- /***
- * 日期月份减一个月
- *
- * @param datetime
- * 日期(2014-11)
- * @return 2014-10
- */
- public static String dateFormat(String datetime) {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
- Date date = null;
- try {
- date = sdf.parse(datetime);
- } catch (ParseException e) {
- e.printStackTrace();
- }
- Calendar cl = Calendar.getInstance();
- cl.setTime(date);
- cl.add(Calendar.MONTH, -1);
- date = cl.getTime();
- return sdf.format(date);
- }
- public static String dateFormat(Date date) {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
- return sdf.format(date);
- }
- /****
- * 传入具体日期 ,返回具体日期减一个月。
- *
- * @param date
- * 日期(2014-04-20)
- * @return 2014-03-20
- * @throws ParseException
- */
- public static String subMonth(String date) throws ParseException {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- Date dt = sdf.parse(date);
- Calendar rightNow = Calendar.getInstance();
- rightNow.setTime(dt);
- rightNow.add(Calendar.MONTH, -1);
- Date dt1 = rightNow.getTime();
- String reStr = sdf.format(dt1);
- return reStr;
- }
- /****
- * 获取月末最后一天
- *
- * @param sDate
- * 2014-11-24
- * @return 30
- */
- private static String getMonthMaxDay(String sDate) {
- SimpleDateFormat sdf_full = new SimpleDateFormat("yyyy-MM-dd");
- Calendar cal = Calendar.getInstance();
- Date date = null;
- try {
- date = sdf_full.parse(sDate + "-01");
- } catch (ParseException e) {
- e.printStackTrace();
- }
- cal.setTime(date);
- int last = cal.getActualMaximum(Calendar.DATE);
- return String.valueOf(last);
- }
- // 判断是否是月末
- public static boolean isMonthEnd(Date date) {
- Calendar cal = Calendar.getInstance();
- cal.setTime(date);
- if (cal.get(Calendar.DATE) == cal
- .getActualMaximum(Calendar.DAY_OF_MONTH))
- return true;
- else
- return false;
- }
- /***
- * 日期减一天、加一天
- *
- * @param option
- * 传入类型 pro:日期减一天,next:日期加一天
- * @param _date
- * 2014-11-24
- * @return 减一天:2014-11-23或(加一天:2014-11-25)
- */
- public static String checkOption(String option, String _date) {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- Calendar cl = Calendar.getInstance();
- Date date = null;
- try {
- date = (Date) sdf.parse(_date);
- } catch (ParseException e) {
- e.printStackTrace();
- }
- cl.setTime(date);
- if ("pre".equals(option)) {
- // 时间减一天
- cl.add(Calendar.DAY_OF_MONTH, -1);
- } else if ("next".equals(option)) {
- // 时间加一天
- cl.add(Calendar.DAY_OF_YEAR, 1);
- } else {
- // do nothing
- }
- date = cl.getTime();
- return sdf.format(date);
- }
- /***
- * 判断日期是否为当前月, 是当前月返回当月最小日期和当月目前最大日期以及传入日期上月的最大日和最小日
- * 不是当前月返回传入月份的最大日和最小日以及传入日期上月的最大日和最小日
- *
- * @param date
- * 日期 例如:2014-11
- * @return String[] 开始日期,结束日期,上月开始日期,上月结束日期
- * @throws ParseException
- */
- public static String[] getNow_Pre_Date(String date) throws ParseException {
- String[] str_date = new String[4];
- Date now = new Date();
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
- SimpleDateFormat sdf_full = new SimpleDateFormat("yyyy-MM-dd");
- String stMonth = sdf.format(now);
- String stdate = "";// 开始日期
- String endate = "";// 结束日期
- String preDate_start = "";// 上月开始日期
- String preDate_end = "";// 上月结束日期
- // 当前月
- if (date.equals(stMonth)) {
- stdate = stMonth + "-01"; // 2014-11-01
- endate = sdf_full.format(now);// 2014-11-24
- preDate_start = subMonth(stdate);// 2014-10-01
- preDate_end = subMonth(endate);// 2014-10-24
- } else {
- // 非当前月
- String monthMaxDay = getMonthMaxDay(date);
- stdate = date + "-01";// 2014-10-01
- endate = date + "-" + monthMaxDay;// 2014-10-31
- preDate_start = subMonth(stdate);// 2014-09-01
- preDate_end = subMonth(endate);// 2014-09-30
- }
- str_date[0] = stdate;
- str_date[1] = endate;
- str_date[2] = preDate_start;
- str_date[3] = preDate_end;
- return str_date;
- }
- public static void main(String[] args) throws ParseException {
- /*
- * String a =DateAndTimeUtil.dateFormat(new Date());
- * System.out.println(a); String b =
- * DateAndTimeUtil.subMonth("2014-03-31"); System.out.println(b);
- * SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); Date
- * dt=sdf.parse("2014-03-31");
- * System.out.println(DateAndTimeUtil.isMonthEnd(dt));
- */
- String str = null;
- // str = DateAndTimeUtil.checkOption("next", "2014-11-30");
- // str = getMonthMaxDay("2014-11-24");
- // str = dateFormat("2014-11");
- str = getNow_Pre_Date("2014-10")[0];
- System.out.println(str);
- }
- }
<li class="alt"><span><span class="keyword">package</span><span> util; </span></span></li><li class=""><span> </span></li><li class="alt"><span><span class="keyword">import</span><span> java.text.ParseException; </span></span></li><li class=""><span><span class="keyword">import</span><span> java.text.SimpleDateFormat; </span></span></li><li class="alt"><span><span class="keyword">import</span><span> java.util.Calendar; </span></span></li><li class=""><span><span class="keyword">import</span><span> java.util.Date; </span></span></li><li class="alt"><span> </span></li><li class=""><span><span class="comment">/***</span> </span></li><li class="alt"><span><span class="comment"> * 日期工具类</span> </span></li><li class=""><span><span class="comment"> * </span> </span></li><li class="alt"><span><span class="comment"> * @author damao</span> </span></li><li class=""><span><span class="comment"> *</span> </span></li><li class="alt"><span><span class="comment"> */</span><span> </span></span></li><li class=""><span><span class="keyword">public</span><span> </span><span class="keyword">class</span><span> DateAndTimeUtil { </span></span></li><li class="alt"><span> <span class="comment">/***</span> </span></li><li class=""><span><span class="comment"> * 日期月份减一个月</span> </span></li><li class="alt"><span><span class="comment"> * </span> </span></li><li class=""><span><span class="comment"> * @param datetime</span> </span></li><li class="alt"><span><span class="comment"> * 日期(2014-11)</span> </span></li><li class=""><span><span class="comment"> * @return 2014-10</span> </span></li><li class="alt"><span><span class="comment"> */</span><span> </span></span></li><li class=""><span> <span class="keyword">public</span><span> </span><span class="keyword">static</span><span> String dateFormat(String datetime) { </span></span></li><li class="alt"><span> SimpleDateFormat sdf = <span class="keyword">new</span><span> SimpleDateFormat(</span><span class="string">"yyyy-MM"</span><span>); </span></span></li><li class=""><span> Date date = <span class="keyword">null</span><span>; </span></span></li><li class="alt"><span> <span class="keyword">try</span><span> { </span></span></li><li class=""><span> date = sdf.parse(datetime); </span></li><li class="alt"><span> } <span class="keyword">catch</span><span> (ParseException e) { </span></span></li><li class=""><span> e.printStackTrace(); </span></li><li class="alt"><span> } </span></li><li class=""><span> Calendar cl = Calendar.getInstance(); </span></li><li class="alt"><span> cl.setTime(date); </span></li><li class=""><span> cl.add(Calendar.MONTH, -<span class="number">1</span><span>); </span></span></li><li class="alt"><span> date = cl.getTime(); </span></li><li class=""><span> <span class="keyword">return</span><span> sdf.format(date); </span></span></li><li class="alt"><span> } </span></li><li class=""><span> </span></li><li class="alt"><span> <span class="keyword">public</span><span> </span><span class="keyword">static</span><span> String dateFormat(Date date) { </span></span></li><li class=""><span> SimpleDateFormat sdf = <span class="keyword">new</span><span> SimpleDateFormat(</span><span class="string">"yyyy-MM"</span><span>); </span></span></li><li class="alt"><span> <span class="keyword">return</span><span> sdf.format(date); </span></span></li><li class=""><span> } </span></li><li class="alt"><span> </span></li><li class=""><span> <span class="comment">/****</span> </span></li><li class="alt"><span><span class="comment"> * 传入具体日期 ,返回具体日期减一个月。</span> </span></li><li class=""><span><span class="comment"> * </span> </span></li><li class="alt"><span><span class="comment"> * @param date</span> </span></li><li class=""><span><span class="comment"> * 日期(2014-04-20)</span> </span></li><li class="alt"><span><span class="comment"> * @return 2014-03-20</span> </span></li><li class=""><span><span class="comment"> * @throws ParseException</span> </span></li><li class="alt"><span><span class="comment"> */</span><span> </span></span></li><li class=""><span> <span class="keyword">public</span><span> </span><span class="keyword">static</span><span> String subMonth(String date) </span><span class="keyword">throws</span><span> ParseException { </span></span></li><li class="alt"><span> SimpleDateFormat sdf = <span class="keyword">new</span><span> SimpleDateFormat(</span><span class="string">"yyyy-MM-dd"</span><span>); </span></span></li><li class=""><span> Date dt = sdf.parse(date); </span></li><li class="alt"><span> Calendar rightNow = Calendar.getInstance(); </span></li><li class=""><span> rightNow.setTime(dt); </span></li><li class="alt"><span> </span></li><li class=""><span> rightNow.add(Calendar.MONTH, -<span class="number">1</span><span>); </span></span></li><li class="alt"><span> Date dt1 = rightNow.getTime(); </span></li><li class=""><span> String reStr = sdf.format(dt1); </span></li><li class="alt"><span> </span></li><li class=""><span> <span class="keyword">return</span><span> reStr; </span></span></li><li class="alt"><span> } </span></li><li class=""><span> </span></li><li class="alt"><span> <span class="comment">/****</span> </span></li><li class=""><span><span class="comment"> * 获取月末最后一天</span> </span></li><li class="alt"><span><span class="comment"> * </span> </span></li><li class=""><span><span class="comment"> * @param sDate</span> </span></li><li class="alt"><span><span class="comment"> * 2014-11-24</span> </span></li><li class=""><span><span class="comment"> * @return 30</span> </span></li><li class="alt"><span><span class="comment"> */</span><span> </span></span></li><li class=""><span> <span class="keyword">private</span><span> </span><span class="keyword">static</span><span> String getMonthMaxDay(String sDate) { </span></span></li><li class="alt"><span> SimpleDateFormat sdf_full = <span class="keyword">new</span><span> SimpleDateFormat(</span><span class="string">"yyyy-MM-dd"</span><span>); </span></span></li><li class=""><span> Calendar cal = Calendar.getInstance(); </span></li><li class="alt"><span> Date date = <span class="keyword">null</span><span>; </span></span></li><li class=""><span> <span class="keyword">try</span><span> { </span></span></li><li class="alt"><span> date = sdf_full.parse(sDate + <span class="string">"-01"</span><span>); </span></span></li><li class=""><span> } <span class="keyword">catch</span><span> (ParseException e) { </span></span></li><li class="alt"><span> e.printStackTrace(); </span></li><li class=""><span> } </span></li><li class="alt"><span> cal.setTime(date); </span></li><li class=""><span> <span class="keyword">int</span><span> last = cal.getActualMaximum(Calendar.DATE); </span></span></li><li class="alt"><span> <span class="keyword">return</span><span> String.valueOf(last); </span></span></li><li class=""><span> } </span></li><li class="alt"><span> </span></li><li class=""><span> <span class="comment">// 判断是否是月末</span><span> </span></span></li><li class="alt"><span> <span class="keyword">public</span><span> </span><span class="keyword">static</span><span> </span><span class="keyword">boolean</span><span> isMonthEnd(Date date) { </span></span></li><li class=""><span> Calendar cal = Calendar.getInstance(); </span></li><li class="alt"><span> cal.setTime(date); </span></li><li class=""><span> <span class="keyword">if</span><span> (cal.get(Calendar.DATE) == cal </span></span></li><li class="alt"><span> .getActualMaximum(Calendar.DAY_OF_MONTH)) </span></li><li class=""><span> <span class="keyword">return</span><span> </span><span class="keyword">true</span><span>; </span></span></li><li class="alt"><span> <span class="keyword">else</span><span> </span></span></li><li class=""><span> <span class="keyword">return</span><span> </span><span class="keyword">false</span><span>; </span></span></li><li class="alt"><span> } </span></li><li class=""><span> </span></li><li class="alt"><span> <span class="comment">/***</span> </span></li><li class=""><span><span class="comment"> * 日期减一天、加一天</span> </span></li><li class="alt"><span><span class="comment"> * </span> </span></li><li class=""><span><span class="comment"> * @param option</span> </span></li><li class="alt"><span><span class="comment"> * 传入类型 pro:日期减一天,next:日期加一天</span> </span></li><li class=""><span><span class="comment"> * @param _date</span> </span></li><li class="alt"><span><span class="comment"> * 2014-11-24</span> </span></li><li class=""><span><span class="comment"> * @return 减一天:2014-11-23或(加一天:2014-11-25)</span> </span></li><li class="alt"><span><span class="comment"> */</span><span> </span></span></li><li class=""><span> <span class="keyword">public</span><span> </span><span class="keyword">static</span><span> String checkOption(String option, String _date) { </span></span></li><li class="alt"><span> SimpleDateFormat sdf = <span class="keyword">new</span><span> SimpleDateFormat(</span><span class="string">"yyyy-MM-dd"</span><span>); </span></span></li><li class=""><span> Calendar cl = Calendar.getInstance(); </span></li><li class="alt"><span> Date date = <span class="keyword">null</span><span>; </span></span></li><li class=""><span> </span></li><li class="alt"><span> <span class="keyword">try</span><span> { </span></span></li><li class=""><span> date = (Date) sdf.parse(_date); </span></li><li class="alt"><span> } <span class="keyword">catch</span><span> (ParseException e) { </span></span></li><li class=""><span> e.printStackTrace(); </span></li><li class="alt"><span> } </span></li><li class=""><span> cl.setTime(date); </span></li><li class="alt"><span> <span class="keyword">if</span><span> (</span><span class="string">"pre"</span><span>.equals(option)) { </span></span></li><li class=""><span> <span class="comment">// 时间减一天</span><span> </span></span></li><li class="alt"><span> cl.add(Calendar.DAY_OF_MONTH, -<span class="number">1</span><span>); </span></span></li><li class=""><span> </span></li><li class="alt"><span> } <span class="keyword">else</span><span> </span><span class="keyword">if</span><span> (</span><span class="string">"next"</span><span>.equals(option)) { </span></span></li><li class=""><span> <span class="comment">// 时间加一天</span><span> </span></span></li><li class="alt"><span> cl.add(Calendar.DAY_OF_YEAR, <span class="number">1</span><span>); </span></span></li><li class=""><span> } <span class="keyword">else</span><span> { </span></span></li><li class="alt"><span> <span class="comment">// do nothing</span><span> </span></span></li><li class=""><span> } </span></li><li class="alt"><span> date = cl.getTime(); </span></li><li class=""><span> <span class="keyword">return</span><span> sdf.format(date); </span></span></li><li class="alt"><span> } </span></li><li class=""><span> </span></li><li class="alt"><span> <span class="comment">/***</span> </span></li><li class=""><span><span class="comment"> * 判断日期是否为当前月, 是当前月返回当月最小日期和当月目前最大日期以及传入日期上月的最大日和最小日</span> </span></li><li class="alt"><span><span class="comment"> * 不是当前月返回传入月份的最大日和最小日以及传入日期上月的最大日和最小日</span> </span></li><li class=""><span><span class="comment"> * </span> </span></li><li class="alt"><span><span class="comment"> * @param date</span> </span></li><li class=""><span><span class="comment"> * 日期 例如:2014-11</span> </span></li><li class="alt"><span><span class="comment"> * @return String[] 开始日期,结束日期,上月开始日期,上月结束日期</span> </span></li><li class=""><span><span class="comment"> * @throws ParseException</span> </span></li><li class="alt"><span><span class="comment"> */</span><span> </span></span></li><li class=""><span> <span class="keyword">public</span><span> </span><span class="keyword">static</span><span> String[] getNow_Pre_Date(String date) </span><span class="keyword">throws</span><span> ParseException { </span></span></li><li class="alt"><span> </span></li><li class=""><span> String[] str_date = <span class="keyword">new</span><span> String[</span><span class="number">4</span><span>]; </span></span></li><li class="alt"><span> Date now = <span class="keyword">new</span><span> Date(); </span></span></li><li class=""><span> SimpleDateFormat sdf = <span class="keyword">new</span><span> SimpleDateFormat(</span><span class="string">"yyyy-MM"</span><span>); </span></span></li><li class="alt"><span> SimpleDateFormat sdf_full = <span class="keyword">new</span><span> SimpleDateFormat(</span><span class="string">"yyyy-MM-dd"</span><span>); </span></span></li><li class=""><span> String stMonth = sdf.format(now); </span></li><li class="alt"><span> String stdate = <span class="string">""</span><span>;</span><span class="comment">// 开始日期</span><span> </span></span></li><li class=""><span> String endate = <span class="string">""</span><span>;</span><span class="comment">// 结束日期</span><span> </span></span></li><li class="alt"><span> String preDate_start = <span class="string">""</span><span>;</span><span class="comment">// 上月开始日期</span><span> </span></span></li><li class=""><span> String preDate_end = <span class="string">""</span><span>;</span><span class="comment">// 上月结束日期</span><span> </span></span></li><li class="alt"><span> </span></li><li class=""><span> <span class="comment">// 当前月</span><span> </span></span></li><li class="alt"><span> <span class="keyword">if</span><span> (date.equals(stMonth)) { </span></span></li><li class=""><span> stdate = stMonth + <span class="string">"-01"</span><span>; </span><span class="comment">// 2014-11-01</span><span> </span></span></li><li class="alt"><span> endate = sdf_full.format(now);<span class="comment">// 2014-11-24</span><span> </span></span></li><li class=""><span> preDate_start = subMonth(stdate);<span class="comment">// 2014-10-01</span><span> </span></span></li><li class="alt"><span> preDate_end = subMonth(endate);<span class="comment">// 2014-10-24</span><span> </span></span></li><li class=""><span> } <span class="keyword">else</span><span> { </span></span></li><li class="alt"><span> <span class="comment">// 非当前月</span><span> </span></span></li><li class=""><span> String monthMaxDay = getMonthMaxDay(date); </span></li><li class="alt"><span> stdate = date + <span class="string">"-01"</span><span>;</span><span class="comment">// 2014-10-01</span><span> </span></span></li><li class=""><span> endate = date + <span class="string">"-"</span><span> + monthMaxDay;</span><span class="comment">// 2014-10-31</span><span> </span></span></li><li class="alt"><span> preDate_start = subMonth(stdate);<span class="comment">// 2014-09-01</span><span> </span></span></li><li class=""><span> preDate_end = subMonth(endate);<span class="comment">// 2014-09-30</span><span> </span></span></li><li class="alt"><span> } </span></li><li class=""><span> str_date[<span class="number">0</span><span>] = stdate; </span></span></li><li class="alt"><span> str_date[<span class="number">1</span><span>] = endate; </span></span></li><li class=""><span> str_date[<span class="number">2</span><span>] = preDate_start; </span></span></li><li class="alt"><span> str_date[<span class="number">3</span><span>] = preDate_end; </span></span></li><li class=""><span> </span></li><li class="alt"><span> <span class="keyword">return</span><span> str_date; </span></span></li><li class=""><span> } </span></li><li class="alt"><span> </span></li><li class=""><span> <span class="keyword">public</span><span> </span><span class="keyword">static</span><span> </span><span class="keyword">void</span><span> main(String[] args) </span><span class="keyword">throws</span><span> ParseException { </span></span></li><li class="alt"><span> <span class="comment">/*</span> </span></li><li class=""><span><span class="comment"> * String a =DateAndTimeUtil.dateFormat(new Date());</span> </span></li><li class="alt"><span><span class="comment"> * System.out.println(a); String b =</span> </span></li><li class=""><span><span class="comment"> * DateAndTimeUtil.subMonth("2014-03-31"); System.out.println(b);</span> </span></li><li class="alt"><span><span class="comment"> * SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); Date</span> </span></li><li class=""><span><span class="comment"> * dt=sdf.parse("2014-03-31");</span> </span></li><li class="alt"><span><span class="comment"> * System.out.println(DateAndTimeUtil.isMonthEnd(dt));</span> </span></li><li class=""><span><span class="comment"> */</span><span> </span></span></li><li class="alt"><span> String str = <span class="keyword">null</span><span>; </span></span></li><li class=""><span> <span class="comment">// str = DateAndTimeUtil.checkOption("next", "2014-11-30");</span><span> </span></span></li><li class="alt"><span> <span class="comment">// str = getMonthMaxDay("2014-11-24");</span><span> </span></span></li><li class=""><span> <span class="comment">// str = dateFormat("2014-11");</span><span> </span></span></li><li class="alt"><span> str = getNow_Pre_Date(<span class="string">"2014-10"</span><span>)[</span><span class="number">0</span><span>]; </span></span></li><li class=""><span> System.out.println(str); </span></li><li class="alt"><span> } </span></li><li class=""><span>} </span></li>