我是这么想的。 
 
我觉得问题可能在Demo2中的Date date=sdf.parse(s);这一句上。 
 
按照这个线索查找API文档,发现这个方法是DateFormat的。 
 
里面说See the parse(String, ParsePosition) method for more information on date parsing. 
 
然后在public abstract Date parse(String source, ParsePosition pos)里面有如下的解释: 
 
This parsing operation uses the calendar to produce a Date.  
As a result, the calendar's date-time fields and the TimeZone value may have been overwritten, depending on subclass implementations.  
Any TimeZone value that has previously been set by a call to setTimeZone may need to be restored for further operations. 
 
用parser方法得到一个Date对象,这个对象的TimeZone属性可能被重写过。我觉得可能是这个原因。更加具体的原因我也不知道了~~ 
 
在Demo2里面加一句format处理,得到的是个字符串。这时候就输出一致了。- ...
 
 - Date date = sdf.parse(s);
 
 - String dateStr = sdf.format(date); // 再次格式化一下,得到字符串                
 
 - System.out.println(dateStr);
 
 - ...
 
  复制代码 希望能帮到你。共同学习,共同进步! |