Java string to convert date and time

I do not see a problem with the example below. For some reason, it seems to ignore the year and say that the dates are the same, as seen in the output below. I have to miss something simple.

01/28/2006
 01/16/2007
 Tue Apr 01 00:00:00 PDT 2008
 Tue Apr 01 00:00:00 PDT 2008
 done

import java.util.*;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

    class ExampleProgram {
      public static void main(String[] args){
        DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
        String d1String = "01/28/2006";
        String d2String = "01/16/2007";
        System.out.println(d1String);
        System.out.println(d2String);
        Date d1=null;
        try {
            d1 = df.parse(d1String);
        } catch (ParseException e) {                
            System.out.println(e.getMessage());
        }           
        Date d2=null;
        try {
            d2 = df.parse(d2String);
        } catch (ParseException e) {                
            System.out.println(e.getMessage());
        }
        System.out.println(d1);
        System.out.println(d2);
        System.out.println("done");
      }
    }
+3
source share
4 answers
"dd/MM/yyyy"

should read:

"MM/dd/yyyy"
+6
source

as Peter mentioned, the meaning of the letters can be found in the documentation: http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html

+2
source

, , , , , SimpleDateFormat "MM/dd/yyyy"

, , , , , "dd/MM/yyyy", d1String - 28. 28 - 12, , 16 - 12, , - 4 (), - 2008 . d2String.

+1
0

Source: https://habr.com/ru/post/1706368/


All Articles