I'm attempting to migrate code from Java 8 to the OpenJDK 11. All was going amazingly smoothly until I attempted to parse date strings. I have a large number of these date strings from the database (and no, I can't change the database) that parse fine in Java 7 but fail in Java 11.
Some notes:
- The locale is set to the same default value.
- The date was generated originally using the date formatter with LONG, LONG settings.
- I ran this on Java 8 to get the time in mills. Then on Java 11, I converted that time in mills to a date. That seemed to work fine.
- However, the system seemed to insert an "at" between the date and time.
- I attempted to add in the "at" text to the date and it still fails.
Can someone point me in the right direction? I need to eventually do date calculations on these strings.
Thanks!
Bruce
package utility;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.Iterator;
import java.util.Locale;
public class OpenJDK11DateDisaster {
    public static void main(String args[])
    {
        /*
         * Example of the date string that works with Java 8 but fails to
         * parse in OpenJDK 11.
         * 
         * Note:  The Locale is identical between the two versions (US, English)
         * 
         */
        
        /* Java 8 Output...
        Java Version = 1.8.0_265
        Broken Date = September 1, 2015 3:13:29 AM MDT
        Convert mills to a date.
        From mills date = September 1, 2015 3:13:29 AM MDT
        Broken Date = September 1, 2015 3:13:29 AM MDT
        Attempting to parse broken date.
        Date in mills = 1441098809000
        From mills date = September 1, 2015 3:13:29 AM MDT
        
        Java 11 output...
        Java Version = 11.0.8
        Broken Date = September 1, 2015 3:13:29 AM MDT
        Convert mills to a date.
        From mills date = September 1, 2015 at 3:13:29 AM MDT
        Broken Date = September 1, 2015 3:13:29 AM MDT
        Attempting to parse broken date.
        java.text.ParseException: Unparseable date: "September 1, 2015 3:13:29 AM MDT"
        Unable to parse broken date.
        Adding in the at...
            at java.base/java.text.DateFormat.parse(DateFormat.java:395)
            at utility.OpenJDK11DateDisaster.main(OpenJDK11DateDisaster.java:60)
        Fixed date =  September 1, 2015 at 3:13:29 AM MDT
            java.text.ParseException: Unparseable date: " September 1, 2015 at 3:13:29 AM MDT"
            at java.base/java.text.DateFormat.parse(DateFormat.java:395)
            at utility.OpenJDK11DateDisaster.main(OpenJDK11DateDisaster.java:89)
        Unable to parse fixed date.
        
        */
        System.out.println("Java Version = " + System.getProperty("java.version"));
        // This is the date that will parse in Java 7 but fails in Java 11
        String BrokenDate = "September 1, 2015 3:13:29 AM MDT";
        System.out.println("Broken Date = " + BrokenDate);
        
        // Broken date converted to mills in Java 7.
        long mills = 1441098809000L;
        
            
        System.out.println("Convert mills to a date.");
        DateFormat df2 = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, Locale.US);
        
        Date millDate = new Date(mills);
        System.out.println("From mills date = " + df2.format(millDate));
        
        System.out.println("Broken Date = " + BrokenDate);
        
        
        /*
         * Parse these old dates.
         */
        System.out.println("Attempting to parse broken date.");
        df2 = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, Locale.US);
        Date date = null;
        try {
            date = df2.parse(BrokenDate) ;
        } catch (ParseException e) {
            e.printStackTrace();
            System.out.println();
            System.out.println("Unable to parse broken date.");
            
            // For some reason, they now put an "at" word between the date and the time.
            // Here we put the "at" back in and see.
            
            // See if we can fix it.
            System.out.println("Adding in the at...");
            StringList fields = Acl.split(BrokenDate, ' ');
            String FixedDate = "";
            Iterator iter = fields.iterator();
            int count = 0;
            while (iter.hasNext() ) {
                String field = (String)iter.next();
                count++;
                FixedDate += " " + field;
                if (count == 3) {
                    // add an at
                    FixedDate += " at";
                }
                
            }
            System.out.println("Fixed date = " + FixedDate);
            // Now see if it will parse
        
            try {
                date = df2.parse(FixedDate);
            } catch (ParseException e2) {
                e2.printStackTrace();
                System.out.println();
                System.out.println("Unable to parse fixed date.");
                System.exit(-1);
            }
            System.out.println("Was able to parse Fixed Date " + date);
        }
        mills = date.getTime();  // From the BrokenDate
        // Should be -62112243600000 as computed on a Java 7 machine
        System.out.println("Date in mills = " + mills);
        
        // Convert the mills to a date.
        millDate = new Date(mills);
        System.out.println("From mills date = " + df2.format(millDate));
    }
}
 
     
    