简体   繁体   中英

Android - How can I store Date/Time to my database?

I am trying to follow this post but I am not sure how to store my data in the iso8601 format because I am not getting any "seconds" input from the timepicker interface, can I just append "0000" to the end of the string builder?

The post also uses SimpleDateFormat() which is supported for API 24 only. My minimum API level is 19. Is there an equivalent function for it?

Java Code:

public void saveExam() {
        date = (DatePicker)findViewById(R.id.examDate);
        Integer day = date.getDayOfMonth();
        Integer month = date.getMonth();
        Integer year = date.getYear();

        time = (TimePicker)findViewById(R.id.examTime);
        Integer hour, minutes;
        if (Build.VERSION.SDK_INT >= 23 ) {
            hour = time.getHour();
            minutes = time.getMinute();
        } else {
            hour = time.getCurrentHour();
            minutes = time.getCurrentMinute();
        }

        StringBuilder temp = new StringBuilder();
        temp.append(year.toString()).append("-").append(month.toString()).append("-").append(day.toString())
                    .append(" ").append(hour).append(":").append(minutes);

        Toast.makeText(add_exam.this, temp, Toast.LENGTH_LONG).show();
    }

Could someone also please explain to me this part of the code, this is my first time seeing flags:

if (date != null) {
    long when = date.getTime();
    int flags = 0;
    flags |= android.text.format.DateUtils.FORMAT_SHOW_TIME;
    flags |= android.text.format.DateUtils.FORMAT_SHOW_DATE;
    flags |= android.text.format.DateUtils.FORMAT_ABBREV_MONTH;
    flags |= android.text.format.DateUtils.FORMAT_SHOW_YEAR;

    finalDateTime = android.text.format.DateUtils.formatDateTime(context,
        when + TimeZone.getDefault().getOffset(when), flags);

Flags are used for set up format output for time in ms you pass into DateUtils.formateDateTime method. They represent parts of bit mask. Here you can see different values of flags for formating date in DateUtils .

For example:

DateUtils.FORMAT_SHOW_TIME = 1; // 1 binary representation
DateUtils.FORMAT_SHOW_WEEKDAY = 2; // 10 binary representation
DateUtils.FORMAT_SHOW_YEAR = 4; // 100 binary representation
...

If you have 3 , in binary it will be 11 which means that DateUtils.FORMAT_SHOW_TIME and DateUtils.FORMAT_SHOW_WEEKDAY will be applied and you will have something like this result: "11:04pm, Saturday".

If you have 5 , in binary it will be 101 which means that DateUtils.FORMAT_SHOW_TIME and DateUtils.FORMAT_SHOW_YEAR will be applied. But instead of something like "11:04pm, 2016" you will have "11:04pm". So, as I understand, there are some collisions in combining flags.

Basically, there are next examples of combining flags and corresponding outputs:

DateUtils.formatDateTime(this, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME) = "10:58pm"
DateUtils.formatDateTime(this, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_DATE) = "September 17"
DateUtils.formatDateTime(this, System.currentTimeMillis(), DateUtils.FORMAT_ABBREV_MONTH) = "Sep 17"
DateUtils.formatDateTime(this, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_YEAR) = "September 17, 2016"
DateUtils.formatDateTime(this, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE) = "September 17, 2016"
DateUtils.formatDateTime(this, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_ABBREV_MONTH) = "Sep 17, 2016"
DateUtils.formatDateTime(this, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_ABBREV_MONTH | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_TIME) = "11:00pm, Sep 17, 2016"
DateUtils.formatDateTime(this, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_MONTH) = "Sep 17"
DateUtils.formatDateTime(this, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_YEAR) = "11:03pm"
DateUtils.formatDateTime(this, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_WEEKDAY) = "11:04pm, Saturday"
DateUtils.formatDateTime(this, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE) = "11:04pm, September 17"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM