Java日期:今天,昨天,某一天,某一天的前一天
当前系统时间SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
昨天
public static void main(String[] args){Calendar calendar = Calendar.getInstance();calendar.add(Calendar.DATE, -1); //得到前一天Date date = calendar.getTime();DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");System.out.println(df.format(date));}
任意一天的前一天
public String method1(String specifiedDay){ Calendar c = Calendar.getInstance(); Date date = null; try{ date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay); }catch (Exception e) { e.printStackTrace(); } c.setTime(date); int day = c.get(Calendar.DATE); c.set(Calendar.DATE, day-1); String dayBefore = new SimpleDateFormat("yyyy-MM-dd).format(c.getTime()); return dayBefore; }