格式日期没有年份 [英] Format date without year
本文介绍了格式日期没有年份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何创建某个日期的文本表示,将区域设置纳入考量,并且仅包含日期和月份(无年)?
How can create text representation of some date, that takes locale into account and contains only day and month (no year)?
以下代码给了我一些像 23/09/2010
Following code gives me something like 23/09/2010
DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault()).format(date);
我想获得 23/09
推荐答案
您可以使用正则表达式来修剪所有 y
和任何非字母字符,之后,如果有的话。这是一个启动示例:
You could use regex to trim off all y
's and any non-alphabetic characters before and after, if any. Here's a kickoff example:
public static void main(String[] args) throws Exception {
for (Locale locale : Locale.getAvailableLocales()) {
DateFormat df = getShortDateInstanceWithoutYears(locale);
System.out.println(locale + ": " + df.format(new Date()));
}
}
public static DateFormat getShortDateInstanceWithoutYears(Locale locale) {
SimpleDateFormat sdf = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.SHORT, locale);
sdf.applyPattern(sdf.toPattern().replaceAll("[^\\p{Alpha}]*y+[^\\p{Alpha}]*", ""));
return sdf;
}
您会看到此代码段也测试所有区域设置。这里的所有地区都可以正常工作。
You see that this snippet tests it for all locales as well. It looks to work fine for all locales here.
这篇关于格式日期没有年份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文