[英]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;
}
}
LocalTime.parse( "03:23" )
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.