0

I would like to get the current date and put into hashmap so I can store it in a database. How do I do that?

For example:

 Map<String, String> map = new HashMap<String, String>();{
 map.put("Speed", speed.gettext.tostring());
 map.put("date",currentdate());

  }
Spotty
  • 197
  • 2
  • 2
  • 14

2 Answers2

1

If you use

Map<String, String>

you have no other choice than format the date as string, see SimpleDateFormat. You can use (Simple)DateFormat's format and parse functions to convert date to string and string to date.

But it is not a nice solution, because there are no date type in sqlite. According to https://www.sqlite.org/datatype3.html you have 3 options to store date(time):

  • TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS").
  • REAL as Julian day numbers, the number of days since noon in Greenwich on November 24, 4714 B.C. according to the proleptic Gregorian calendar.
  • INTEGER as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC.

you can see also: Best way to work with dates in Android SQLite

Community
  • 1
  • 1
Dániel Kis
  • 2,341
  • 5
  • 28
  • 51
0

You can extract the Year, month and day as below.

Calendar cal = Calendar.getInstance();
cal.setTime(date);
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH); // month starts at 0 and not from 1.
int day = cal.get(Calendar.DAY_OF_MONTH);
nits.kk
  • 5,204
  • 4
  • 33
  • 55