1

我收到一个填充有数据的 json 对象,使用这些数据的任务之一是将时间组合在一起。

例如,如果GAME_TIME是 12:15 PM,它应该在 12 PM 标题下。

我想出了一个解决方案,但它是一个怪物,并且很难调试,必须有更好的方法来做到这一点。

    DbDate = DateFormatter.parse(json_data.getString("GAME_TIME"));

if (DbDate.compareTo(DateFormatter.parse("12:00 AM")) == 0 || DbDate.compareTo(DateFormatter.parse("1:00 AM") ) < 0)  {
    Users12AM.add(json_data.getString("IN_GAME_NAME"));
} else if (DbDate.compareTo(DateFormatter.parse("1:00 AM")) == 0 || DbDate.compareTo(DateFormatter.parse("2:00 AM"))  < 0) {
    Users1AM.add(json_data.getString("IN_GAME_NAME"));
} else if (DbDate.compareTo(DateFormatter.parse("2:00 AM")) == 0 || DbDate.compareTo(DateFormatter.parse("3:00 AM"))  < 0) {
    Users2AM.add(json_data.getString("IN_GAME_NAME"));
} else if (DbDate.compareTo(DateFormatter.parse("3:00 AM")) == 0 || DbDate.compareTo(DateFormatter.parse("4:00 AM"))  < 0) {
    Users3AM.add(json_data.getString("IN_GAME_NAME"));
} else if (DbDate.compareTo(DateFormatter.parse("4:00 AM")) == 0 || DbDate.compareTo(DateFormatter.parse("5:00 AM"))  < 0) {
    Users4AM.add(json_data.getString("IN_GAME_NAME"));
} else if (DbDate.compareTo(DateFormatter.parse("5:00 AM")) == 0 || DbDate.compareTo(DateFormatter.parse("6:00 AM"))  < 0) {
    Users5AM.add(json_data.getString("IN_GAME_NAME"));
} else if (DbDate.compareTo(DateFormatter.parse("6:00 AM")) == 0 || DbDate.compareTo(DateFormatter.parse("7:00 AM"))  < 0) {
    Users6AM.add(json_data.getString("IN_GAME_NAME"));
} else if (DbDate.compareTo(DateFormatter.parse("7:00 AM")) == 0 || DbDate.compareTo(DateFormatter.parse("8:00 AM"))  < 0) {
    Users7AM.add(json_data.getString("IN_GAME_NAME"));
} else if (DbDate.compareTo(DateFormatter.parse("8:00 AM")) == 0 || DbDate.compareTo(DateFormatter.parse("9:00 AM"))  < 0) {
    Users8AM.add(json_data.getString("IN_GAME_NAME"));
} else if (DbDate.compareTo(DateFormatter.parse("9:00 AM")) == 0 || DbDate.compareTo(DateFormatter.parse("10:00 AM"))  < 0) {
    Users9AM.add(json_data.getString("IN_GAME_NAME"));
} else if (DbDate.compareTo(DateFormatter.parse("10:00 AM")) == 0 || DbDate.compareTo(DateFormatter.parse("11:00 AM"))  < 0) {
    Users10AM.add(json_data.getString("IN_GAME_NAME"));
} else if (DbDate.compareTo(DateFormatter.parse("11:00 PM")) == 0 || DbDate.compareTo(DateFormatter.parse("12:00 PM"))  < 0) {
    Users11AM.add(json_data.getString("IN_GAME_NAME"));
} else if (DbDate.compareTo(DateFormatter.parse("12:00 PM")) == 0 || DbDate.compareTo(DateFormatter.parse("1:00 PM"))  < 0) {
    Users12PM.add(json_data.getString("IN_GAME_NAME"));
} else if (DbDate.compareTo(DateFormatter.parse("1:00 PM")) == 0 || DbDate.compareTo(DateFormatter.parse("2:00 PM"))  < 0) {
    Users1PM.add(json_data.getString("IN_GAME_NAME"));
} else if (DbDate.compareTo(DateFormatter.parse("2:00 PM")) == 0 || DbDate.compareTo(DateFormatter.parse("3:00 PM"))  < 0) {
    Users2PM.add(json_data.getString("IN_GAME_NAME"));
} else if (DbDate.compareTo(DateFormatter.parse("3:00 PM")) == 0 || DbDate.compareTo(DateFormatter.parse("4:00 PM"))  < 0) {
    Users3PM.add(json_data.getString("IN_GAME_NAME"));
} else if (DbDate.compareTo(DateFormatter.parse("4:00 PM")) == 0 || DbDate.compareTo(DateFormatter.parse("5:00 PM"))  < 0) {
    Users4PM.add(json_data.getString("IN_GAME_NAME"));
} else if (DbDate.compareTo(DateFormatter.parse("5:00 PM")) == 0 || DbDate.compareTo(DateFormatter.parse("6:00 PM"))  < 0) {
    Users5PM.add(json_data.getString("IN_GAME_NAME"));
} else if (DbDate.compareTo(DateFormatter.parse("6:00 PM")) == 0 || DbDate.compareTo(DateFormatter.parse("7:00 PM"))  < 0) {
    Users6PM.add(json_data.getString("IN_GAME_NAME"));
} else if (DbDate.compareTo(DateFormatter.parse("7:00 PM")) == 0 || DbDate.compareTo(DateFormatter.parse("8:00 PM"))  < 0) {
    Users7PM.add(json_data.getString("IN_GAME_NAME"));
} else if (DbDate.compareTo(DateFormatter.parse("8:00 PM")) == 0 || DbDate.compareTo(DateFormatter.parse("9:00 PM"))  < 0) {
    Users8PM.add(json_data.getString("IN_GAME_NAME"));
} else if (DbDate.compareTo(DateFormatter.parse("9:00 PM")) == 0 || DbDate.compareTo(DateFormatter.parse("10:00 PM"))  < 0) {
    Users9PM.add(json_data.getString("IN_GAME_NAME"));
} else if (DbDate.compareTo(DateFormatter.parse("10:00 PM")) == 0 || DbDate.compareTo(DateFormatter.parse("11:00 PM"))  < 0) {
    Users10PM.add(json_data.getString("IN_GAME_NAME"));
} else if (DbDate.compareTo(DateFormatter.parse("11:00 PM")) == 0 || DbDate.compareTo(DateFormatter.parse("12:00 PM"))  < 0) {
    Users11PM.add(json_data.getString("IN_GAME_NAME"));
}
4

