0
In this Android app, I have a date field in the SQLITE database table, thus defined:
String createTable = "CREATE TABLE " + TABLE_RUNS + " ( " +
_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COL_RUN_DATE + " DATETIME DEFAULT CURRENT_DATE, " +...
When entering a new record, it automatically places the date as YYYY-MM-DD.
When displaying, I pass the date by this method that returns it in DD/MM/AA format. This is done by calling the following method:
String strDateToShow(String dateToFormat){
// format date to display
SimpleDateFormat formatFrom, formatTo;
formatFrom = new SimpleDateFormat("yyyy-MM-dd");
formatTo = new SimpleDateFormat();
if(dateToFormat != null) {
try {
Date mDate = formatFrom.parse(dateToFormat);
dateToFormat = formatTo.format(mDate);
} catch (Exception e) {
e.printStackTrace();
}
}
if(dateToFormat == null){
dateToFormat = formatTo.format(new Date());
}
return stringToSpace(dateToFormat);
}
Example: between 2017-01-26 sai 26/01/17
Later, when it comes to updating the record, I try to do the reverse, but it’s not working. Via debug I noticed that there is always an exception in the following commented line:
private String strDateToStore(String dateToFormat){
// format date
SimpleDateFormat formatFrom, formatTo;
formatFrom = new SimpleDateFormat();
formatTo = new SimpleDateFormat("yyyy-MM-dd");
if(dateToFormat != null) {
try {
Date mDate = formatFrom.parse(dateToFormat); // <-- aqui ocorre a exceção.
dateToFormat = formatTo.format(mDate);
} catch (Exception e) {
e.printStackTrace();
}
}
if(dateToFormat == null){
dateToFormat = formatTo.format(new Date());
}
return stringToSpace(dateToFormat);
}
Example: Enters 26/01/17 and leaves 26/01/17 when it should leave 2017-01-26
The display format may vary. If it was always day/month/year, just make it explicit, but the format should be the place where the application is being used.
PS. The solution does not need to use the methods. Just convert from and to that is good.
UPDATE: Due to the content of the answers, it is necessary to clarify a little more the situation.
When the record is generated, the date is automatically inserted into the yyyy-MM-dd format, for example 2017-01-25. Records are shown in a list. At this time, the date is converted to the display format. At some point, the user decides to change the record data and it will update the database, I take the date and convert the display format, whatever, in the format yyyy-MM-dd, which is the core of the proposed issue.
Some remarks:
1) the methods of operations in the database, use a POJO, so I need to reformat the date. If it were an explicit UPDATE (SQL) statement, I could skip the date and it would be kept, but as is the type
database.update(TABLE_RUNS, values, whereClause, null);
I have to take back all my values.
2) My Jerico solution: I will record the original date on tag
textView’s date and time to update, instead getText()
I’ll use getTag()
. Solve, but it’s a branch break. It would be cool (and less asnice) to have one (or two) universal(s) method(s) for converting from/to dates.
The dd-MM-yy format reflects the LOCALE. I’m not sure it will always be this format. If it had, it would be solved. It has to pull the format of the date that the device is using?
– Rene Freak
@Renefreak Let me see if I understand your question, in the input information you have to take the format configured in the system to perform the conversion?
– Julio Borges
@Renefreak, if you need Voce you can take the Dateformat of the system with 'Dateformat.getDateFormat(context)' and pass the format as method parameter.
– Julio Borges
what I need: to display the date in the format that the user expects to see their dates, be dd/mm, mm/dd (you don’t know this in advance). What we know is, how the date is stored in the database: yyyy-mm-dd. Look at the answer I gave, with the changes in the code you suggested.
– Rene Freak