如何利用java得到当前的时间和前一天的时间
1.得到当前的时间
- Calendar cal = Calendar.getInstance();
- String time=formatter.format(cal.getTime());
- String strEnd=time.substring(0, 16);// 2007-10-30 09:30
2.得到前一天的时间
- Calendar c = Calendar.getInstance();
- c.add(Calendar.DAY_OF_MONTH, -1);
- SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- String mDateTime=formatter.format(c.getTime());
- String strStart=mDateTime.substring(0, 16);//2007-10-29 09:30
3.得到前几天的时间
- Calendar c = Calendar.getInstance();
- c.add(Calendar.DAY_OF_MONTH, -5);//得到前5
- SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- String mDateTime=formatter.format(c.getTime());
- String strStart=mDateTime.substring(0, 16);//2007-10-24 09:30
4.得到当前时间的前几年的时间
- Calendar c = Calendar.getInstance();
- c.add(Calendar.YEAR, -5);//得到前5年
- SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- String mDateTime=formatter.format(c.getTime());
- String strStart=mDateTime.substring(0, 16);//2002-10-24 09:30
来自:http://heiwuya.iteye.com/blog/294174