I have a sort on an ArrayList;
matchesList.sort(Comparator.comparing(matches::getDate).thenComparing(matches::getTime));
The value comes from a Firebase database so the value is stored as a String instead of Date (I don't want to have to store the date value as long format) in the following format; dd/MM/yyyy
The sort above does not work as expected, I'm guessing after reading through other posts that this is due to the comparator not knowing what the String date value actually means so cannot sort it as expected.
After some reading and playing around with, ideally I'd like to convert the class that defines my ArrayList structure to support the Date type but when trying this I get an error about cannot convert a string value to date when looping the dataset and trying to populate the ArrayList;
matches match = matchSnapshot.getValue(matches.class);
Again another post talks about this being due to Firebase not supporting Date types, but I've also tried using *.toString on Date type variables and still it doesn't like this.
My question is; is there a way to store the date format dd/MM/yyyy as String and then to cast that to date that can be added to an ArrayList?
I would suggest you to cast your dd/MM/yyyy format String to timestamp (long) and then sorting timestamps (you can use Collections.sort() for that). More on timestamps here: Convert string Date into timestamp in Android?
Then, when you need to use those sorted values again, just cast them back to date. More about this here: Convert timestamp into string Date in Android?
Hope it helps.
Add your own Comparator and use the other comparing() method.
matchesList.sort(Comparator.comparing(matches::getDate, myComparator).thenComparing(matches::getTime));
Because the date format you are using does not sort naturally, it's not going to be able to do it properly. I don't know of any date comparator that's readily available in a standard library, so probably best to just write your own.
class DateComparator implements Comparator<String> {
#Override
public int compare(String date1, String date2) {
// TODO: compare your date strings
}
}
Related
How to compare dates which stored as String in Sqlite Database and fetch data...?
I am storing date in a format 19-03-2014 14:23:43
I have tried BETWEEN and also >= and <= but none of them is working. And I want to compare only Date and not Time. So I am passing value as 20-03-2014, If time is necessary to pass then I don't have any problem to pass 00:00:00
String comparisons use the lexicographical ordering of characters.
To be able to compare date strings correctly, the most significant field must come first, i.e., you must use the format yyyy-MM-dd.
Set this date into date object and use before(obj) and after(obj) method of date object to compare two date.
in the TextView I have the date formatted in "Locale" now I have to insert it into the sqlite db. How do I convert the format suitable for SQLite?
This is my Content Values
cv.put(MyTable.DATE, mDate.getText().toString());
You can insert it as a long:
cv.put(MyTable.DATA, mDate.getTime());
Then retrieve it as a long and use the following constructor:
mDate = new Date(cv.getAsLong(MyTable.DATA));
Additionally, if you need to save the Locale, just store it separately as a String:
cv.put(MyTable.LOCALE, mLocale.getLanguage());
and get it later:
mLocale = new Locale(cv.getAsString(MyTable.LOCALE));
I suggest that you store strings in text format for machine-readable code. See
http://developer.android.com/reference/java/text/SimpleDateFormat.html. SimpleDateFormat allows you to go between Date and String. I've seen some documentation that suggests you should pass dates internally in binary format. I tend to disagree. Storing a date in text format (if you know the format) is simpler and safer.
My date is in text format, as SQLite has no data type for date and time, so now I want to sort my data by date.
So when I query the database like
SELECT jobno, ondate FROM Reports ORDER BY DATE(ondate)
this will return the data sort alphabetically not by data. How would I sort by date?
Either use Unix-epoch format, which is basically an INTEGER type, or time string (YYYY-MM-DD stuff).
A list of time string formats you can use can be found here.
Sort unix-epoch as you would sort an INTEGER type, and a time string as you would sort a TEXT type.
When SQLite compares/sorts strings of the form dd/mm/YYYY, it handles them like any other string, i.e., the first characters in the string have priority, e.g., 01/10/2013 is sorted before 22/01/2000.
If you ever compare or sort dates, or use any of SQLite's built-in date functions, you must use one of the supported date formats, like YYYY-MM-DD.
(In you particular query, you are calling the DATE function, which returns NULL if it does not recognize the format of its parameter. This means that your records were not sorted at all.)
I would suggest 2 things, to put your dates in milliseconds or use a text with the format YYYYMMDD.
Convert them every time you want to display them or you want to store them.
It's not complicated, and it will save you a lot of trouble.
Storing the date as a timestamp i.e in millisecond is a better option.
From how to store 6:00pm in sqlite database in android, I know that for SQLite, there is no data type "TIME".
So, if I want to store time(year,month,day,hour,min,sec), how can I create that column? I should use "TEXT"?? But, how can I parse them for Android and
if I want to sort by time, how can I do it?
Thank for all your help!
You can define for yourself a date format (link) and you can create let's say a DateManager class to convert a date to a string (to store it in the SQLite database) and to convert a string into a date, both based on that date format specified by you.
The easiest solution would be to store the Date as miliseconds (long).
You can get the miliseconds from a date using date.getTime() when you store them, and when you re-create your Date object you could use Date d=new Date(milliseconds).
I'm writing an application where users enter some details into an SQLite Database. I need to write code whereby the last 31 days of stored data in the database is outputted.
The code to save the date string is:
currentDateString = DateFormat.getDateInstance().format(new Date());
This gives the date in the format (for todays date) as 30 Mar 2012.
Is there anyway to change this data format into DD/MM/YYYY? (30/03/2012)
This string is saved into the SQL Database as follows:
ContentValues dbcv = new ContentValues();
dbcv.put(KEY_DATE, date);
(currentDateTime String = Date in previous class)
I wish to output every row of the database that comes within the last 31 days.
How can I do this?
Thanks!
First of all, you are doing two questions instead on just one, I will try to give you an answer to both.
To change the date format in java take a look at the documentation for the SimpleDateFormat, you can pass a pattern to the constructor of that class to make it format your dates the way you really want. In your specific case the pattern you are looking for is dd/MM/yyyy
If you are storing dates in your database as strings you lose some of the benefits that storing dates as they are have. For example, your second question will be easily performed if you were storing dates instead of strings in your tables since the only thing you need to achieve what you want is to do a select operation to your table using the date functions provided by SQLLite. Take a look a this webpage as it will give you an idea of what you can do with those functions: SQLLite Date Functions
Note: Of course, if you still want to store them as strings the only solution you will have is read all the records, parse all the strings back to dates and use the java.util.Calendar class to check which record is older than 31 days and which one is not. I'd rather do the SQL query instead of that as it has too much better performance (specially on a mobile device).
EDIT:
SQLLite and Android are not specifically my strengths, I did a small research and I found several posts in which people were actually storing them as strings as you initially started to do. If SQLLite on Android doesn't support storing dates as they are (as other databases do) then maybe the best option in your scenario is using a long value as the date. Look at the Date definition class, it has a method getTime that returns the date value as long value, you can use that value later to create a new instance of a date (using its constructor).
To format that date (the Date object, of course) in something readable by the user, DateFormat (or any of its implementations) is still the answer.
To do the query you want to do having dates stored as longs I suggest you to take a look to the DateUtils class from the Apache Commons Lang library. In that class you will find several functions that work on dates, so, before creating the query you need to fetch the rows in last 31 dates, you could use something like:
Date now = new Date();
Date before = DateUtils.addDays(now, -31);
long beforeAsLong = before.getTime();
String query = "select * from TABLE_NAME where DATE_FIELD >= " + beforeAsLong;
Hope this helps.