java 时间的前面 几天的时间

 

import java.text.SimpleDateFormat;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;



public class DateUtils {


//	public static void main(String[] args) {
//		getTodayTime() ;
//	}
	
	public static void main(String[] args) {
		getTodayTime() ;
		getBeforeDaysTime(1) ;
		getBeforeDaysTime(2) ;
		getBeforeDaysTime(3) ;
		getBeforeDaysTime(4) ;
		getBeforeDaysTime(5) ;
		getBeforeDaysTime(6) ;
		getBeforeDaysTime(7) ;
		getBeforeDaysTime(8) ;
		getBeforeDaysTime(9) ;
		getBeforeDaysTime(10) ;
		getBeforeDaysTime(11) ;
		getBeforeDaysTime(12) ;
		getBeforeDaysTime(13) ;
		getBeforeDaysTime(14) ;
		getBeforeDaysTime(15) ;
		
	}
	
	//---------------------------------以上是产生一个随机数的,写错了
	//-----下面是日期的代码
	/**
	 * 获取当前的日期
	 */
	public static String getTodayTime() {
		String strTodayTime = "" ; //19880214这样形式
		Date now = new Date() ;
		SimpleDateFormat f= new SimpleDateFormat("yyyy-MM-dd");
	
		strTodayTime = f.format(now) ;  
		System.out.println(strTodayTime);
		return  strTodayTime ;

	}
	
	/**
	 * 得到当前日期的前的第beforData天的日期
	 * @param beforData
	 * @return
	 */
	public static String  getBeforeDaysTime(int beforData) {
		
		GregorianCalendar calendar = new GregorianCalendar();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - beforData);
		Date date = calendar.getTime();
		System.out.println(df.format(date));
		return df.format(date);
	}
	
}
2012-06-10
2012-06-09
2012-06-08
2012-06-07
2012-06-06
2012-06-05
2012-06-04
2012-06-03
2012-06-02
2012-06-01
2012-05-31
2012-05-30
2012-05-29
2012-05-28
2012-05-27
2012-05-26
 
                                                                                                                                                                          

                            

你可能感兴趣的:(java)