Java中Date的toGMTString已经不推荐使用
JavaScript中Date的toGMTString输出的却是UTC格式的
想用不能用,郁闷
找到些快速的解决办法
以下以是代码
JavaScript版
/*normalize Date.prototype.toUTCString*/
(function(){
if(new Date(Date.UTC(2000,0,1)).toUTCString()!=="Sat, 01 Jan 2000 00:00:00 GMT"){
var dayAbbrs=["Sun","Mon","Tue","Wed","Thu","Fri","Fri"];
var monthAbbrs=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
var f=function(n){return n<10?"0"+n:""+n;};
var DateToUTCString=function() {
return isFinite(this.valueOf())?
dayAbbrs[this.getUTCDay()]+", "+
f(this.getUTCDate())+' '+
monthAbbrs[this.getUTCMonth()]+' '+
this.getUTCFullYear()+' '+
f(this.getUTCHours())+':'+
f(this.getUTCMinutes())+':'+
f(this.getUTCSeconds())+' GMT':
null;
};
Date.prototype.toUTCString0=Date.prototype.toUTCString;
Date.prototype.toGMTString=Date.prototype.toUTCString=DateToUTCString;
}
})();
Java版
package com.jbird.util;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class DateUtils {
public static void main(String[] args) {
System.out.println(DateUtils.toGMTString(new Date());
}
public static String toGMTString(Date date) {
SimpleDateFormat df = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z", Locale.UK);
df.setTimeZone(new java.util.SimpleTimeZone(0, "GMT"));
return df.format(date);
}
}