import java.util.ArrayList;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.SimpleTimeZone;
import java.util.TimeZone;
public class TimeZoneUtil {
public static ArrayList<String> getDaysBetwwen(int days){
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);
} 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);
} catch (Exception e) {
}
c.setTime(date);
c.add(Calendar.DAY_OF_MONTH, -days);
return c.getTime();
}
public static String getBeijingTime(){
return getFormatedDateString(8);
}
public static String getUTCWAST2(){
return getFormatedDateString(2);
}
public static String getUTC0(){
return getFormatedDateString(0);
}
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);
}catch(Exception e){
}
}
}
}