java时间转换,带时区的

现在我有一个时间类型是带时区的,“Fri Jan 17 11:14:45 CST 2014” 想把他转换成北京时间, 转了以后的时间类型是“yyyy-MM-dd HH:mm:ss”
望大神指点迷津

我假设了你的已知时间类型为Calendar,如果不是你也可以自己改成Date类型,代码如下:

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;

public class Test {

public static void main(String[] args) {
//假如这个是你已知的时间类型
Calendar cal = Calendar.getInstance();
cal.getTimeInMillis();
//北京时区GMT+8
Calendar beijingcal = Calendar.getInstance();
beijingcal.clear();
beijingcal.setTimeZone(TimeZone.getTimeZone("GMT+8"));
beijingcal.setTimeInMillis(cal.getTimeInMillis());
DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String beijingFormatStr = fmt.format(beijingcal.getTime());
System.out.println(beijingFormatStr);
}

}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-01-17
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());追问

明显不嘛。。。

第2个回答  2014-01-17
String dateString = "Fri Feb 01 00:00:00 GMT+08:00 2013";
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy",Locale.US);
TimeZone tz = TimeZone.getTimeZone("GMT+8");
sdf.setTimeZone(tz);
String str = sdf.format(Calendar.getInstance().getTime());
System.out.println(str);
Date s;
try {
s = sdf.parse(dateString);
sdf = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(sdf.format(s));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}本回答被网友采纳
第3个回答  2014-01-17
DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String time = fmt.format(new Date(System.currentTimeMillis()));System.out.println(time);
第4个回答  2014-01-17
去google百度一下,哈哈
相似回答