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
For android.text.format.DateFormat you designate Hour in day as kk
like this:
String dateAndroid = android.text.format.DateFormat.format(
"dd-MM-yyyy kk:mm:ss", calendar).toString();
For java.text.SimpleDateFormat you designate hour in day as HH
.
H hour in day (0-23)
Documentation for android.text.format.DateFormat:
public static final char HOUR_OF_DAY
This designator indicates the hour of the day in 24 hour format. Example for 3pm: k -> 15 Examples for midnight: k -> 0 kk -> 00
Because ... it's not the same thing and it's behaving as the documentation states?
From the Documentation for android.text.format.DateFormat
This class only supports a subset of the full Unicode specification. Use SimpleDateFormat if you need more.
But if you read the docs further:
public static final char HOUR_OF_DAY
This designator indicates the hour of the day in 24 hour format. Example for 3pm: k -> 15 Examples for midnight: k -> 0 kk -> 00
So ... using that class, it'd be kk
instead of HH
I understand you have accepted an answer already but just to explain this fully to you...
From the source code for DateFormat.java...
The
format
methods in this class implement a subset of Unicode UTS #35 patterns. The subset currently supported by this class includes the following format characters:acdEHhLKkLMmsyz
. Up to API level 17, onlyadEhkMmszy
were supported. Note that this class incorrectly implementsk
as if it wereH
for backwards compatibility.
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...
/**
* @deprecated Use a literal {@code 'H'} (for compatibility with {@link SimpleDateFormat}
* and Unicode) or {@code 'k'} (for compatibility with Android releases up to and including
* Jelly Bean MR-1) instead. Note that the two are incompatible.
*/
@Deprecated
public static final char HOUR_OF_DAY = 'k';
...and later during character replacement...
case 'H': // hour in day (0-23)
case 'k': // hour in day (1-24) [but see note below]
{
int hour = inDate.get(Calendar.HOUR_OF_DAY);
// Historically on Android 'k' was interpreted as 'H', which wasn't
// implemented, so pretty much all callers that want to format 24-hour
// times are abusing 'k'. http://b/8359981.
if (false && c == 'k' && hour == 0) {
hour = 24;
}
replacement = zeroPad(hour, count);
}
break;
I have never programmed for Android. I googled the DateFormat javadoc and saw there the following examples:
Examples for April 6, 1970 at 3:23am:
"MM/dd/yy h:mmaa" -> "04/06/70 3:23am"
"MMM dd, yyyy h:mmaa" -> "Apr 6, 1970 3:23am"
"MMMM dd, yyyy h:mmaa" -> "April 6, 1970 3:23am"
"E, MMMM dd, yyyy h:mmaa" -> "Mon, April 6, 1970 3:23am&
"EEEE, MMMM dd, yyyy h:mmaa" -> "Monday, April 6, 1970 3:23am"
"'Noteworthy day: 'M/d/yy" -> "Noteworthy day: 4/6/70"
The "hour" is marked using small letter h
in opposite to SimpleDateFormat
where capital letter is used for this purpose.
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";
public static String getDateAsString(Date date, String pattern) {
Locale locale = new Locale("EN");
SimpleDateFormat sdf = null;
try {
sdf = new SimpleDateFormat(pattern, locale);
return sdf.format(date);
} catch (IllegalArgumentException ex) {
// default format
sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm", locale);
return sdf.format(date);
}
}