Advertisement

GMT时间格式转换

阅读量:

普通的时间转换问题我这里就不再罗嗦了,我想大家应该都会那种低级的转换问题吧,现在我向大家总结一下如何转换GMT时间格式,这种格式的转换方法网上还不是很多,所以有必要总结一下,也算给有需要的朋友一个小小的帮助啦。

1、可以使用

SimpleDateFormat SimpleDateFormat

EEE-三位星期

d-天

MMM-月

yyyy-四位年

HH-24小时制消息

mm-分钟

ss-秒

‘GMT’-GMT字符串,别忘了加上单引号

通过该掩码,能完美的构造出HTTP头所需的日期格式。

2、文件修改时间是本地时间,如果要对文件时间进行换算,换成GMT时间,则要加上时区的设置,

sdf.setTimeZone(TimeZone.getTimeZone("GMT"));

代码

  1. String sFile = "c:\ test.jpg";
  2. File file = new File(sFile);
  3. if(file.exists()){
  4. long lm = file.lastModified();
  5. Calendar cd = Calendar.getInstance();
  6. cd.setTimeInMillis(lm);
  7. SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss 'GMT'", Locale.US);
  8. sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
  9. String timeStr = sdf.format(cd.getTime());
  10. String timeStr = sdf.format(cd.getTime());
  11. log.debug("lastModify=" + timeStr);
  12. }

3、设置Locale.US, 是因为根据机器的本地化设置,输出的是中文,这里只想输出英文, SimpleDateFormat构造函数如下:

SimpleDateFormat(String pattern, Locale locale)

输出结果:

lastModify=Thu, 15 Sep 2011 12:13:41 GMT

首先先来了解一下GMT的时间格式:

Mon Feb 13 08:00:00 GMT+08:00 2012 可能还会有其他的格式类似 Sun Sep 02 2012 08:00:00 GMT+08:00 只是顺序改变而已。

那么我们如何将这种格式转换成普通date格式呢,方法如下:

第一种实现方法:

[html]

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Locale;

/**

  • @author yaohucaizi

*/

public class DateFormat {

public static void main(String[] args) throws ParseException {

String s = "Mon Feb 13 08:00:00 GMT+08:00 2012";

// String s = "Sun Sep 02 2012 08:00:00 GMT+08:00";

SimpleDateFormat sf = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy", Locale.ENGLISH);

// SimpleDateFormat sf = new SimpleDateFormat("EEE MMM dd yyyy hh:mm:ss z", Locale.ENGLISH);

Date date = sf.parse(s);

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

String result = sdf.format(date);

System.out.println(result);

}

}

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Locale;

/**

  • @author yaohucaizi

*/

public class DateFormat {

public static void main(String[] args) throws ParseException {

String s = "Mon Feb 13 08:00:00 GMT+08:00 2012";

// String s = "Sun Sep 02 2012 08:00:00 GMT+08:00";

SimpleDateFormat sf = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy", Locale.ENGLISH);

// SimpleDateFormat sf = new SimpleDateFormat("EEE MMM dd yyyy hh:mm:ss z", Locale.ENGLISH);

Date date = sf.parse(s);

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

String result = sdf.format(date);

System.out.println(result);

}

}

第二种方法:

首先将GMT日期转换成long型毫秒数然后再进一步的转换,看代码:

[html]

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Locale;

/**

  • @author yaohucaizi

*/

public class DateFormat {

public static final String SOURCE = "Wed Feb 13 08:00:00 +0800 2012";

public static void main(String[] args) throws ParseException {

SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", new Locale("ENGLISH", "CHINA"));

Date myDate = sdf.parse(SOURCE);

System.out.println(myDate);

sdf.applyPattern("EEE MMM dd HH:mm:ss Z yyyy");

System.out.println(sdf.format(myDate));

SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", new Locale("CHINESE", "CHINA"));

System.out.println(sdf2.format(myDate));

sdf2.applyPattern("yyyy年MM月dd日 HH时mm分ss秒");

System.out.println(sdf2.format(myDate));

long miliSeconds = myDate.getTime();

System.out.println("自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象经过的毫秒数为:" + miliSeconds + "毫秒");

}

}

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Locale;

/**

  • @author yaohucaizi

*/

public class DateFormat {

public static final String SOURCE = "Wed Feb 13 08:00:00 +0800 2012";

public static void main(String[] args) throws ParseException {

SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", new Locale("ENGLISH", "CHINA"));

Date myDate = sdf.parse(SOURCE);

System.out.println(myDate);

sdf.applyPattern("EEE MMM dd HH:mm:ss Z yyyy");

System.out.println(sdf.format(myDate));

SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", new Locale("CHINESE", "CHINA"));

System.out.println(sdf2.format(myDate));

sdf2.applyPattern("yyyy年MM月dd日 HH时mm分ss秒");

System.out.println(sdf2.format(myDate));

long miliSeconds = myDate.getTime();

System.out.println("自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象经过的毫秒数为:" + miliSeconds + "毫秒");

}

}

输出结果为:

[html]

Mon Feb 13 08:00:00 GMT+08:00 2012

Mon Feb 13 08:00:00 +0800 2012

2012-02-13 08:00:00

2012年02月13日 08时00分00秒

自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象经过的毫秒数为:1329091200000毫秒

Mon Feb 13 08:00:00 GMT+08:00 2012

Mon Feb 13 08:00:00 +0800 2012

2012-02-13 08:00:00

2012年02月13日 08时00分00秒

自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象经过的毫秒数为:1329091200000毫秒

GMT(Greenwich Mean Time)是格林尼治平时:

由于地球轨道并非圆形,其运行速度又随着地球与太阳的距离改变而出现变化,因此视太阳时欠缺均匀性。视太阳日的长度同时亦受到地球自转轴相对轨道面的倾斜度所影响。为着要纠正上述的不均匀性,天文学家计算地球非圆形轨迹与极轴倾斜对视太阳时的效应。平太阳时就是指经修订后的视太阳时。在格林尼治子午线上的平太阳时称为世界时(UTC),又叫格林尼治平时(GMT)。

全部评论 (0)

还没有任何评论哟~