I like to format the local timeformat into a string without the year. At the moment I am able to show the local format containing the year:
java.text.DateFormat df = java.text.DateFormat.getDateInstance(java.text.DateFormat.SHORT);
String dateString = df.format(date);
therefore i receive an time string output like
12.03.2012
03/12/2012
for the different countries. Now i like to get a short form like
12.03.
03/12
how would i do this?
thanks for your help!
You can use
SimpleDateFormat
:Output:
EDIT:
After researching locale formats further, and expanding on Peters answer, here's some code to demonstrator differences between
toPattern()
andtoLocalizedPattern()
:Produces following output:
So in conclusion, to display a localized date without a year:
String yearlessPattern = DateFormat.getDateInstance(DateFormat.SHORT).toPattern().replaceAll("\\W?[Yy]+\\W?", "");
java.time.MonthDay
Java offers a class to represent such a value, a month and a day-of-month without any year:
java.time.MonthDay
ISO 8601
If you call
toString
, you get a string generated in the format following the ISO 8601 standard,--MM-DD
. That is the Extended format, and the standard also allows for the Basic format minimizing the use of separators,--MMDD
. Reference: Section 5.2.1.3 Truncated representations d) A specific day of a month in the implied year of the ISO 8601:2000 standard.Custom formats
I suggest using the standard formats whenever possible. But if you must use other formats, you can specify with the
DateTimeFormatter
class.The java.time classes provide for automatic localization of date-only values and date-time values. Unfortunately, no such support for month-day values. So you will have to define your own custom format explicitly for your desired localization. For alternatives, see the Answer by Meno Hochschild.
You can extract the pattern and remove the year.
prints
on my system.
Other authors have correctly recognized that the Java platform does not manage predefined localized format patterns for a month-day-combination without year so complex workarounds like using data analysis and regexp-patterns were suggested.
I have embedded such format patterns into my library Time4J based on the newest CLDR-data. Example:
Good to know: ICU4J does have this capability, too, but with a far more complex API.
There is no predefined format to achieve this. Here is a workaround: Format the date with
java.text.DateFormat.SHORT
and with a custom formatter with the formatyyyy
. Now search the result of the former for the latter.If the year is near the beginning, remove the next non-digit after the year, otherwise strip non-digits before the year.
But that's not perfect either, because it gives you "12.3" instead of "12.3." for German.
If you really need to get it right, look into the Java source code, specifically the package
sun.text.resources
inrt.jar
and print all the locale dates (you can simply create aDateFormat
with typeSHORT
for all locales).That should give you all the data you need to create your own resource bundle with day/month format strings for all languages.
I needed to convert date to String removing year. String should keep locale settings. Date format is of type
DateFormat.LONG
, notDateFormat.SHORT
. E.g., full string isSeptember 18, 2012
, not09/18/12
.My solution (based on Alex post):
Program output:
Locale Complete date Pattern Yearless date Yearless pattern
Japan 2012/09/18 yyyy/MM/dd 09/18 MM/dd
Japan H24.09.18 Gy.MM.dd 09.18 MM.dd
United States September 18, 2012 MMMM d, yyyy September 18 MMMM d
Spain 18 de septiembre de 2012d' de 'MMMM' de 'yyyy 18 de septiembre d' de 'MMMM
United States 18 de septiembre de 2012d' de 'MMMM' de 'yyyy 18 de septiembre d' de 'MMMM
Ukraine 18 вересня 2012 d MMMM yyyy 18 вересня d MMMM
Spain 18 / setembre / 2012 d' / 'MMMM' / 'yyyy 18 / setembre d' / 'MMMM
Russia 18 Сентябрь 2012 г. d MMMM yyyy 'г.' 18 Сентябрь d MMMM
China 2012年9月18日 yyyy'年'M'月'd'日' 9月18日 M'月'd'日'
France 18 septembre 2012 d MMMM yyyy 18 septembre d MMMM
Germany 18. September 2012 d. MMMM yyyy 18. September d. MMMM
Sweden den 18 september 2012 'den 'd MMMM yyyy den 18 september 'den 'd MMMM