java工具类(四)之实现日期任意跳转

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;
}
上一篇:$.each遍历JSON字符串和 Uncaught TypeError: Cannot use 'in' operator to search for '156错误


下一篇:李洪强iOS开发Swift篇---11_变量&常量&元组