import java.text.Format;
import java.text.ParseException;
import
java.text.SimpleDateFormat;
import java.util.Date;
import
java.util.Properties;
import java.util.SimpleTimeZone;
import
java.util.TimeZone;
import javax.swing.text.html.HTMLDocument.Iterator;
public class Test2 {
protected static Format format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/**
* timeZoneOffset表示时区,如中国一般使用东八区,因此timeZoneOffset就是8
* @param
timeZoneOffset
* @return
*/
public String
getFormatedDateString(int timeZoneOffset){
if (timeZoneOffset > 13 ||
timeZoneOffset < -12) {
timeZoneOffset = 0;
}
TimeZone timeZone;
String[] ids =
TimeZone.getAvailableIDs(timeZoneOffset * 60 * 60 * 1000);
if
(ids.length == 0) {
// if no ids were returned, something is wrong.
use default TimeZone
timeZone = TimeZone.getDefault();
}
else {
timeZone = new SimpleTimeZone(timeZoneOffset * 60 * 60 *
1000, ids[0]);
}
SimpleDateFormat sdf = new
SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(timeZone);
return sdf.format(new Date());
}
public static String
getFormatedDateString(String _timeZone) throws Exception{
SimpleDateFormat
s=new SimpleDateFormat("yyyy/MM/dd HH:mm");
Date ddd=s.parse("2014/6/13
4:00");
TimeZone timeZone =
null;
if("".equals(_timeZone)){
timeZone =
TimeZone.getDefault();
}else{
timeZone =
TimeZone.getTimeZone(_timeZone);
}
SimpleDateFormat sdf = new
SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
sdf.setTimeZone(timeZone);
//TimeZone.setDefault(timeZone);
return sdf.format(ddd);
}
public static void setCNTimeZone(){
final TimeZone zone =
TimeZone.getTimeZone("GMT+8");
TimeZone.setDefault(zone);
}
public static void main(String args[]) throws
Exception{
System.out.println(getFormatedDateString(""));
System.out.println(getFormatedDateString("Asia/Shanghai"));
System.out.println(getFormatedDateString("Asia/Seoul"));
System.out.println(getFormatedDateString("Europe/Madrid"));
System.out.println(getFormatedDateString("GMT+1:00"));
System.out.println(TimeZone.getDefault().getID());
}
}
Anddoi 将时间转换为指定时区的时间,布布扣,bubuko.com
原文地址:http://www.cnblogs.com/horrywu/p/3765363.html