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!
LocalTime format () method in Java with Examples. The format () method of a LocalTime class is used to format this time using the specified formatter passed as a parameter. This method formats this time based on passed formatter to a string.
LocalDate format () method in Java. The format () method of LocalDate class in Java method formats this date using the specified formatter. Parameter: This method accepts a parameter obj which specifies the formatter to be used and it is not null.
Using Java 8 With the release of the new time API in Java 8, there is an easier way to get a date without the time. One of the new features that this new API has brought is that there are several classes to work with dates with or without time, or even to work only with time.
Following is the declaration for java.time.LocalDate.format (DateTimeFormatter formatter) method. formatter − the formatter to use, not null. the formatted date string, not null.
I needed to convert date to String removing year. String should keep locale settings. Date format is of type DateFormat.LONG
, not DateFormat.SHORT
. E.g., full string is September 18, 2012
, not 09/18/12
.
My solution (based on Alex post):
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.regex.Pattern;
public class Test {
private static void testLocaleDates() {
String fmtString = "%-20s %-25s %-25s %-20s %-20s";
System.out.println(String.format(fmtString, "Locale", "Complete date", "Pattern", "Yearless date", "Yearless pattern"));
Pattern regExpPattern = Pattern.compile("Sweden|Spain|Russia|Ukraine|States|France|German|Japan|China", Pattern.CASE_INSENSITIVE);
for (Locale locale : Locale.getAvailableLocales()) {
boolean isPrint = regExpPattern.matcher(locale.getDisplayCountry()).find();
if (!isPrint)
continue;
Date date = new Date();
String dateTxt = DateFormat.getDateInstance(DateFormat.LONG, locale).format(date);
SimpleDateFormat sdf = (SimpleDateFormat) SimpleDateFormat.getDateInstance(DateFormat.LONG, locale);
String pattern = sdf.toPattern();
// Checking 'de' we omit problems with Spain locale
String regExpPatternTxt = pattern.contains("de") ? "[^Mm]*[Yy]+[^Mm]*" : "[^DdMm]*[Yy]+[^DdMm]*";
String yearlessPattern = pattern.replaceAll(regExpPatternTxt, "");
SimpleDateFormat yearlessSDF = new SimpleDateFormat(yearlessPattern, locale);
System.out.println(String.format(fmtString, locale.getDisplayCountry(), dateTxt, pattern, yearlessSDF.format(date), yearlessPattern));
}
}
public static void main(String[] args) {
testLocaleDates();
}
}
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
You can use SimpleDateFormat
:
Date date = new Date();
java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("MM/dd");
String dateString = df.format(date);
Output:
03/15
EDIT:
After researching locale formats further, and expanding on Peters answer, here's some code to demonstrator differences between toPattern()
and toLocalizedPattern()
:
import java.text.*
import java.util.*
ArrayList<Locale> locales = new ArrayList<Locale>();
locales.add(Locale.US);
locales.add(Locale.UK);
locales.add(Locale.GERMANY);
locales.add(Locale.CHINA);
Date date = new Date();
for(Locale l : locales)
{
SimpleDateFormat sdf = (SimpleDateFormat) SimpleDateFormat.getDateInstance(DateFormat.SHORT, l);
String pattern = sdf.toPattern();
String localizedPattern = sdf.toLocalizedPattern()
println "country: " + l.getDisplayName();
println "pattern: " + pattern;
println "localizedPattern: " + localizedPattern;
try {
SimpleDateFormat temp = new SimpleDateFormat(localizedPattern, l);
println "localized pattern re-parsed successfully"
} catch (IllegalArgumentException e) {
println "localized pattern re-parsed unsuccessfully: " + e.getMessage();
}
SimpleDateFormat df = new SimpleDateFormat(pattern, l);
String dateString = df.format(date);
println "resulting date: " + dateString
String yearlessPattern = pattern.replaceAll("\\W?[Yy]+\\W?", "");
println "yearlessPattern = " + yearlessPattern;
SimpleDateFormat yearlessSDF = new SimpleDateFormat(yearlessPattern, l);
println "resulting date without year: " + yearlessSDF.format(date) + "\n";
}
Produces following output:
country: English (United States)
pattern: M/d/yy
localizedPattern: M/d/yy
localized pattern re-parsed successfully
resulting date: 3/15/12
yearlessPattern = M/d
resulting date without year: 3/15
country: English (United Kingdom)
pattern: dd/MM/yy
localizedPattern: dd/MM/yy
localized pattern re-parsed successfully
resulting date: 15/03/12
yearlessPattern = dd/MM
resulting date without year: 15/03
country: German (Germany)
pattern: dd.MM.yy
localizedPattern: tt.MM.uu
localized pattern re-parsed unsuccessfully: Illegal pattern character 't'
resulting date: 15.03.12
yearlessPattern = dd.MM
resulting date without year: 15.03
country: Chinese (China)
pattern: yy-M-d
localizedPattern: aa-n-j
localized pattern re-parsed unsuccessfully: Illegal pattern character 'n'
resulting date: 12-3-15
yearlessPattern = M-d
resulting date without year: 3-15
So in conclusion, to display a localized date without a year:
String yearlessPattern = DateFormat.getDateInstance(DateFormat.SHORT).toPattern().replaceAll("\\W?[Yy]+\\W?", "");
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