Sun, 16 Nov 2008 03:53:12 GMT+8
请教:该如何写SimpleDateFormat,最好timezone通用一点的。试过EEE, dd MMM yyyy HH:mm:ss zzz不行。How?
请教:该如何写SimpleDateFormat,最好timezone通用一点的。试过EEE, dd MMM yyyy HH:mm:ss zzz不行。How?
解决方案 »
- java 将数据写入Excel中的问题,哪里错了,为什么运行不了?
- 我想请教一个简单的JAVA问题
- 新鲜出炉的用JAVA写的小游戏..点点世界V1.0发布!
- dwr为什么不能调用方法
- 别小看小问题,你不一定弄的明白!!!
- 难道csdn上没有高手?是高手就进来!
- 如何获取鼠标拖动的轨迹啊??
- 为什么openssl req时,找不到config info阿?回复有分
- 简单问题,急急急!!!
- ***急问!利用Java如何得到一个文件的属性信息例如:-rw-rw-rw-?***
- 我建立了一个ButtonGroup 如何把它添加到JDialog里去
- 我用applet播放flash,请帮忙看看程序出什么问题了。
SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz");
sdf.setTimeZone(TimeZone.getTimeZone("GMT-8"));
String s = sdf.format(new Date());
System.out.println(s);
将Sun, 16 Nov 2008 03:53:12 GMT+8解析为Date
String dateString = "Sun, 16 Nov 2008 03:53:12 GMT+8";
String pattern = "EEE,dd MMM yyyy HH:mm:ss z";
SimpleDateFormat formatter = new SimpleDateFormat(pattern,Locale.US);
try {
Date date = formatter.parse(dateString);
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));
}
catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
String pattern = "EEE,dd MMM yyyy HH:mm:ss z";
SimpleDateFormat formatter = new SimpleDateFormat(pattern,Locale.US);
Locale locale = new Locale("en","US");
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format,locale);你最大的问题正在这里 这里好像跟不同国家的习惯 有关系;
"EEE,dd MMM yyyy HH:mm:ss z";
是美国人的习惯, 所以要这么写哦
SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz",Locale.US);
format.setTimeZone(TimeZone.getTimeZone("GMT-9"));
System.out.println(format.format(format.parse("Sun, 16 Nov 2008 03:53:12 GMT")));
sdf.setTimeZone(TimeZone.getTimeZone("GMT-9"));
Date d = sdf.parse("Sun, 16 Nov 2008 03:53:12 GMT");//转换成Date
System.out.println(sdf.format(sdf.parse("Sun, 16 Nov 2008 03:53:12 GMT")))
应该是Sun, 16 Nov 2008 03:53:12 GMT+8:00 ,你再转换试一下
还有,我不只解析它一个,我还要通用性。
求高手出手相救
sdf.setTimeZone(TimeZone.getTimeZone("GMT-8"));
String s = sdf.format(new Date());
System.out.println(s);
{
int start = pos.index;
int oldStart = start;
int textLength = text.length(); boolean[] ambiguousYear = {false}; calendar.clear(); // Clears all the time fields for (int i = 0; i < compiledPattern.length; ) {
int tag = compiledPattern[i] >>> 8;
int count = compiledPattern[i++] & 0xff;
if (count == 255) {
count = compiledPattern[i++] << 16;
count |= compiledPattern[i++];
} switch (tag) {
case TAG_QUOTE_ASCII_CHAR:
if (start >= textLength || text.charAt(start) != (char)count) {
pos.index = oldStart;
pos.errorIndex = start;
return null;
}
start++;
break; case TAG_QUOTE_CHARS:
while (count-- > 0) {
if (start >= textLength || text.charAt(start) != compiledPattern[i++]) {
pos.index = oldStart;
pos.errorIndex = start;
return null;
}
start++;
}
break; default:
// Peek the next pattern to determine if we need to
// obey the number of pattern letters for
// parsing. It's required when parsing contiguous
// digit text (e.g., "20010704") with a pattern which
// has no delimiters between fields, like "yyyyMMdd".
boolean obeyCount = false;
if (i < compiledPattern.length) {
int nextTag = compiledPattern[i] >>> 8;
if (!(nextTag == TAG_QUOTE_ASCII_CHAR || nextTag == TAG_QUOTE_CHARS)) {
obeyCount = true;
}
}
start = subParse(text, start, tag, count, obeyCount,
ambiguousYear, pos);
if (start < 0) {
pos.index = oldStart;
return null;
}
}
} // At this point the fields of Calendar have been set. Calendar
// will fill in default values for missing fields when the time
// is computed. pos.index = start; // This part is a problem: When we call parsedDate.after, we compute the time.
// Take the date April 3 2004 at 2:30 am. When this is first set up, the year
// will be wrong if we're parsing a 2-digit year pattern. It will be 1904.
// April 3 1904 is a Sunday (unlike 2004) so it is the DST onset day. 2:30 am
// is therefore an "impossible" time, since the time goes from 1:59 to 3:00 am
// on that day. It is therefore parsed out to fields as 3:30 am. Then we
// add 100 years, and get April 3 2004 at 3:30 am. Note that April 3 2004 is
// a Saturday, so it can have a 2:30 am -- and it should. [LIU]
/*
Date parsedDate = calendar.getTime();
if( ambiguousYear[0] && !parsedDate.after(defaultCenturyStart) ) {
calendar.add(Calendar.YEAR, 100);
parsedDate = calendar.getTime();
}
*/
// Because of the above condition, save off the fields in case we need to readjust.
// The procedure we use here is not particularly efficient, but there is no other
// way to do this given the API restrictions present in Calendar. We minimize
// inefficiency by only performing this computation when it might apply, that is,
// when the two-digit year is equal to the start year, and thus might fall at the
// front or the back of the default century. This only works because we adjust
// the year correctly to start with in other cases -- see subParse().
Date parsedDate;
try {
if (ambiguousYear[0]) // If this is true then the two-digit year == the default start year
{
// We need a copy of the fields, and we need to avoid triggering a call to
// complete(), which will recalculate the fields. Since we can't access
// the fields[] array in Calendar, we clone the entire object. This will
// stop working if Calendar.clone() is ever rewritten to call complete().
Calendar savedCalendar = (Calendar)calendar.clone();
parsedDate = calendar.getTime();
if (parsedDate.before(defaultCenturyStart))
{
// We can't use add here because that does a complete() first.
savedCalendar.set(Calendar.YEAR, defaultCenturyStartYear + 100);
parsedDate = savedCalendar.getTime();
}
}
else parsedDate = calendar.getTime();
}
// An IllegalArgumentException will be thrown by Calendar.getTime()
// if any fields are out of range, e.g., MONTH == 17.
catch (IllegalArgumentException e) {
pos.errorIndex = start;
pos.index = oldStart;
return null;
} return parsedDate;
}
看看老外的回答: Convert it to the more standard GMT+08:00 before parsing.
这个建议很好,我觉得。我英语表达很不好,但人家都能看懂。
对于这个帖子,我的意思表达得够准确了。我想你们是瞟了两眼,然后从哪复制一个大概的答案,
很不负责任的回答!
话说回来,还是非常非常感谢认真回答的朋友。