-2

I am getting date as String MMMM yyyy (e.g. August 2014). Now i need this to be converted into the last date of the month (2014-08-30). Any guidance would help me. I am doing like dis:

 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
user1782009
  • 221
  • 3
  • 13
  • 32
  • 1
    A programming language might help... if you know the programming language, what did the docs say? What problems are you having implementing what's in the docs? Any error messages? – Ben Aug 09 '14 at 09:59
  • How will you identify the date? The present format doesnot provide the date information! – Rahul Tripathi Aug 09 '14 at 10:04
  • I am doing like dis: 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 – user1782009 Aug 09 '14 at 10:05
  • Similar questions have been asked many times here. – Raedwald Aug 09 '14 at 10:14
  • So you want to parse it, add a month and subtract a day. Try Calendar up to Java 7 and LocalDate in Java 8 http://www.threeten.org/articles/local-date.html – Peter Lawrey Aug 09 '14 at 10:45
  • Question already answered [here](http://stackoverflow.com/questions/18480633/java-util-date-format-conversion-yyyy-mm-dd-to-mm-dd-yyyy) and [here](http://stackoverflow.com/questions/3469507/how-can-i-change-the-date-format-in-java). – Varpie Aug 09 '14 at 10:46
  • A java.util.Date *has no format*. Its `toString` method generates a String representation of the date-time value in a certain (inconvenient, poorly chosen) format **and** applies your JVM’s current default time zone. Tip: [Joda-Time](http://www.joda.org/joda-time/) – Basil Bourque Aug 09 '14 at 17:04

1 Answers1

0

Try something like this

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));
SparkOn
  • 8,193
  • 3
  • 23
  • 28
  • Thanks a lot...but the output is coming in string..i want it to convert it into Date type. I am doing like this to do so: String finalDate = properFormattedDate+"-"+String.valueOf(noOfDays); System.out.println(finalDate); Date dt = simpleDateFormat.parse(finalDate); System.out.println(dt); but this is again giving me as Sat Feb 01 00:00:00 PST 2014 – user1782009 Aug 09 '14 at 13:06
  • understand date object in java it will always be in that format – SparkOn Aug 09 '14 at 13:12
  • then is there no way i can get '2014-03-30' in DATE type?? – user1782009 Aug 09 '14 at 13:27
  • I don't think it is possible check this answer over [here](http://stackoverflow.com/questions/12575990/calendar-date-to-yyyy-mm-dd-format-in-java) – SparkOn Aug 09 '14 at 13:33