WEB开发网
开发学院WEB开发Jsp 如何利用java得到当前的时间和前一天的时间 阅读

如何利用java得到当前的时间和前一天的时间

 2009-11-04 20:57:11 来源:WEB开发网   
核心提示:1.得到当前的时间view plaincopy to clipboardPRint?Calendar cal = Calendar.getInstance();String time=formatter.format(cal.getTime()); String strEnd=time.substring(0, 16)
1.得到当前的时间

view plaincopy to clipboardPRint?
Calendar  cal  =  Calendar.getInstance();  
String time=formatter.format(cal.getTime());  
String strEnd=time.substring(0, 16);// 2007-10-30 09:30 
Calendar  cal  =  Calendar.getInstance();
String time=formatter.format(cal.getTime());
String strEnd=time.substring(0, 16);// 2007-10-30 09:30

2.得到前一天的时间

view plaincopy to clipboardprint?
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 
 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.得到前几天的时间

view plaincopy to clipboardprint?
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 
 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.得到当前时间的前几年的时间

view plaincopy to clipboardprint?
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 
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

补充:我要用的是前一天零点的时间,所以这样写:

view plaincopy to clipboardprint?
Calendar c = Calendar.getInstance();  
c.add(Calendar.DAY_OF_MONTH, -1);  
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd 00:00:00");   
String theDayBeforeToday = formatter.format(c.getTime()); 

Tags:如何 利用 java

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接