简体   繁体   English

Jackson json xmlgregoriancalendar 解串器

[英]Jackson json xmlgregoriancalendar deserializer

I have a problem with parsing json. It has a date for exapmle - "2014-01-07".我在解析 json 时遇到问题。它有一个示例日期 - “2014-01-07”。 And when it parses and became to createUserRequest.getBirthday() it contain - "2014-01-07T04:00:00.000+04:00".当它解析并变成 createUserRequest.getBirthday() 时,它包含 - “2014-01-07T04:00:00.000+04:00”。 I need it in createUserRequest object, then I will assert it with another object. The question is how to get just "2014-01-07"?我在 createUserRequest object 中需要它,然后我将用另一个 object 断言它。问题是如何获得“2014-01-07”?

In CreateUserRequest I have XMLGregorianCalendar variable and cannot change it.在 CreateUserRequest 中,我有 XMLGregorianCalendar 变量,无法更改它。

protected XMLGregorianCalendar birthday;受保护的 XMLGregorianCalendar 生日;

Below just pulled out part of the code.下面只是拉出一部分代码。 Ignore class and variable names.忽略 class 和变量名。

public class Test {
    private static final ObjectMapper MAPPER = new ObjectMapper();
    
    public static <T> T parseJson(String pathname, Class<T> objectClass) throws Exception {
        return MAPPER.readValue(new File(pathname), objectClass);
    }

    public void parse() throws Exception {
        CreateUserRequest createUserRequest =
                Test.parseJson("src/test/resources/createUser.json", CreateUserRequest.class);
        System.out.println(createUserRequest.getBirthday());
    }
}

LocalDate

Your Question is unclear.你的问题不清楚。 But if you are asking how to parse a date-only value represented in text as "2014-01-07" , the answer is to parse as a LocalDate object.但是,如果您询问如何解析文本中表示为"2014-01-07"的仅限日期的值,答案是解析为LocalDate object。

LocalDate ld = LocalDate.parse( "2014-01-07" ) ;

The XMLGregorianCalendar class represents a moment, a date with time of day as seen in a particular time zone. XMLGregorianCalendar class 代表一个时刻,一个在特定时区中看到的具有一天中时间的日期。 For a date only value, this is the wrong class to use.对于仅日期值,这是错误的 class。 Furthermore, this class is now legacy, supplanted years ago by the modern java.time classes.此外,这个 class 现在是遗留的,多年前被现代的java.time类取代。

The latest versions of Jackson support java.time . Jackson 的最新版本支持java.time

Conversion转换

If handed a XMLGregorianCalendar object by code not yet updated to java.time , convert from that legacy class to the modern java.time classes.如果通过尚未更新为java.time的代码将 XMLGregorianCalendar object 传递给XMLGregorianCalendar object,请将其从遗留的 class 转换为现代的java.time类。

Look to new conversion methods added to the old classes.查看添加到旧类的新转换方法。

You need to convert your XMLGregorianCalendar object to ZonedDateTime by way of GregorianCalendar .您需要通过GregorianCalendarXMLGregorianCalendar object 转换为ZonedDateTime

GregorianCalendar gc = myXmlGregorianCalendar.toGregorianCalendar() ;
ZonedDateTime zdt = gc.toZonedDateTime() ;

A ZonedDateTime represent a date with time of day as seen in a particular time zone. ZonedDateTime表示在特定时区中看到的日期和时间。 But you are interested in only the date portion.但是您只对日期部分感兴趣。 So extract a LocalDate object.所以提取一个LocalDate object。

LocalDate ld = zdt.toLocalDate() ;

Compare to your target date with LocalDate#isEqual .使用LocalDate#isEqual与您的目标日期进行比较。

LocalDate target = LocalDate.parse( "2014-01-07" ) ;
if( ld.isEqual( target ) ) { … }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM