简体   繁体   English

如何使 LocalTime 实例变量根据我的特定格式接受 json 数据?

[英]How can i make LocalTime instance variable accept json data based on my specific format?

im having a problem, im basically getting data as Json objects from an API, the field time in the Json object is set to a format like this: HH:mm im having a problem, im basically getting data as Json objects from an API, the field time in the Json object is set to a format like this: HH:mm

I am having a problem filling in LocalTime object since its defualt format looks like this 23:37:48.120473200 (LocalTime is an instance variable in my Message class)我在填写 LocalTime object 时遇到问题,因为它的默认格式看起来像这样23:37:48.120473200 (LocalTime 是我的Message类中的一个实例变量)

how can i solve this?我该如何解决这个问题?

Message class消息 class


public class Message {
    private LocalDate date;
    private LocalTime time;
    private String message;
    private String messageId;


    public Message(LocalDate date, LocalTime time, String message, String messageId) {
        this.date = date;
        this.time = time;
        this.message = message;
        this.messageId = messageId;
    }

    @Override
    public String toString() {
        return "Message{" +
                "date='" + date + '\'' +
                ", time='" + time + '\'' +
                ", message='" + message + '\'' +
                ", messageId='" + messageId + '\'' +
                '}';
    }

    public String getMessageId() {
        return messageId;
    }

    public void setMessageId(String messageId) {
        this.messageId = messageId;
    }

    public LocalDate getDate() {
        return date;
    }

    public void setDate(LocalDate date) {
        this.date = date;
    }

    public LocalTime getTime() {
        return time;
    }

    public void setTime(LocalTime time) {
        this.time = time;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

}

tl;dr tl;博士

LocalTime.parse( "03:23" )

Details细节

Text in format of hour in 24-hour clock with padding zero, followed by a COLON and minute with padding zero, complies with the ISO 8601 standard for textual date-time formats.格式为 24 小时制小时格式的文本,后跟一个冒号和带有填充零的分钟,符合文本日期时间格式的ISO 8601标准。

The java.time classes use ISO 8601 formats by default when parsing such text. java.time类在解析此类文本时默认使用 ISO 8601 格式。 So no need to specify a formatting pattern.所以不需要指定格式模式。

String input = "03:23" ;
LocalTime lt = LocalTime.parse( input ) ;

If you use a framework for parsing your JSON, you should be able to ask the framework to do this parsing for you.如果您使用框架来解析 JSON,您应该能够要求框架为您执行此解析。 The major JSON frameworks have been updated to leverage the java.time classes.主要的 JSON 框架已更新以利用java.time类。

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

相关问题 如何根据特定变量(ID)使此类工作? - How can I make this class work based on a on specific variable (ID)? 如何让我的 POST 请求接受以下格式的 JSON 有效负载? 我正在使用 Jackson 进行此操作 - How do I make my POST request accept my JSON payload in the following format ? I am using Jackson for this operation 如何使用 LocalTime 在 Java 中求和两次? - How to sum two times in Java, using LocalTime? 如何使方法接受Java中的类类型变量? - How can I make a method accept a class type variable in Java? 如何使ListAdapter接受所有点击? - How can I make my ListAdapter accept all clicks? 如何使接口实例方法仅接受同一类的参数? - How can I make an interface instance method accept arguments of the same class only? 我怎样才能使接口实例方法只接受同一个类的参数,真的吗? - How can I make an interface instance method accept arguments of the same class only, really? 如何获取Spring @RestController以JSON格式而不是www-form-urlencoded接受参数? - How can I get a Spring @RestController to accept parameters in JSON format rather than www-form-urlencoded? 如何将 LocalTime 添加到日期 object 中? - How can I add LocalTime into a Date object? 如何使用另一个类的实例变量接受用户输入? - How do I accept user input with a instance variable of another class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM