When I use the "HH" flag in android.text.format.DateFormat
, it is interpreted as a literal "HH". But when I use java.text.SimpleDateFormat
it is interpreted as the 2 digit Hour. Why are they different?
I'm not looking for a working alternative (I already know I have to use kk
instead of HH
). I'm just curious why "HH" isn't recognized.
Java example:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Calendar calendar = Calendar.getInstance();
String dateJava = new java.text.SimpleDateFormat(
"dd-MM-yyyy HH:mm:ss").format(calendar.getTime());
String dateAndroid = android.text.format.DateFormat.format(
"dd-MM-yyyy HH:mm:ss", calendar).toString();
TextView tvAndroid = (TextView) findViewById(R.id.tvAndroid);
TextView tvJava = (TextView) findViewById(R.id.tvJava);
tvAndroid.setText("Android: " + dateAndroid); //prints 26-05-2013 HH:36:34
tvJava.setText("Java: " + dateJava); //prints 26-05-2013 22:36:34
}
Output is:
Android: 26-05-2013 HH:36:34
Java: 26-05-2013 22:36:34
I expect both to be 26-05-2013 22:36:34
Does Android's DateFormat have a bug?
Java's SimpleDateFormat accepts these:
H Hour in day (0-23) Number 0
k Hour in day (1-24) Number 24
K Hour in am/pm (0-11) Number 0
h Hour in am/pm (1-12) Number 12
So it appears the Android developers decided to change the meaning of k
and in their DateFormat function it is equivalent to the SimpleDateFormat H
as they explicitly say in their documentation page:
This constant was deprecated in API level 18. Use a literal 'H' (for
compatibility with SimpleDateFormat and Unicode) or 'k' (for compatibility
with Android releases up to and including Jelly Bean MR-1) instead. Note
that the two are incompatible.
What is the reason for this?
This work for all Android 4.0+ and for two date time format.
Use java.text.SimpleDateFormat.
Work example:
24 hour format use this date pattern = "dd-MM-yyyy HH:mm";
12 hour format use this date pattern = "dd-MM-yyyy hh:mm a";
I have never programmed for Android. I googled the DateFormat javadoc and saw there the following examples:
The "hour" is marked using small letter
h
in opposite toSimpleDateFormat
where capital letter is used for this purpose.I understand you have accepted an answer already but just to explain this fully to you...
From the source code for DateFormat.java...
Note the part I have marked in bold.
The source I linked to has been updated to allow the use of H but it isn't on general release yet (API 17 is the current release of Android and doesn't support H).
Later in the source, at the stage of declaring the format character constants, there is this comment...
...and later during character replacement...
Because ... it's not the same thing and it's behaving as the documentation states?
From the Documentation for android.text.format.DateFormat
But if you read the docs further:
So ... using that class, it'd be
kk
instead ofHH
For android.text.format.DateFormat you designate Hour in day as
kk
like this:For java.text.SimpleDateFormat you designate hour in day as
HH
.Documentation for android.text.format.DateFormat: