您的位置:首页 > 编程语言 > Java开发

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

2014-08-29 14:26 405 查看
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 block
e.printStackTrace();
}

} else if (string.contains("-")&&string.contains(":")) {
Date newDate;
try {
newDate = resultSdf.parse(string);
return newDate;
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else if(string.contains("-")&&!string.contains(":")){
Date newDate;
try {
newDate = resultSdfdate.parse(string);
return newDate;
} catch (ParseException e) {
// TODO Auto-generated catch block
e.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 

MaskDescription
dDay 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.
NameMaskExample
defaultddd mmm dd yyyy HH:MM:ssSat Jun 09 2007 17:46:21
shortDatem/d/yy6/9/07
mediumDatemmm d, yyyyJun 9, 2007
longDatemmmm d, yyyyJune 9, 2007
fullDatedddd, mmmm d, yyyySaturday, June 9, 2007
shortTimeh:MM TT5:46 PM
mediumTimeh:MM:ss TT5:46:21 PM
longTimeh:MM:ss TT Z5:46:21 PM EST
isoDateyyyy-mm-dd2007-06-09
isoTimeHH:MM:ss17:46:21
isoDateTimeyyyy-mm-dd'T'HH:MM:ss2007-06-09T17:46:21
isoUtcDateTimeUTC: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()); 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: