0

sdfDateTime.format(new Date(System.currentTimeMillis()));通过从系统中获取今天的日期,我知道 SimpleDateFormat 是如何工作的。SimpleDateFormat 可以通过抓取字符串来格式化日期吗?假设您有一个字符串:String dateStr = "04/05/2012";您将如何将其格式化为:“2012 年 4 月 5 日”?

4

2 回答 2

1

检查这个,它是一个非常有用的库,易于使用和扩展以满足此类需求 https://bitbucket.org/dfa/strtotime/wiki/Home

于 2012-04-25T19:46:15.423 回答
1
DateFormat inputFormat = new SimpleDateFormat("MM/dd/yyyy");
inputFormat.setLenient(false);
DateFormat outputFormat = new SimpleDateFormat("MMMM dd, yyyy");
outputFormat.setLenient(false);

String inputDateAsString = "04/05/2012";
Date inputDate = inputFormat.parse(inputDateAsString);
System.out.println(outputFormat.format(inputDate));

您不能只抓取任意字符串并弄清楚它的格式是什么。

于 2012-04-25T19:49:20.333 回答