[英]Compare java getTime time with strings representing a date
I have stored the date of a specific method each time it gets executed in ab sqlite database in 3 columns (one for the day, the month and the year). 每次将特定方法的日期存储在ab sqlite数据库中的3列中(我分别将其存储在日期,月份和年份中)分别存储在3列中。
Now I want to compare it to the date of the actutal day the user uses the app. 现在,我想将其与用户使用该应用的实际日期进行比较。 With 同
Date currentTime = Calendar.getInstance().getTime()
I get this date, but how am I able to compare it to the strings I get from my database? 我得到了这个日期,但是如何将其与从数据库中获得的字符串进行比较? Thank you! 谢谢!
Your Question is a duplicate of many others. 您的问题是许多其他问题的重复。 So briefly… 简而言之...
Use java.time classes rather than the troublesome old legacy date-time classes. 使用java.time类,而不要使用麻烦的旧式旧版日期时间类。 For Android, use libraries from the ThreeTen-Backport and ThreeTenABP projects. 对于Android,请使用ThreeTen-Backport和ThreeTenABP项目中的库。
Get today's date. 获取今天的日期。
ZoneId z = ZoneId.of( "America/Montreal" ) )
LocalDate today = LocalDate.now( z );
Get to parts of the date. 获取部分日期。
int y = today.getYear() ;
int m = today.getMonthValue() ;
int d = today.getDayOfMonth() ;
Query the database. 查询数据库。
myPreparedStatement.setInt( 1 , y ) ;
myPreparedStatement.setInt( 2 , m ) ;
myPreparedStatement.setInt( 3 , d ) ;
As others suggested, you should be using date-time types in your database to store date-time values rather than mere ints for the pieces. 正如其他人所建议的那样,您应该在数据库中使用日期时间类型来存储日期时间值,而不是仅使用整数作为片段。
The java.time framework is built into Java 8 and later. java.time框架内置于Java 8及更高版本中。 These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
. 这些类取代了麻烦的旧的旧式日期时间类,例如java.util.Date
, Calendar
和SimpleDateFormat
。
The Joda-Time project, now in maintenance mode , advises migration to the java.time classes. 现在处于维护模式的Joda-Time项目建议迁移到java.time类。
To learn more, see the Oracle Tutorial . 要了解更多信息,请参见Oracle教程 。 And search Stack Overflow for many examples and explanations. 并在Stack Overflow中搜索许多示例和说明。 Specification is JSR 310 . 规格为JSR 310 。
Where to obtain the java.time classes? 在哪里获取java.time类?
The ThreeTen-Extra project extends java.time with additional classes. ThreeTen-Extra项目使用其他类扩展了java.time。 This project is a proving ground for possible future additions to java.time. 该项目为将来可能在java.time中添加内容提供了一个试验场。 You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more . 您可以在这里找到一些有用的类,比如Interval
, YearWeek
, YearQuarter
,和更多 。
Try this: 尝试这个:
SimpleDateFormat formattedDate = new SimpleDateFormat("dd/MM/yyyy");
String strDate= formattedDate.format(date);
It will give you your date as shown in the Template. 如模板中所示,它将为您提供日期。 Now simply build a String out of your SQLite data in the same way (dd/MM/yyyy) and you cam simply compare them like shown in this post: https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java 现在,只需以相同的方式(dd / MM / yyyy)从SQLite数据中构建一个String,就可以像本文中所示对它们进行简单比较: https : //stackoverflow.com/questions/513832/how-do-i -compare串式的Java
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.