How to convert Date(ActionScript 3) to java.util.Date through a xml?

我的未来我决定 提交于 2019-11-29 12:50:24

The text representation of your Flash Date and java.util.Date are probably not compatible. Since both date objects are internally based on the number of milliseconds since January 1, 1970, I would recommend using date.time in AS3 to get the integer value of the date, sending it to Java, and then using date.setTime() to set the correct date.

You can use an XmlAdapter to accomplish this:

package example;

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

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

public class DateAdapter extends XmlAdapter<String, Date> {

    private SimpleDateFormat format = new SimpleDateFormat("EEE MMM d HH:mm:ss zZ yyyy");

    @Override
    public Date unmarshal(String v) throws Exception {
        System.out.println(format.parse(v));
        return format.parse(v);
    }

    @Override
    public String marshal(Date v) throws Exception {
        return format.format(v);
    }

}

Then specify this XmlAdapter on the userDate property on your User class:

package example;

import java.util.Date;    
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlRootElement(name="User")
public class User {

    private String id;
    private String password;
    private Date userDate;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @XmlJavaTypeAdapter(DateAdapter.class)
    public Date getUserDate() {
        return userDate;
    }

    public void setUserDate(Date userDate) {
        this.userDate = userDate;
    }

}

For more information on XmlAdapter see:

UPDATE

Based on your comments, if you want to specify this as a package level annotation you need to include a class called package-info in the same package as your model classes. This class will look like:

@XmlJavaTypeAdapter(value=DateAdapter.class, type=Date.class)
package example;

import java.util.Date;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

If you want an alternative means to specify JAXB metadata you could use the XML representation extension in EclipseLink JAXB (MOXy), I'm the tech lead:

Alternatively, you could ensure that the date that is passed to JAXB is in the following format (xsd:dateTime):

[-]CCYY-MM-DDThh:mm:ss[Z|(+|-)hh:mm]

You may just want to look into using BlazeDS to handle the job of conversion between AMF (serialized AS3) and serialized POJO.

I don't have a lot of experience with using JAXB to unmarshall but found this out on the interwebs.

The correct format for an XML Schema dateTime type is: yyyy-mm-ddThh:mm:ss

If you change your date to the following do you still see the error? 2006-02-01T08:00:00

You could use a DateFormatter if your in Flex otherwise you could make a getter on the AS3 side to return the appropriately formatted date. weltraumpirat's answer seems equally valid.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!