import java.util.ArrayList;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.SimpleTimeZone;
import java.util.TimeZone;
class Main {
public static void main(String[] args) {
System.out.println("东二区:" + getFormatedDateString(2));
long timeMillis = new Date().getTime();
System.out.println("东八区:" + timeMillis);
System.out.println(new Date(timeMillis));
long timeMillis2= timeMillis+32400000;
System.out.println(timeMillis2);
TimeZone zt = TimeZone.getTimeZone("GMT+2:00");
System.out.println(TimeUtil.isSameDayOfMillis(timeMillis,timeMillis2,zt));
long t = System.currentTimeMillis();
System.out.println("long = " + t);
SimpleDateFormat sdf_default = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf_default.format(t));
SimpleDateFormat sdf_8 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf_8.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
System.out.println("GMT+8:00 = " + sdf_8.format(t));
SimpleDateFormat sdf_2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf_2.setTimeZone(TimeZone.getTimeZone("GMT+2:00"));
System.out.println("GMT+2:00 = " + sdf_2.format(t));
SimpleDateFormat sdf_la = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf_la.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
System.out.println("America/Los_Angeles = " + sdf_la.format(t));
}
public static String getUTCWAST2(){
return getFormatedDateString(2);
}
public static Date getUTCWAST2Date() {
String utc2Date = getUTCWAST2();
Date date = null;
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
date= sdf.parse(utc2Date);
} catch (Exception e) {
}
return date;
}
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());
}
}
class TimeUtil {
public static final int SECONDS_IN_DAY = 60 * 60 * 24;
public static final long MILLIS_IN_DAY = 1000L * SECONDS_IN_DAY;
public static boolean isSameDayOfMillis(final long ms1, final long ms2,TimeZone tz) {
final long interval = ms1 - ms2;
return interval < MILLIS_IN_DAY
&& interval > -1L * MILLIS_IN_DAY
&& toDay(ms1,tz) == toDay(ms2,tz);
}
private static long toDay(long millis,TimeZone tz) {
return (millis + tz.getOffset(millis)) / MILLIS_IN_DAY;
}
}