java格林尼治时间等转成常规日期类型字符串

来源:互联网 发布:网络舆情危机 编辑:程序博客网 时间:2024/05/17 23:38


private Date formatDate(String string) {SimpleDateFormat resultSdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");SimpleDateFormat resultSdfdate = new SimpleDateFormat("yyyy-MM-dd");if (string != null) {if (string.contains("CST")) {long d2 = Date.parse(string);Date datetime = new Date(d2);return datetime;} else if (string.contains("Z")) {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'.'sss'Z'");java.util.Date datetime;try {datetime = sdf.parse(string);return (Date) datetime;} catch (ParseException e) {// TODO Auto-generated catch blocke.printStackTrace();}} else if (string.contains("-")&&string.contains(":")) {Date newDate;try {newDate = resultSdf.parse(string);return newDate;} catch (ParseException e) {// TODO Auto-generated catch blocke.printStackTrace();}} else if(string.contains("-")&&!string.contains(":")){Date newDate;try {newDate = resultSdfdate.parse(string);return newDate;} catch (ParseException e) {// TODO Auto-generated catch blocke.printStackTrace();}}else {Date longDate = new Date(Long.parseLong(string));return longDate;}}return null;}


上述方法可以转如下格式:

2014-06-27T08:11:07.168Z


Fri Aug 15 08:00:37 CST 2014


1404359095333


2014-08-29 




MaskDescriptiondDay of the month as digits; no leading zero for single-digit days.ddDay of the month as digits; leading zero for single-digit days.dddDay of the week as a three-letter abbreviation.ddddDay of the week as its full name.mMonth as digits; no leading zero for single-digit months.mmMonth as digits; leading zero for single-digit months.mmmMonth as a three-letter abbreviation.mmmmMonth as its full name.yyYear as last two digits; leading zero for years less than 10.yyyyYear represented by four digits.hHours; no leading zero for single-digit hours (12-hour clock).hhHours; leading zero for single-digit hours (12-hour clock).HHours; no leading zero for single-digit hours (24-hour clock).HHHours; leading zero for single-digit hours (24-hour clock).MMinutes; no leading zero for single-digit minutes.
Uppercase M unlike CF timeFormat's m to avoid conflict with months.MMMinutes; leading zero for single-digit minutes.
Uppercase MM unlike CF timeFormat's mm to avoid conflict with months.sSeconds; no leading zero for single-digit seconds.ssSeconds; leading zero for single-digit seconds.l or LMilliseconds. l gives 3 digits. L gives 2 digits.tLowercase, single-character time marker string: a or p.
No equivalent in CF.ttLowercase, two-character time marker string: am or pm.
No equivalent in CF.TUppercase, single-character time marker string: A or P.
Uppercase T unlike CF's t to allow for user-specified casing.TTUppercase, two-character time marker string: AM or PM.
Uppercase TT unlike CF's tt to allow for user-specified casing.ZUS timezone abbreviation, e.g. EST or MDT. With non-US timezones or in the Opera browser, the GMT/UTC offset is returned, e.g.GMT-0500
No equivalent in CF.oGMT/UTC timezone offset, e.g. -0500 or +0230.
No equivalent in CF.SThe date's ordinal suffix (st, nd, rd, or th). Works well with d.
No equivalent in CF.'…' or "…"Literal character sequence. Surrounding quotes are removed.
No equivalent in CF.UTC:Must be the first four characters of the mask. Converts the date from local time to UTC/GMT/Zulu time before applying the mask. The “UTC:” prefix is removed.
No equivalent in CF.NameMaskExampledefaultddd mmm dd yyyy HH:MM:ssSat Jun 09 2007 17:46:21shortDatem/d/yy6/9/07mediumDatemmm d, yyyyJun 9, 2007longDatemmmm d, yyyyJune 9, 2007fullDatedddd, mmmm d, yyyySaturday, June 9, 2007shortTimeh:MM TT5:46 PMmediumTimeh:MM:ss TT5:46:21 PMlongTimeh:MM:ss TT Z5:46:21 PM ESTisoDateyyyy-mm-dd2007-06-09isoTimeHH:MM:ss17:46:21isoDateTimeyyyy-mm-dd'T'HH:MM:ss2007-06-09T17:46:21isoUtcDateTimeUTC:yyyy-mm-dd'T'HH:MM:ss'Z'2007-06-09T22:46:21Z

格林尼治时间转化详解:

首先  Mon Dec 09 22:06:24 格林尼治标准时间+0800 2013   字段一个格林尼治标准时间时间,一般情况下字段中不会含有中文,对于这种格式有两种解决方法

1剔除中文字符串

public static String convertGMTToLoacale(String gmt){
        String cc = gmt.substring(0, 19) + gmt.substring(33, 38);
        SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy",new Locale("English"));
        try {
            Date date = sdf.parse(cc);
            SimpleDateFormat dateformat = new SimpleDateFormat("dd/MM");
            String result = dateformat.format(date);
            return result;
        } catch (ParseException e) {
        }
        return "";
    }

2.第二种方法是在不进行字符串剔除的情况下:

在simpleDateFormat方法中将格式字符串变换为:"EEE MMM dd HH:mm:ss 格林尼治标准时间+0800 yyyy" 就可以了。这样就可一将时间转换为Date类型:
private DateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss 格林尼治标准时间+0800 yyyy",Locale.ENGLISH);



3.第三种方法:

String str = "Wed Jun 5 00:00:00 GMT+08:00 2013";//在08与00之间加:
java.text.SimpleDateFormat sdf = new SimpleDateFormat("E MMM dd HH:mm:ss z yyyy",Locale.US);
System.out.println(sdf.format(new Date()));
Date d;
try {
d = sdf.parse(str);
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(d));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}



4.第四种方法:

 long d2 = Date.parse("Fri Aug 15 08:00:37 CST 2014"); 
Date datetime=new Date(d2);
       System.out.println(datetime.toLocaleString()); 






1 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 晒出成片的斑怎么办 太阳晒出胳膊上长斑怎么办 宇航员在太空死后怎么办 太阳暴晒起的斑怎么办 太阳晒出来的斑怎么办 被认定D级危房怎么办 突然发现两个关系遥远怎么办 如果没有地球人类会怎么办 小锅盖被屏蔽了怎么办 美的冰箱故障通讯不合格怎么办 美的冰箱通讯不合格怎么办 文明6金币降到0怎么办 紫癜肾炎长期尿潜血怎么办 肾移植后血压高怎么办 尿道长了个肿瘤怎么办 吃了有病的鹅怎么办 狗狗得了乳腺瘤怎么办 孕28周还是臀位怎么办 怀孕五个月胎位不正怎么办 33周了胎位不正怎么办 足月胎儿不足5斤怎么办 绒癌观察期怀孕怎么办 宝宝囱门闭合晚怎么办 慢性硬脑膜下血肿复发怎么办 佝偻病导致囟门晚闭怎么办 儿童液体补多了怎么办 脑脊液鼻漏3年了怎么办 结石掉到膀胱里怎么办 肾结石引起的腰疼怎么办 肾结石小但很疼怎么办 狗狗得了尿结石怎么办 生理期第四天必须游泳怎么办 碎石后吐的厉害怎么办 白细胞高红细胞高血尿怎么办? 早期肾癌术后复发该怎么办 肾结石因运动引起尿血怎么办 宝宝大便镜检阳性潜血怎么办 尿不尽刺痛带血怎么办 狗狗拉肚子拉血怎么办 肾血肿怎么办才吸收快 体检尿蛋白高3怎么办