3 回答 3

2

Presumably you can extract the hour from your DbDate object, whatever that is. Convert it to 24hr time. If you can't a Calendar certainly can do this for you.

Extract the hour as 0 - 23, use that as a key for Map, store a list of Strings in the Map. Done.

Map<Integer, List<String>> myMap = new HashMap<Integer, List<String>>();
...

// `hour` here represents the 24hr value described above
List<String> list = myMap.get(hour);
if (null == list) 
{
    // There's no List for this hour - create and insert it.
    list = new ArrayList<String>();
    myMap.put(hour, list);
}
list.add(json_data.getString("IN_GAME_NAME"));

If you'd like to skip the null check bit and be guaranteed there's a List for every hour, pre-populate the Map with a loop, creating and inserting the List<String> for each hour 0 - 23.

于 2013-08-09T19:50:00.097 回答
1

Store everything in a HashMap<Date,String>. Date is the key, identifying the "group", and the String is your ingame_name. You could first fill the HashMap with all empty groups, and then iterate over them.

于 2013-08-09T19:15:25.810 回答
0

有。使用数据库。这正是内存中 SQLite 数据库的用途。只需创建一个SQLiteOpenHelper对象,传递nullname,您将获得一个内存中的仅限此进程的 SQL 数据库。

然后,只需将所有数据转储到一个表中并运行类似于SELECT * FROM data ORDER BY time(date_field) ASC. 有关详细信息,请参阅日期和时间函数

You can of course do a lot more processing on the SQLite side so that you express it in a language meant for data processing.

Alternatively, you can of course create a Comparator and use Collections.sort() to achieve a similar effect.

于 2013-08-09T19:15:15.937 回答