Java 如何将日期的MM yyyy格式转换为yyyy MM DD日期格式

Java 如何将日期的MM yyyy格式转换为yyyy MM DD日期格式,java,datetime,Java,Datetime,我获取的日期为字符串MMMM yyyy(例如2014年8月)。现在我需要将其转换为当月的最后一天(2014-08-30)。任何指导都会帮助我。 我做的就像是: String birthDate = "MARCH 2014"; formatter = new SimpleDateFormat("MMMM yyyy"); birth = (Date) formatter.parse(birthDate); // birtDate is a string System.out.println(

我获取的日期为字符串MMMM yyyy(例如2014年8月)。现在我需要将其转换为当月的最后一天(2014-08-30)。任何指导都会帮助我。 我做的就像是:

 String birthDate = "MARCH 2014"; 
formatter = new SimpleDateFormat("MMMM yyyy"); 
birth = (Date) formatter.parse(birthDate); // birtDate is a string 
System.out.println("Default date format " + birth); 

//this gives me Default date format Sat Mar 01 00:00:00 PST 2014

试试这样的

SimpleDateFormat dateFormat = new SimpleDateFormat("MMMMM yyyy");
Date date = (dateFormat).parse("March 2014");
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int noOfDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
String properFormattedDate = simpleDateFormat.format(date);
System.out.println(properFormattedDate+"-"+String.valueOf(noOfDays));

编程语言可能有助于。。。如果你知道编程语言,文档会怎么说?您在实施文档中的内容时遇到了什么问题?有错误消息吗?您将如何识别日期?当前格式不提供日期信息!我喜欢dis:String birthDate=“2014年3月”;格式化程序=新的SimpleDataFormat(“MMMM yyyy”);出生=(日期)格式化程序。解析(出生日期);//birtDate是一个字符串系统.out.println(“默认日期格式”+birth)//这给了我默认的日期格式Sat Mar 01 00:00:00 PST 2014类似的问题在这里被问了很多次。所以你想解析它,加上一个月,减去一天。请尝试Java 7之前的日历和Java 8中的LocalDate,非常感谢……但是输出是字符串形式的……我希望它将其转换为日期类型。我是这样做的:String finalDate=properFormattedDate+“-”+String.valueOf(noOfDays);系统输出打印LN(最终日期);日期dt=SimpleDataFormat.parse(最终日期);系统输出打印ln(dt);但这又让我想起了Sat Feb 01 00:00:00 PST 2014understand date对象在java中的格式,它将始终是这种格式,那么我是否无法在日期类型中获得“2014-03-30”?我认为不可能仔细检查这个答案