gmt转换北京时间 java_GMT时间与北京时间的转换
文章参考了很多其他技术网站,然后自己再根据需求,调试出来的,感谢其他网站的筒子们,临时关了那些网站,无法附链接了,在此SAY SORRY
js版本的【GMT时间转换成北京时间】代码:
/****************************************************
功能描述:将GMT时间转换成北京时间
参数说明:
value:要格式化的时间
strFormat:格式,如:'Y-m-d H:i:s'
******************************************************/
this.GMTtoTime = function(value){
if(value=="" || value==null){
return "";
}
else{
var tempValue = value.replace("T", " ");
var dateBefore = tempValue.slice(0,10);
var timeBefore = tempValue.slice(11,19);
var dateArray = dateBefore.split("-");
var timeArray = timeBefore.split(":");
//注意,Date对象中的getMonth() 返回0~11
var feedDate = Date.UTC(dateArray[0],dateArray[1]-1,dateArray[2],timeArray[0],timeArray[1],timeArray[2],0) + 8*60*60;
var now = new Date();
now.setTime(feedDate);
if (now.getMonth()<10){
var m=0;
m=now.getMonth()+1;
var month = "0" + m;
}else{
var month = now.getMonth()+1;
}
if (now.getDate()<10){
var d=0;
d=now.getDate()+1;
var date = "0" + d;
}else{
var date = now.getDate();
}
var dateAfter = now.getFullYear() + "-" + month + "-" + date;
// var dateAfter = month + "月" + date + "日";
if (now.getHours()<10){
var hour = "0" + now.getHours();
}else{
var hour = now.getHours();
}
if (now.getMinutes()<10){
var minute = "0" + now.getMinutes();
}else{
var minute = now.getMinutes();
}
var timeAfter = hour + ":" + timeArray[1]+":"+timeArray[2];
var timeFinal = dateAfter + " " + timeAfter;
return timeFinal;
}
}
java版本的【北京时间转成GMT时间】
/**
* 将日期转换成GMT日期 方法
* @param d
* @return
*/
public static Date getTimeToGMT(Date d){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
DateFormat gmt = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
gmt.setTimeZone(TimeZone.getTimeZone("GMT"));
String gmtStr=gmt.format(d);//日期 转成 字符串
Date gmt_date =null;
try {
gmt_date = sdf.parse(gmtStr); //字符串 转成 日期
} catch (ParseException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
return gmt_date;
}