gpt4 book ai didi

java - 日期排序 ArrayList

转载 作者:行者123 更新时间:2023-11-29 05:00:09 25 4
gpt4 key购买 nike

<分区>

伙计们,我有一个奇怪的要求。我有一个 Object,它包含一个 Date 引用,通常称为 endDate。除此之外,还有另一个名为 status 的字符串字段,它选择诸如“Soon”、“Ended”和“Closed” 之类的值。此类对象位于 ArrayList 中。

我想对此实现排序策略。

要应用的第一个排序是基于耗时。我想在 EndDate 上进行 First 排序,最接近现在的差异应该排在第一位。即,如果当前时间是 Sep 3,2015 7:34 PM IST 并且存在一个对象,其 endDateSep 3,2015 7:36 PM IST.

第二个是在 status 上,soon 的所有对象的逻辑应该首先出现,然后是 ended 的,然后是 closed

例子:如果输入是

[0]( Sep 1,2015 10:36 PM IST ,ended)
[1]( Sep 3,2015 7:36 PM IST , soon )
[2]( Aug 29,2015 11:16 AM IST , closed)
[3]( Sep 7,2015 12:00 PM IST , soon )
[4]( Sep 1,2015 12:12 PM IST , ended)

那么输出应该是

[0]( Sep 3,2015 7:36 PM IST , soon)
[1]( Sep 7,2015 12:00 PM IST , soon)
[2]( Sep 1,2015 10:36 PM IST ,ended)
[3]( Sep 1,2015 12:12 PM IST , ended)
[4]( Aug 29,2015 11:16 AM IST , closed)

注意:此处使用的时间戳仅供引用

代码:比较器内部的比较实​​现,时间戳被读取为 GMT。

@Override
public int compare(JSONObject a, JSONObject b) {

SimpleDateFormat dateFormatGmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateFormatGmt.setTimeZone(TimeZone.getTimeZone("GMT"));
SimpleDateFormat dateFormatLocal = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

try {
Date currentdate = dateFormatLocal.parse( dateFormatGmt.format(new Date()));
} catch (ParseException e1) {
e1.printStackTrace();
}

try {
Date betEnds1 = dateFormatLocal.parse((String) a.get(KEY_NAME));
Date betEnds2 = dateFormatLocal.parse((String) b.get(KEY_NAME));


return betEnds1.compareTo(betEnds2);
}
catch (JSONException e) {
return -1;
} catch (ParseException e) {
return -1;
}
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com