SORU
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 ss

Eğ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ş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Brandon McCrary

    Brandon McCr

    15 Ocak 2012
  • Candyrat Records

    Candyrat Rec

    10 Mayıs 2006
  • The Dubstep FX

    The Dubstep

    5 Mart 2011