2

I am using following code to convert date value to string.

SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
    String dateInString = "27-Dec-2014";

    try {

        Date date = formatter.parse(dateInString);
        System.out.println(date);
        System.out.println(formatter.format(date));

        String dateString = null;
        SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd hh:mm:ss");
        dateString = sdf.format(date);
        System.out.println("Date generated by getFormattedDate "+dateString);


    } catch (ParseException e) {
        e.printStackTrace();
    }

If we use any date before 28th December it works fine. It produces following output.

   Sat Dec 27 00:00:00 IST 2014
27-Dec-2014
Date generated by getFormattedDate 2014-12-27 12:00:00

If we use date as 28th December then it generates date for next year.

String dateInString = "28-Dec-2014";

It produces following output.

    Sun Dec 28 00:00:00 IST 2014
28-Dec-2014
Date generated by getFormattedDate 2015-12-28 12:00:00

Can anyone suggest why it is returning next year date for date which is greater than 27 december?

ParkerHalo
  • 4,341
  • 9
  • 29
  • 51
Chetan Shirke
  • 896
  • 4
  • 13
  • 35

0 Answers0