I want to format a date string to not have a year (Ex: "1/4").
int flags = DateUtils.FORMAT_NUMERIC_DATE | DateUtils.FORMAT_NO_YEAR;
The above flags still append a year to the date (Ex: "1/4/2016"). How do I drop the year?
The international standard recommends writing the date as year, then month, then the day: YYYY-MM-DD.
Creating A Simple Date FormatString pattern = "yyyy-MM-dd" ; SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); The specified parameter “pattern” is the pattern used for formatting and parsing dates.
The United States is one of the few countries that use “mm-dd-yyyy” as their date format–which is very very unique! The day is written first and the year last in most countries (dd-mm-yyyy) and some nations, such as Iran, Korea, and China, write the year first and the day last (yyyy-mm-dd).
util. Date in yyyy-mm-dd format // Converting it into String using formatter String strDate = sm.
It seems that date formatting changed after 4.4 Android version:
For 4.1 Android version DateUtils.formatDateTime goes up to DateUtils.formatDateRange where the string is formatted using Formatter.
But from 4.4 Android version DateUtils.formatDateRange uses libcore.icu.DateIntervalFormat
to format a String.
public static Formatter formatDateRange(Context context, Formatter formatter, long startMillis,
long endMillis, int flags, String timeZone) {
// If we're being asked to format a time without being explicitly told whether to use
// the 12- or 24-hour clock, icu4c will fall back to the locale's preferred 12/24 format,
// but we want to fall back to the user's preference.
if ((flags & (FORMAT_SHOW_TIME | FORMAT_12HOUR | FORMAT_24HOUR)) == FORMAT_SHOW_TIME) {
flags |= DateFormat.is24HourFormat(context) ? FORMAT_24HOUR : FORMAT_12HOUR;
}
String range = DateIntervalFormat.formatDateRange(startMillis, endMillis, flags, timeZone);
try {
formatter.out().append(range);
} catch (IOException impossible) {
throw new AssertionError(impossible);
}
return formatter;
}
So you have to trim a resulting String or use DateFormat/SimpleDateFormat to remove year field on 4.1 Android.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With