GMT时间格式转换(Mon Feb 13 08:00:00 GMT+08:00 2012)

GMT时间格式转换(Mon Feb 13 08:00:00 GMT+08:00 2012)普通的时间转换问题我这里就不再罗嗦了 我想大家应该都会那种低级的转换问题吧 现在我向大家总结一下如何转换 GMT 时间格式 这种格式的转换方法网上还不是很多 所以有必要总结一下 也算给有需要的朋友一个小小的帮助啦 首先先来了解一下 GMT 的时间格式 MonFeb1308 00 00GMT 08 002012 可能还会有其他的格式类似 SunSep0 00 00G

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

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

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

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

第一种实现方法:

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型毫秒数然后再进一步的转换,看代码:

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 + "毫秒"); } }

输出结果为:

 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 对象经过的毫秒数为:00毫秒 



版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/233878.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号