编辑代码

import java.util.ArrayList;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.SimpleTimeZone;
import java.util.TimeZone;
 
/**
 * 时区工具 
 * @author Administrator
 *
 */
public class TimeZoneUtil {

    //最近几天日期;但不包含今天
    public static ArrayList<String> getDaysBetwwen(int days){ 
        //days=days+1;
        ArrayList<String> dayss = new ArrayList<>();
        Calendar start = Calendar.getInstance();
        start.setTime(getDateAdd(days));
        System.out.println("start date=" + start.getTime());
        Long startTIme = start.getTimeInMillis();
        Calendar end = Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String utc0Date = getUTCWAST2();
        Date date = new Date();
		try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			date= sdf.parse(utc0Date);
            //System.out.println("date = "+date);
		} catch (Exception e) {
		}        
        end.setTime(date);
        System.out.println("end date=" + end.getTime());
        Long endTime = end.getTimeInMillis();
        Long oneDay = 1000 * 60 * 60 * 24l;
        Long time = startTIme;
        while (time < endTime) {
            Date d = new Date(time);
            dayss.add(df.format(d));
            System.out.println("dayDate = " + df.format(d));
            time += oneDay;
        }
        return dayss;
    }

    private static  Date getDateAdd(int days){
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        String utc0Date = getUTCWAST2();
        Date date = new Date();
		try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			date= sdf.parse(utc0Date);
            //System.out.println("date = "+date);
		} catch (Exception e) {
		}        
        c.setTime(date);
        c.add(Calendar.DAY_OF_MONTH, -days);
        //System.out.println(c.getTime());
        return c.getTime();
    }


    /**
     * 取北京时间,格式:yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String getBeijingTime(){
        return getFormatedDateString(8);
    }

    public static String getUTCWAST2(){
        return getFormatedDateString(2);
    }

        public static String getUTC0(){
        return getFormatedDateString(0);
    }
    
    /**
     * 此函数非原创,从网上搜索而来,timeZoneOffset原为int类型,为班加罗尔调整成float类型
     * timeZoneOffset表示时区,如中国一般使用东八区,因此timeZoneOffset就是8
     * @param timeZoneOffset
     * @return
     */
    public static String getFormatedDateString(float timeZoneOffset){
        if (timeZoneOffset > 13 || timeZoneOffset < -12) {
            timeZoneOffset = 0;
        }
        
        int newTime=(int)(timeZoneOffset * 60 * 60 * 1000);
        TimeZone timeZone;
        String[] ids = TimeZone.getAvailableIDs(newTime);
        if (ids.length == 0) {
            timeZone = TimeZone.getDefault();
        } else {
            timeZone = new SimpleTimeZone(newTime, ids[0]);
        }
    
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdf.setTimeZone(timeZone);
        return sdf.format(new Date());
    }
    
    public static void main(String[] args) {
        String utcWast2 = getUTCWAST2() ;
		System.out.println(utcWast2);
        ArrayList<String> list = getDaysBetwwen(7);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        for(String strDate : list){
            try{
               date=sdf.parse(strDate);
               //System.out.println(date);
            }catch(Exception e){

            }
        }
        //System.out.println();
	}
	
}