java 获取特定天数的时间戳

时间:2023-03-08 20:21:31
java 获取特定天数的时间戳
     public Timestamp strToDate(int type){
Timestamp time = null;
DateFormat format2= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = null;
Calendar cale = Calendar.getInstance();
cale.add(Calendar.DATE, type);
String tarday = new SimpleDateFormat("yyyy-MM-dd").format(cale.getTime())+" 00:00:00"; // String转Date try {
date = format2.parse(tarday);
System.out.println(date.getTime());
time = new Timestamp(date.getTime());
System.out.println(time.toString());
} catch (ParseException e) {
e.printStackTrace();
} return time;
}

type 为-1 表示获取昨天的日期,-2为前天的日期,以此类推。0 为今天的日期。