解析する日付の形式がいくつか異なりますが、で認識できませんSimpleDateFormat
。誰かがこの日付のパターンを見つけるのを手伝ってくれますか?
- 1892年7月6日
- 1915年5月9日
- 335年2月
- 1768-02-12
と
- 紀元前63年9月23日
- 8月19日広告14
ありがとう
複数の形式を使用して解析する必要がある場合は、通常、Jodaの方が適しています。例えば、
private static DateTimeFormatter dateFormatter;
String[] validDateFormats = new String[] { "dd-MMM-yyyy HH:mm:ss",
"yyyy-MM-dd HH:mm:ss.SSS" };
DateTimeParser[] parsers = new DateTimeParser[validDateFormats.length];
for (int i = 0; i < validDateFormats.length; ++i) {
parsers[i] = DateTimeFormat.forPattern(validDateFormats[i])
.getParser();
}
dateFormatter = new DateTimeFormatterBuilder().append(null, parsers)
.toFormatter().withZone(DateTimeZone.UTC);
これで、入力がいずれかの形式に一致する場合、このdateFormatterは正しく解析されます。
inputDate = dateFormatter.parseDateTime(dateStr);
DateTimeFormatter outputFormat = DateTimeFormat
.forPattern("yyyy/MM/dd");
String outputString = inputDate.toString(outputFormat);
フォーマット文字列はここから検索できます:http://joda-time.sourceforge.net/api-release/org/joda/time/format/DateTimeFormat.html
日付を解析して最初の一致を見つけるパターンを実行する可能性を提供するだけです。
ここでは、想像できるすべての日付形式を作成するための優れたジェネレーターを見つけることができます http://www.fileformat.info/tip/java/simpledateformat.htm
たとえば、最初の両方の例はdd MMMM yyyyで生成されます;)
これらを試してください
2012年5月27日:
System.out.println(new SimpleDateFormat("d-MMM-YYYY").format(new Date()));
2012年5月27日:
System.out.println(new SimpleDateFormat("d-MM-YYYY").format(new Date()));
27-05-12:
System.out.println(new SimpleDateFormat("dd-MM-YY").format(new Date()));