java本地timeformat没有年份

我喜欢将本地时间格式格式化为没有年份的字符串。 目前我能够显示包含年份的本地格式:

java.text.DateFormat df = java.text.DateFormat.getDateInstance(java.text.DateFormat.SHORT); String dateString = df.format(date); 

因此我收到一个时间字符串输出,如

 12.03.2012 03/12/2012 

适用于不同的国家。 现在我想得到一个简短的表格

 12.03. 03/12 

我该怎么做?

谢谢你的帮助!

您可以使用SimpleDateFormat

 Date date = new Date(); java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("MM/dd"); String dateString = df.format(date); 

输出:

 03/15 

编辑:

在进一步研究语言环境格式并扩展Peters答案后,这里有一些代码来说明toPattern()toLocalizedPattern()之间的toPattern()

 import java.text.* import java.util.* ArrayList locales = new ArrayList(); 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"; } 

产生以下输出:

 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-Md localizedPattern: aa-nj localized pattern re-parsed unsuccessfully: Illegal pattern character 'n' resulting date: 12-3-15 yearlessPattern = Md resulting date without year: 3-15 

总而言之,要显示没有一年的本地化日期:

String yearlessPattern = DateFormat.getDateInstance(DateFormat.SHORT).toPattern().replaceAll("\\W?[Yy]+\\W?", "");

我需要将日期转换为String删除年份。 字符串应保持区域设置。 日期格式的类型为DateFormat.LONG ,而不是DateFormat.SHORT 。 例如,完整字符串是September 18, 2012 ,而不是09/18/12

我的解决方案(基于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(); } } 

节目输出:

区域设置 完成日期 模式无 年历日期无 年级模式
日本 2012/09/18 yyyy / MM / dd 09/18 MM / dd
日本 H24.09.18 Gy.MM.dd 09.18 MM.dd
美国 2012年9月18日 MMMM d,yyyy 9月18日 MMMM d
西class牙 18 de septiembre de 2012 d' de'MMMM'de'yyyy 18 de septiembre d' de'MMMM
美国 18 de septiembre de 2012 d' de'MMMM'de'yyyy 18 de septiembre d' de'MMMM
乌克兰 2012年12月 MMMM yyyy 18周年纪念日MM MMMM MMY yyyy
西class牙 18 / setembre / 2012 d'/'MMMM'/'yyyy 18 / setembre d'/'MMMM
俄罗斯 18Сентябрь2012г。 d MMMMyyyy'г。' 18СентябрьdMMMM
中国 2012年9月18日 yyyy'年'M'月'日' 9月18日 M'月'''日'
法国 18 septembre 2012 d MMMM yyyy 18 septembre d MMMM
德国 18. 2012年9月 d。 MMMM yyyy 18.九月 d。 MMMM
瑞典 邮报2012年9月18日 'den'd MMMM yyyy den 18九月 'den'd MMMM

没有预定义的格式来实现这一目标。 这是一个解决方法:使用java.text.DateFormat.SHORT格式化日期,并使用格式为yyyy的自定义格式化程序。 现在为后者搜索前者的结果。

如果年份接近开头,则删除年后的下一个非数字,否则在年份之前删除非数字。

但这也不完美,因为它给你“12.3”而不是“12.3”。 对于德国人

如果你真的需要正确,请查看Java源代码,特别是rt.jar中的rt.jar包并打印所有语言环境日期(您只需为所有语言环境创建一个类型为SHORTDateFormat )。

这应该为您提供创建自己的资源包所需的所有数据,其中包含所有语言的日/月格式字符串。

您可以提取模式并删除年份。

 SimpleDateFormat df = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.SHORT); String pattern = df.toLocalizedPattern().replaceAll(".?[Yy].?", ""); System.out.println(pattern); SimpleDateFormat mdf = new SimpleDateFormat(pattern); 

版画

 M/d 

在我的系统上。

其他作者已经正确地认识到Java平台没有管理一个月 – 日组合的预定义本地化格式模式,因此建议使用数据分析和正则表达式模式等复杂的解决方法。

我已根据最新的CLDR数据将这些格式模式嵌入到我的库Time4J中 。 例:

 AnnualDate now = SystemClock.inLocalView().now(AnnualDate.chronology()); System.out.println("xml-schema: " + now); // --09-23 ChronoFormatter fEnglishUS = ChronoFormatter.ofStyle(DisplayMode.SHORT, Locale.US, AnnualDate.chronology()); ChronoFormatter fFrench = ChronoFormatter.ofStyle(DisplayMode.SHORT, Locale.FRENCH, AnnualDate.chronology()); ChronoFormatter fGerman = ChronoFormatter.ofStyle(DisplayMode.SHORT, Locale.GERMAN, AnnualDate.chronology()); System.out.println("us: " + fEnglishUS.format(now)); // 9/23 System.out.println("french: " + fFrench.format(now)); // 23/09 System.out.println("german: " + fGerman.format(now)); // 23.9. 

很高兴知道: ICU4J确实具有这种function,但具有更复杂的API。

您可能已经意识到,短格式不仅取决于语言,还取决于国家。 不幸的是,Java中没有内置的模式。 相反,您可能需要使用CLDR定义的那些 – 搜索“模式”或“Md”(月/日模式)。

但是,我必须警告你,这些模式可能是错误的。 相反,您可能希望将它们移动到资源文件并让翻译人员处理它们(适当的评论它是什么以及如何处理它将是必需的。)

java.time.MonthDay

Java提供了一个类来表示没有任何年份的这样的值,月份和日期: java.time.MonthDay

 LocalDate ld = LocalDate.parse( "2012-12-03" ); // December 3rd. MonthDay md = MonthDay.from( ld ); 

ISO 8601

如果调用toString ,则会得到一个符合ISO 8601标准的字符串生成的字符串--MM-DD 。 这是扩展格式,标准还允许使用基本格式最小化分隔符的使用--MMDD 。 参考: 第5.2.1.3节截断的表示d) ISO 8601:2000标准的隐含年份中的特定日期

 String output = md.toString(); 

–12-03

自定义格式

我建议尽可能使用标准格式。 但是,如果必须使用其他格式,则可以使用DateTimeFormatter类进行指定。

 DateTimeFormatter f = DateTimeFormatter.ofPattern( "MM/dd" ); String output = md.format( f ); 

12/03

java.time类提供了仅 日期 值和日期时间值的自动本地化。 不幸的是,没有对月日价值的支持。 因此,您必须明确定义自己的自定义格式以进行所需的本地化。 有关替代方案,请参阅Meno Hochschild的答案 。