如何在Java中将日期转换为dd.MM.yyyy格式?
如何将日期如何在Java中将日期转换为dd.MM.yyyy格式?,java,date,jdk6,Java,Date,Jdk6,如何将日期(2015年1月12日星期一00:00:00 IST)转换为MM.dd.yyyy或dd.MM.yyyy 我试着使用下面的方法 String dateStr = "Mon Jan 12 00:00:00 IST 2015"; DateFormat formatter = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy"); System.out.println(formatter.format(dateStr));
(2015年1月12日星期一00:00:00 IST)
转换为MM.dd.yyyy
或dd.MM.yyyy
我试着使用下面的方法
String dateStr = "Mon Jan 12 00:00:00 IST 2015";
DateFormat formatter = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
System.out.println(formatter.format(dateStr));
但是得到
Exception in thread "main" java.lang.IllegalArgumentException:
Cannot format given Object as a Date
您必须将字符串解析为Date,然后格式化该日期
String dateStr = "Mon Jan 12 00:00:00 IST 2015";
DateFormat formatter = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
DateFormat formatter1 = new SimpleDateFormat("dd.MM.yyyy");
System.out.println(formatter1.format(formatter.parse(dateStr)));
您必须将字符串解析为Date,然后格式化该日期
String dateStr = "Mon Jan 12 00:00:00 IST 2015";
DateFormat formatter = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
DateFormat formatter1 = new SimpleDateFormat("dd.MM.yyyy");
System.out.println(formatter1.format(formatter.parse(dateStr)));
首先,必须将字符串解析为日期:
DateFormat parser = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
Date date = parser.parse(dateStr);
然后您可以将其格式化:
DateFormat formatter = new SimpleDateFormat("dd.MM.yyy");
System.out.println(formatter.format(date));
首先,必须将字符串解析为日期:
DateFormat parser = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
Date date = parser.parse(dateStr);
然后您可以将其格式化:
DateFormat formatter = new SimpleDateFormat("dd.MM.yyy");
System.out.println(formatter.format(date));
首先,您必须将字符串解析为日期,然后才能对其进行格式化。首先,您必须将字符串解析为日期,然后才能对其进行格式化。如果日期是2015年1月32日星期一00:00:00 IST,它将转换为下个月的日期(在本例中为2015年2月1日),我如何防止这种情况发生。使用
setLenient(false)
要验证日期,请检查以了解更多信息。如果日期是2015年1月32日星期一00:00:00 IST,它将转换为下个月的日期(在本例中为2015年2月1日),如何防止此情况发生。使用setLenient(false)
验证日期,请检查以了解更多信息。