SimpleDateFormat sdf = new SimpleDateFormat("ddMMM", Locale.ENGLISH);
try {
sdf.parse(sDate);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我的日期格式是 04AUG2011,我想要 20110804。那么我该怎么做呢?
最佳答案
使用以下格式:
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd", Locale.ENGLISH);
我认为你需要区分解析和输出:
SimpleDateFormat parseFormat = new SimpleDateFormat("MMMdd", Locale.ENGLISH);
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyyMMdd", Locale.ENGLISH);
String dateStr = "06Sep";
// parse with 06Sep format
Date din = parseFormat.parse(dateStr);
// output with 20101106 format
System.out.println(String.format("Output: %s", outputFormat.format(din)));
关于java - java中的日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5220967/