0

I am trying to format a string into specific date format but there is some problem in conversion. In converted string month is always coming "January". Following is the code snippet:

    String date = "30/10/2012-11:11:11";
    DateFormat dateForm = new SimpleDateFormat("dd/mm/yyyy-hh:mm:ss");

    try {
       System.out.println(dateForm.parse(date));
    } 
    catch (ParseException e) {
       e.printStackTrace();
    }

I am not able to get the reason behind this. Please suggest.

Infotechie
  • 1,653
  • 6
  • 23
  • 35
  • Replace `dd/mm/yyyy` with `dd/MM/yyyy` and [Never use SimpleDateFormat or DateTimeFormatter without a Locale](https://stackoverflow.com/a/65544056/10819573). – Arvind Kumar Avinash Jul 11 '21 at 10:22

3 Answers3

3

As others have said, you should use MM for months - and I strongly suspect you always want to use HH for hours, instead of hh. HH is the 24 hour clock; hh is the 12 hour clock, usually used in conjunction with an AM/PM designator.

Finally, you should consider what time zone and locale you want your SimpleDateFormat to use. Even in this case where there aren't any month or day names, I'd still specify the locale explicitly, to make it clear that you're not trying to use any localized data.

SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy-HH:mm:ss", Locale.US);
// TODO: Set the time zone appropriately
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

Use MM instead of mm for the date portion of your format string. mm is for minutes, not months.

Reference: http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

Isaac
  • 16,458
  • 5
  • 57
  • 81
1

Please use "dd/MM/yyyy-hh:mm:ss"

vishal_aim
  • 7,636
  • 1
  • 20
  • 23