1 public Timestamp strToDate(int type){ 2 Timestamp time = null; 3 DateFormat format2= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 4 Date date = null; 5 Calendar cale = Calendar.getInstance(); 6 cale.add(Calendar.DATE, type); 7 String tarday = new SimpleDateFormat("yyyy-MM-dd").format(cale.getTime())+" 00:00:00"; 8 9 // String转Date 10 11 try { 12 date = format2.parse(tarday); 13 System.out.println(date.getTime()); 14 time = new Timestamp(date.getTime()); 15 System.out.println(time.toString()); 16 } catch (ParseException e) { 17 e.printStackTrace(); 18 } 19 20 return time; 21 }
type 为-1 表示获取昨天的日期,-2为前天的日期,以此类推。0 为今天的日期。