In this Android application, I have a date field in the SQLITE database table, defined as:
String createTable = "CREATE TABLE " + TABLE_RUNS + " ( " +
_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COL_RUN_DATE + " DATETIME DEFAULT CURRENT_DATE, " +...
When you insert a new record, it automatically places the date as YYYY-MM-DD.
At the time of viewing, I step the date by this method that returns it in DD / MM / YY 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: enter 2017-01-26 sai 01/26/17
Later, when updating the registry, I try to do the reverse, but it is not working. Via debug I noticed that there is always exception in the line commented below:
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: Enter 26/01/17 and leaves 26/01/17 when it should be leaving 2017-01-26
The display format may vary. If it was always day / month / year, it was just to 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 what is good.
UPDATE: Due to the content of the answers, it is necessary to clarify the situation a little more.
When the record is generated, the date is automatically entered in the format yyyy-MM-dd, for example, 2017-01-25. The 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 registry data and that will update the database, I take the date and convert the display format, whatever, in the format yyyy-MM-dd, which is the crux of the matter proposal.
Some comments:
1) Database operations methods, 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 retained, but as it is of type
database.update(TABLE_RUNS, values, whereClause, null);
I have to resume all values.
2) My solution for jerico: I'm going to save the original date in%% of textView of the date and time to update, instead% I'll% use tag
. Resolves, but it's a twist. It would be legal (and less asnice) to have one (or two) universal method (s) for conversion from / to dates.