Java取消编组LocalDateTime [英] java unmarshall LocalDateTime

查看:55
本文介绍了Java取消编组LocalDateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的适配器类:

public class LocalDateTimeAdapter extends XmlAdapter<String, LocalDateTime> {

    @Override
    public LocalDateTime unmarshal(String v) throws Exception {
        return new LocalDateTime(v);
    }

    @Override
    public String marshal(LocalDateTime v) throws Exception {
        return v.toString();
    }

}

这是我要存储日期的对象类:

and this is an object-class where I want to store the date:

    @XmlAccessorType(XmlAccessType.FIELD)
    public class Object {

    @XmlJavaTypeAdapter(LocalDateTimeAdapter.class)
    private LocalDateTime time;

    public LocalDateTime getTime() {
            return time;
    }

由于某种原因,我无法编译它.它表明问题出在 return new LocalDateTime(v); .这是我得到的错误:

For some reason, I can't compile it. It shows that the problem is at return new LocalDateTime(v);. And this is the error I get:

Error:(9, 16) java: constructor LocalDateTime in class java.time.LocalDateTime cannot be applied to given types;
      required: java.time.LocalDate,java.time.LocalTime
      found: java.lang.String
      reason: actual and formal argument lists differ in length

和xml部分:

<time type="dateTime">2000-01-01T19:45:00Z</time>

我正在关注示例.

推荐答案

可能您正在使用

Probably you're using LocalDateTime from Java 8. This class has not any constructor for string.

在您关注的示例中, JodaTime .

In the example which you're following LocalDateTime is from JodaTime.

因此,您可以通过以下方式做到这一点:

So, you can do this in to ways:

  • 导入 org.joda.time.LocalDateTime (您需要JodaTime依赖项),而不是 java.time.LocalDateTime ;

  • Import org.joda.time.LocalDateTime (you will need JodaTime dependency) instead of java.time.LocalDateTime;

或将 unmarshal 方法更改为以下形式:

or change unmarshal method to something like this:

@Override
public LocalDateTime unmarshal(String v) throws Exception {
    return LocalDateTime.parse(v);
}

您可能需要通知

You may need to inform the date time format, as the default is a format to 2011-12-03T10:15:30, maybe this:

@Override
public LocalDateTime unmarshal(String v) throws Exception {
    return LocalDateTime.parse(v, DateTimeFormatter.ISO_INSTANT);
}

此外,在 java.time.LocalDateTime

Also, in java.time.LocalDateTime toString will output one of the following ISO-8601 formats:

  • uuuu-MM-dd'T'HH:mm
  • uuuu-MM-dd'T'HH:mm:ss
  • uuuu-MM-dd'T'HH:mm:ss.SSS
  • uuuu-MM-dd'T'HH:mm:ss.SSSSSS
  • uuuu-MM-dd'T'HH:mm:ss.SSSSSSSSS

这篇关于Java取消编组LocalDateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