Ant*_*hea 7 java localization date-format
我喜欢将本地时间格式格式化为没有年份的字符串.目前我能够显示包含年份的本地格式:
java.text.DateFormat df = java.text.DateFormat.getDateInstance(java.text.DateFormat.SHORT);
String dateString = df.format(date);
Run Code Online (Sandbox Code Playgroud)
因此我收到一个时间字符串输出,如
12.03.2012
03/12/2012
Run Code Online (Sandbox Code Playgroud)
对于不同的国家.现在我想得到一个简短的表格
12.03.
03/12
Run Code Online (Sandbox Code Playgroud)
我该怎么做?
谢谢你的帮助!
Ale*_*lex 15
你可以使用SimpleDateFormat:
Date date = new Date();
java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("MM/dd");
String dateString = df.format(date);
Run Code Online (Sandbox Code Playgroud)
输出:
03/15
Run Code Online (Sandbox Code Playgroud)
编辑:
在进一步研究了语言环境格式并扩展Peters的答案之后,这里有一些代码来证明toPattern()和之间的差异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";
}
Run Code Online (Sandbox Code Playgroud)
产生以下输出:
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
Run Code Online (Sandbox Code Playgroud)
总而言之,要显示没有一年的本地化日期:
String yearlessPattern = DateFormat.getDateInstance(DateFormat.SHORT).toPattern().replaceAll("\\W?[Yy]+\\W?", "");
我需要将日期转换为String删除年份.字符串应保持区域设置.日期格式是类型DateFormat.LONG,而不是DateFormat.SHORT.例如,完整的字符串是September 18, 2012,而不是09/18/12.
我的解决方案(基于Alex帖子):
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();
}
}
Run Code Online (Sandbox Code Playgroud)
节目输出:
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
| 归档时间: |
|
| 查看次数: |
7053 次 |
| 最近记录: |