1

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);
Community
  • 1
  • 1
nanjero echizen
  • 241
  • 1
  • 5
  • 14
  • `SimpleDateFormat` is a basic Java class that predates Android: http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html You can use it on any version. And for the flags, look for tutorials/resources on bit masking to learn how that works. – nasch Sep 17 '16 at 22:56

1 Answers1

0

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"
Michael Spitsin
  • 2,539
  • 2
  • 19
  • 29
  • Am I understanding this right? The formatDateTime function is passed a millisecond value. which is then converted into binary and placed against the flags? so with my code in reference if I pass a value 15 which is 1111 in binary it will apply all the 4 flags? – nanjero echizen Sep 17 '16 at 23:28
  • No. Basically (except Context) you have 2 parameters. First one is Time, which represents by milliseconds. Seconds is formatting flags. These flags used by internal formatters (I guess DateInternalFormatter) to define formatting template for your time parameter. Then this template puts in special internal cache for native computations and then somewhere deep in code `formateDateInterval` is called which passes 2 dates in ms as params and id of template. In your case both dates will be same. Then in native code ms transforms to pretty String and returns to you :) – Michael Spitsin Sep 18 '16 at 08:31
  • You can read more in this source: http://grepcode.com/file/repo1.maven.org/maven2/org.robolectric/android-all/5.0.0_r2-robolectric-1/libcore/icu/DateIntervalFormat.java#DateIntervalFormat.formatDateRange%28long%2Clong%2Cint%2Cjava.lang.String%29 – Michael Spitsin Sep 18 '16 at 08:31