3 回答
TA贡献1993条经验 获得超5个赞
您可以使用GsonBuilder将时间解析为日期,指定日期格式:
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setDateFormat("HH:mm a");
Gson gson = gsonBuilder.create();
Pojo fromJson = gson.fromJson(<string>, Pojo.class);
POJO 字段将如下所示(如果需要,请重命名):
private Date start_time;
private Date end_time;
TA贡献1820条经验 获得超2个赞
你对图书馆要求不高。您可以自己用纯 Java 编写该类,因为您只比较一天中的 24 小时。你可以这样:
public class TimeKeeper implements Comparable<TimeKeeper> {
byte startHour;
byte startMinute;
byte endHour;
byte endMinute;
public TimeKeeper(String start, String end) {
startHour = parseHour(start);
startMinute = parseMinute(start);
endHour = parseHour(end);
endMinute = parseMinute(end);
}
public byte parseHour(String time) {
byte hour = Byte.valueOf(String.split(time, ":")[0]);
if(time.contains("PM") {
hour += 12;
}
return hour;
}
public byte parseMinute(String time) {
return Byte.valueOf(String.valueOf(time, ":")[1].substring(0, 2));
}
@Override
public int compareTo(TimeKeeper tk) {
int diff = startHour - tk.startHour;
if(diff == 0) {
return startMinute - tk.startMinute;
}
return diff;
}
}
添加回答
举报