如何按日期以字符串形式订购房间结果 [英] How to Order Room results by Date as String
本文介绍了如何按日期以字符串形式订购房间结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个room database
.我有date
列,并且已另存为string
.
I have a room database
. I have a column for date
and it's saved as string
.
我使用此query
来对我的列进行排序:
I used this query
for sort my column :
@Query("SELECT * FROM session WHERE class_id = :classId ORDER BY session_date ASC")
List<SessionEntry> getAllSessions(int classId);
结果:
1398/11/25
1398/11/29
1398/12/5
1398/2/14
1398/4/25
1398/6/17
1398/6/30
1398/7/9
1398/9/14
但是我想这样排序:
1398/2/14
1398/4/25
1398/6/17
1398/6/30
1398/7/9
1398/9/14
1398/11/25
1398/11/29
1398/12/5
有什么方法可以按日期作为字符串排序而不修改数据库结构吗?
Is there any way I could order by Date as String without modifying the database structure ?
推荐答案
我找到了解决方法,但这不是最好的方法.
I found a solution but it's not best.
这种方法适用于无法将Date Column
从字符串更改为另一种类型的情况
This way is for when you can't change your Date Column
from String to another type
List<SessionEntry> sessionEntries = mDb.sessionDao().getAllSessions(classId);
Collections.sort(sessionEntries, comparing(SessionEntry::convertStringToDate));
会话条目:
public class SessionEntry {
.
.
.
public Date convertStringToDate() {
try {
return new SimpleDateFormat("yyyy/MM/dd").parse(getSessionDate());
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}
这篇关于如何按日期以字符串形式订购房间结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文