如何通过xml将Date(ActionScript 3)转换为java.util.Date?

发布于 2024-10-12 09:23:26 字数 1379 浏览 8 评论 0原文

我想通过 xml 将 Date(ActionScript 3) 转换为 java.util.Date。

首先,像这样编写一个用户定义的 ActionScript 类。

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

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

}

其次,创建其实例并设置每个值,以便将 ActionScript 类转换为 xml,以便使用具有其架构文件的 XMLEncoder。

这是结果 xml,并将此 xml 发送到服务器以使用 HTTPService。

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

最后,在 Java 的服务器端,我想将此 xml 转换为 Java 类,如下所示,以便使用 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;
    }

}

但是,结果,“UserDate”属性只会为 null...

为什么“UserDate”属性为 null ? 并且,请告诉我解决方案(如果有)。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝眼泪 2024-10-19 09:23:26

Flash Datejava.util.Date 的文本表示形式可能不兼容。由于这两个日期对象在内部都是基于自 1970 年 1 月 1 日以来的毫秒数,因此我建议在 AS3 中使用 date.time 来获取日期的整数值,将其发送到 Java,然后使用 date.setTime() 设置正确的日期。

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.

八巷 2024-10-19 09:23:26

您可以使用 XmlAdapter 来完成此操作:

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);
    }

}

然后在 User 类的 userDate 属性上指定此 XmlAdapter:

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;
    }

}

有关 XmlAdapter 的详细信息,请参阅:

更新

根据您的评论,如果您想将其指定为包级注释,您需要包含一个名为package-info 与模型类位于同一包中。这个类看起来像:

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

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

如果您想要一种指定 JAXB 元数据的替代方法,您可以使用 EclipseLink JAXB (MOXy) 中的 XML 表示扩展,我是技术主管:

或者,您可以确保传递给 JAXB 的日期采用以下格式 (xsd:dateTime):

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

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]
云之铃。 2024-10-19 09:23:26

您可能只想研究使用 BlazeDS 来处理 AMF(序列化 AS3)和序列化 POJO 之间的转换工作。

我没有太多使用 JAXB 进行解组的经验,但在互联网上发现了这一点。

XML 架构日期时间类型的正确格式是:
yyyy-mm-ddThh:mm:ss

如果将日期更改为以下内容,您还会看到错误吗?
2006-02-01T08:00:00

如果你在 Flex 中,你可以使用 DateFormatter,否则你可以在 AS3 端创建一个 getter 来返回适当格式的日期。 weltraumpirat 的回答似乎同样有效。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文