1

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.

Take
  • 213
  • 1
  • 6
  • 12

3 Answers3

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.

weltraumpirat
  • 22,544
  • 5
  • 40
  • 54
  • Nope. http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Date.html#time – weltraumpirat Mar 23 '12 at 00:13
  • My bad. Actually either works: help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Date.html#getTime() (Though stack overflow can't handle the URL correctly.) – DoomGoober May 09 '12 at 01:25
1

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]
bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • Thank you Blaise! But I want to accomplish this without any annotations in Java classes. And I know @XmlJavaTypeAdapters annotaion can put in package-info.java file, but I don't know how to use package-info.java file... – Take Jan 17 '11 at 00:01
  • @Take - I have updated my answer with instructions on using package-info. – bdoughan Jan 17 '11 at 14:57
  • @Blaise, can @ XmlJavaTypeAdapter work WITHOUT a @ XmlRootElement model/xml definition? We have a Java interface defining a Web Service. A WSDL 'contract' is generated, via CXF/ant (3rd party codes a server from this). The interface contains an @ XmlJavaTypeAdapter method that returns a MAP, unmarshalled by an Adapter based on the WSMAP* solution found @ http://goo.gl/wlG6F. I've tried both direct annotation and at package-info.java, but I get an 'org.apache.cxf.interceptor.Fault: Unmarshalling Error: unexpected element (uri:"", local:"return")' and my 'WSMapAdapter' isn't being called. Ideas? – Big Rich Feb 20 '12 at 12:48
0

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.

shaunhusain
  • 19,630
  • 4
  • 38
  • 51