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


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 




Mask Description
d Day of the month as digits; no leading zero for single-digit days.
dd Day of the month as digits; leading zero for single-digit days.
ddd Day of the week as a three-letter abbreviation.
dddd Day of the week as its full name.
m Month as digits; no leading zero for single-digit months.
mm Month as digits; leading zero for single-digit months.
mmm Month as a three-letter abbreviation.
mmmm Month as its full name.
yy Year as last two digits; leading zero for years less than 10.
yyyy Year represented by four digits.
h Hours; no leading zero for single-digit hours (12-hour clock).
hh Hours; leading zero for single-digit hours (12-hour clock).
H Hours; no leading zero for single-digit hours (24-hour clock).
HH Hours; leading zero for single-digit hours (24-hour clock).
M Minutes; no leading zero for single-digit minutes.
Uppercase M unlike CF timeFormat's m to avoid conflict with months.
MM Minutes; leading zero for single-digit minutes.
Uppercase MM unlike CF timeFormat's mm to avoid conflict with months.
s Seconds; no leading zero for single-digit seconds.
ss Seconds; leading zero for single-digit seconds.
l or L Milliseconds. l gives 3 digits. L gives 2 digits.
t Lowercase, single-character time marker string: a or p.
No equivalent in CF.
tt Lowercase, two-character time marker string: am or pm.
No equivalent in CF.
T Uppercase, single-character time marker string: A or P.
Uppercase T unlike CF's t to allow for user-specified casing.
TT Uppercase, two-character time marker string: AM or PM.
Uppercase TT unlike CF's tt to allow for user-specified casing.
Z US 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.
o GMT/UTC timezone offset, e.g. -0500 or +0230.
No equivalent in CF.
S The 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.
Name Mask Example
default ddd mmm dd yyyy HH:MM:ss Sat Jun 09 2007 17:46:21
shortDate m/d/yy 6/9/07
mediumDate mmm d, yyyy Jun 9, 2007
longDate mmmm d, yyyy June 9, 2007
fullDate dddd, mmmm d, yyyy Saturday, June 9, 2007
shortTime h:MM TT 5:46 PM
mediumTime h:MM:ss TT 5:46:21 PM
longTime h:MM:ss TT Z 5:46:21 PM EST
isoDate yyyy-mm-dd 2007-06-09
isoTime HH:MM:ss 17:46:21
isoDateTime yyyy-mm-dd'T'HH:MM:ss 2007-06-09T17:46:21
isoUtcDateTime UTC: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()); 






你可能感兴趣的:(spring,Date,GMT,CST,格林尼治时间)