25 Mart 2010, PERŞEMBE
jaxb unmarshal zaman damgası
Yapamam JAX-RS Resteasy sunucu uygulaması bir zaman damgası unmarshal için JAXB.
Benim sınıf bu gibi görünüyor:
@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement(name = "foo")
public final class Foo {
// Other fields omitted
@XmlElement(name = "timestamp", required = true)
protected Date timestamp;
public Foo() {}
public Date getTimestamp() {
return timestamp;
}
public String getTimestampAsString() {
return (timestamp != null) ? new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(timestamp) : null;
}
public void setTimestamp(final Date timestamp) {
this.timestamp = timestamp;
}
public void setTimestamp(final String timestampAsString) {
try {
this.timestamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(timestampAsString);
} catch (ParseException ex) {
this.timestamp = null;
}
}
}
Herhangi bir fikir?
Teşekkürler.
CEVAP
5 Temmuz 2010, PAZARTESİ
JAXB java işleyebilir.açıklama.Sınıf tarih. Ancak biçimi umuyor:
< . p ^"yyyy-aa-gg Mİ İYİ:dd:" yerine "yyyy-aa-gg HH:mm:". ss ssEğer bir XmlAdapter kullanmanızı öneriyorum tarih biçimi kullanmak isterseniz, aşağıdaki gibi görünecektir:
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.xml.bind.annotation.adapters.XmlAdapter;
public class DateAdapter extends XmlAdapter<String, Date> {
private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Override
public String marshal(Date v) throws Exception {
return dateFormat.format(v);
}
@Override
public Date unmarshal(String v) throws Exception {
return dateFormat.parse(v);
}
}
Sonra zaman damgası mülkiyet: bu adaptör belirtirsiniz
import java.util.Date;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement(name = "foo")
public final class Foo {
// Other fields omitted
@XmlElement(name = "timestamp", required = true)
@XmlJavaTypeAdapter(DateAdapter.class)
protected Date timestamp;
public Foo() {}
public Date getTimestamp() {
return timestamp;
}
public void setTimestamp(final Date timestamp) {
this.timestamp = timestamp;
}
}
Bunu Paylaş:
Nasıl java ile Joda-Zaman kullanmak iç...
Eklemek için bir Dosya Adı, zaman Damg...
'0000-00-00 00:00:00' java o...
Datetime, zaman Damgası arasında dönüş...
Zaman Damgası Python...