0

What are the factors that make a date parsing not parsing the month correctly?

I have this very basic code :

    DateFormat format =  new SimpleDateFormat("DD/MM/yyyy", Locale.FRENCH);
    Date date = format.parse(dateInterv);

dateInterv is a String with the value "14/06/2016" , and when I parse the date, date is in january. Even in debugger I can't see why it transforms 14/06/2016 into 14/01/2016.

3 Answers3

2

Your String must be:

  DateFormat format =  new SimpleDateFormat("dd/MM/yyyy", Locale.FRENCH);
    Date date = format.parse(dateInterv);

because dd is for day of month and DD is for day of year

Opiatefuchs
  • 9,800
  • 2
  • 36
  • 49
0

You can follow any of the following format from SimpleDateFormat.

    SimpleDateFormat format = new SimpleDateFormat();
    Date curDate = new Date();

    format = new SimpleDateFormat("yyyy/MM/dd");
    DateToStr = format.format(curDate);
    System.out.println(DateToStr);

    format = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");
    DateToStr = format.format(curDate);
    System.out.println(DateToStr);

    format = new SimpleDateFormat("dd MMMM yyyy zzzz", Locale.ENGLISH);
    DateToStr = format.format(curDate);
    System.out.println(DateToStr);

    format = new SimpleDateFormat("MMMM dd HH:mm:ss zzzz yyyy",
            Locale.ITALIAN);
    DateToStr = format.format(curDate);
    System.out.println(DateToStr);

    format = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z");
    DateToStr = format.format(curDate);
    System.out.println(DateToStr);
SkyWalker
  • 28,384
  • 14
  • 74
  • 132
0

I think you might wanna change it to this:

    SimpleDateFormat formatter =new SimpleDateFormat("dd/MM/yyyy", Locale.French);
    Date date = formatter.parse(dateInterv);
    String formattedDate = formatter.format(date);
pVas94
  • 31
  • 3