Java 时区独立

Java 时区独立,java,jaxb,timezone,Java,Jaxb,Timezone,我有一个转换日期时间的类,如下所示: import java.text.SimpleDateFormat; import java.util.Date; import java.util.TimeZone; import javax.xml.bind.annotation.adapters.XmlAdapter; public class DateFormatConverter extends XmlAdapter<String, Date> { private stati

我有一个转换日期时间的类,如下所示:

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import javax.xml.bind.annotation.adapters.XmlAdapter;

public class DateFormatConverter extends XmlAdapter<String, Date> {

  private static final String XML_DATE_PATTERN = "yyyy-MM-dd HH:mm:ss.SSS z";

  @Override
  public Date unmarshal(String xmlDate) throws Exception {
    if (xmlDate == null || xmlDate.length() == 0) 
    {
      return null;
    }    
    SimpleDateFormat xmlDateFormat = new SimpleDateFormat(XML_DATE_PATTERN);    
    return xmlDateFormat.parse(xmlDate);
  }

  @Override
  public String marshal(Date date) throws Exception {
    if (date == null) {
      return "";
    }    
    SimpleDateFormat xmlDateFormat = new SimpleDateFormat(XML_DATE_PATTERN);    
    return xmlDateFormat.format(date);
  }
}
控制台中的输出为:

Default Timezone:ICST
Timezone of formatter:ICST
Timezone:ICST
MarshaledDate: 2011-10-13 00:00:00.000 ICT
例外情况是:

Marshalled date string is not expected. expected:<...S...> but was:<......>
junit.framework.ComparisonFailure: Marshalled date string is not expected. expected:<...S...> but was:<......>
不需要封送的日期字符串。预期:但是:
junit.framework.ComparisonFailure:不需要封送的日期字符串。预期:但是:

为什么封送日期具有时区ICT,而我所在位置的默认时区为ICST。我应该如何修正以使时区独立?

我怀疑您看到了“时区的一部分”和“时区名称”之间的区别。例如,英国的时区使用GMT和BST,两者实际上都是时区的一部分,ID为“欧洲/伦敦”,尽管我不知道在这种情况下显示的名称是什么


如果此处不需要默认时区,请在创建时将其指定为类似UTC的格式。(在构造函数调用之后调用
setTimeZone

我修复了它,问题是这一行的原因:我修复了它,问题是这一行的原因:String timezone=Calendar.getInstance().getTimeZone().getDisplayName(true,timezone.SHORT);将true更改为false,因为默认情况下SimpleDataFormat使用非日光格式(在本例中为ICT)。
Marshalled date string is not expected. expected:<...S...> but was:<......>
junit.framework.ComparisonFailure: Marshalled date string is not expected. expected:<...S...> but was:<......>