是否可以解析日期這樣的在Java中:是否可以解析這些日期
Sunday February 09th, 2014 or Sunday February 21st, 2014.
我搜索與SimpleDateFormat的一種方式。由於
是否可以解析日期這樣的在Java中:是否可以解析這些日期
Sunday February 09th, 2014 or Sunday February 21st, 2014.
我搜索與SimpleDateFormat的一種方式。由於
您可以使用SimpleDateFormat的,請嘗試面膜:
"E, dd MMMMM yyyy"
如:星期天2014年2月9日
存在內的SimpleDateFormat沒有辦法添加日,RD等..你會必須手動做到這一點:
// http://code.google.com/p/guava-libraries
import static com.google.common.base.Preconditions.*;
String getDayOfMonthSuffix(final int n) {
checkArgument(n >= 1 && n <= 31, "illegal day of month: " + n);
if (n >= 11 && n <= 13) {
return "th";
}
switch (n % 10) {
case 1: return "st";
case 2: return "nd";
case 3: return "rd";
default: return "th";
}
}
SimpleDateFormat formatter = new SimpleDateFormat("E, MMM dd yyyy");
String dateInString = "Fri, June 7 2013";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
嘗試這樣的事情。參考號:http://www.mkyong.com/java/how-to-convert-string-to-date-java/
OP要求的數字是什麼? – PeterMmm
SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, "PST");
pdt.setStartRule(DateFields.APRIL, 1, DateFields.SUNDAY, 2*60*60*1000);
pdt.setEndRule(DateFields.OCTOBER, -1, DateFields.SUNDAY, 2*60*60*1000);
// Format the current time.
SimpleDateFormat formatter
= new SimpleDateFormat ("yyyy.mm.dd e 'at' hh:mm:ss a zzz");
Date currentTime_1 = new Date();
String dateString = formatter.format(currentTime_1);
// Parse the previous string back into a Date.
ParsePosition pos = new ParsePosition(0);
Date currentTime_2 = formatter.parse(dateString, pos);
請參閱本文了解更多信息; http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
我的DateFields出現錯誤。我應該輸入什麼? – Hunsu
該代碼只是在使用模式EEE MMM dd, yyyy
解析字符串之前,使用帶有逆向視圖的正則表達式替換所有匹配的nd|rd|th|st
。
String value = "Sunday February 09th, 2014";
String formatted = value.replaceAll("(?<=\\d)(nd|rd|th|st)", "");
System.out.println(formatted);
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd, yyyy");
Date date = format.parse(formatted);
您是否嘗試過'E \t星期和'M \t月? – Smutje
Propbaly你應該在使用DateFormat.parse()之前用正則表達式去除序號。 – PeterMmm
這就是我所做的。我只想知道是否有SimpleDateFormat的方法。 – Hunsu