首页 > 代码库 > 字符串和转为Data类型前后几天
字符串和转为Data类型前后几天
以防忘记:SimpleDateFormat 可以设置字符串的格式
package com.apploft.util.lang;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class test1 {
public static void main(String ager[]) {
/*时间类型格式*/
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
String dateString = "20170301";
try {
Date date = df.parse(dateString);
System.out.println(df.format(date));
Date date2=getDate_NumDay(date,-7);
System.out.println(df.format(date2));
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
/**
* 得到前几天的时间
*/
public static Date getDate_NumDay(Date d, int day) {
Calendar now = Calendar.getInstance();
now.setTime(d);
now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
return now.getTime();
}
}
字符串和转为Data类型前后几天