SimpleDateFormat可以根据字符串格式化日期吗?

时间:2012-04-25 19:37:44

标签: android simpledateformat

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

2 个答案:

答案 0 :(得分:1)

检查这一个,它是非常有用的库,易于使用&为这些需求而扩展 https://bitbucket.org/dfa/strtotime/wiki/Home

答案 1 :(得分: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));

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