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

橙三吉。 提交于 2019-11-28 06:19:20

问题


I want to convert Date(ActionScript 3) to java.util.Date through a xml.

First, write a user defined ActionScript class like this.

public class User
{
    public function User()
    {
        userDate = new Date();
    }

    public var id:String = null;
    public var password:String = null;
    public var userDate:Date = null;

}

Second, create its instance and set each of values, so it converts ActionScript class to a xml for using XMLEncoder having its schema file.

This is the result xml, and send this xml to a server for using a HTTPService.

<User>
  <id>system</id>
  <password>manager</password>
  <userDate>Fri Jan 14 09:02:17 GMT+0900 2011</userDate>
</User>

Finally, In server side of Java, I want to convert this xml to Java class like this for using JAXB Unmarshaller.

public class User {

    public User() {
    }

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


    public void setId(String id) {
        this.id = id;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public void setUserDate(Date userDate) {
        this.userDate = userDate;
    }
    public String getId() {
        return id;
    }
    public String getPassword() {
        return password;
    }
    public Date getUserDate() {
        return userDate;
    }

}

But, as a result, "UserDate" property is only going to be null...

Why "UserDate" property is null ? And, please tell me solutions if any.


回答1:


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.




回答2:


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:

  • http://bdoughan.blogspot.com/2010/07/xmladapter-jaxbs-secret-weapon.html
  • http://bdoughan.blogspot.com/2010/12/jaxb-and-immutable-objects.html
  • http://bdoughan.blogspot.com/2010/12/represent-string-values-as-element.html

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:

  • http://bdoughan.blogspot.com/2010/12/extending-jaxb-representing-annotations.html

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]



回答3:


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.



来源:https://stackoverflow.com/questions/4687188/how-to-convert-dateactionscript-3-to-java-util-date-through-a-xml

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