Java实现日期任意跳转
项目开发过程中,需要进行订单提醒日期的设置,主要包括设置每月指定的日期或者指定的天数,代码如下:
public static String DateOperation(String type, Date myDate, int num) { String date = null; SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd"); try { Calendar c = Calendar.getInstance(); c.setTime(myDate); if (type.equals("0")){ c.add(Calendar.MONTH, 1); myDate = c.getTime(); date = formatter.format(myDate); String dt = date.substring(date.length()-2,date.length()); System.out.println("截取后的日期:[" + dt + "]"); int days = num - Integer.parseInt(dt); System.out.println("相差天数:" + days); c.add(Calendar.DATE, days); myDate = c.getTime(); date = formatter.format(myDate); }else if (type.equals("1")){ c.add(Calendar.DATE, num); myDate = c.getTime(); date = formatter.format(myDate); } System.out.println("订单下次提醒日期:[" + date + "]"); } catch (Exception e1) { e1.printStackTrace(); } return date; }