-2

Getting an Unparseable date error while calculating difference between Current date/time and Start date/time for an user.

Error: java.text.ParseException: Unparseable date: "09/11/20 00:00:00 AM CDT" at java.base/java.text.DateFormat.parse(DateFormat.java:395) 

I get this error at line no.8, which is

String output2 = sdf1.format((sdf1.parse(startDate)).getTime());

'dateDifference' is a library used to calculate the difference between the current date/time and the start date/time of an user.

if(link.getAttribute("lastLogonTimeStamp")== null){
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    Calendar cur_time = Calendar.getInstance();
    cur_time.setTime(new Date());
    String output = sdf.format(cur_time.getTime());
    System.out.println(" +++++ Output +++++" + output);
          
    SimpleDateFormat sdf1 = new SimpleDateFormat("MM/dd/yy HH:mm:ss a zzz");
    String output2 = sdf1.format((sdf1.parse(startDate)).getTime());
    System.out.println(" +++++ Start Date +++++" + output2);
    int diff = dateDifference(output2);
    System.out.println(" +++++ Difference +++++" + diff);
          
    if(diff>0){
        System.out.println("Start Date is not a Future Date  :" + startDate);
        bw.write(id.getName()+","+ntID+","+id.getFirstname() +" "+id.getLastname() +","+id.getEmail()+ "," + id.getAttribute("empType")+ "," +lastLoginDt+ ","+mgrName+","+(String)id.getAttribute("startDate")+","+(String)id.getAttribute("title")+"\n");
        count++;
    }
}
Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
  • I don't think CDT is a recognised time zone alias. – Dawood ibn Kareem Mar 09 '22 at 20:05
  • Does this answer your question? [Calculating the difference between two Java date instances](https://stackoverflow.com/questions/1555262/calculating-the-difference-between-two-java-date-instances) – Med Elgarnaoui Mar 09 '22 at 20:06
  • Show example data, your inputs. – Basil Bourque Mar 09 '22 at 20:18
  • You are using terrible date-time classes that were years ago supplanted by the modern *java.time* classes defined in JSR 310. – Basil Bourque Mar 09 '22 at 20:19
  • Tip: Educate the publisher of your data about the standard date-time formats defined in [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601). – Basil Bourque Mar 09 '22 at 20:20
  • 1
    @BasilBourque - the input date is clearly shown in the error message, in Priyanka's first comment under the question. Also, replacing the pre-Java-8 classes with the corresponding classes from `java.time` is not going to fix the problem here, so it's a bit of red herring. The problem is that CDT is not a recognised alias for a time zone. – Dawood ibn Kareem Mar 09 '22 at 20:33
  • @DawoodibnKareem I don’t see any error message nor any sample data in the Question. On Stack Overflow, we are not expected to dredge through Comments to assemble a Question. Assembling a proper Question is the author’s job, not the readers’ job. – Basil Bourque Mar 09 '22 at 20:47
  • @BasilBourque I have mentioned the error message above which has sample input data. – Priyanka Das Mar 10 '22 at 05:39

2 Answers2

0

tl;dr

I would not accept such a poor input string into my own app. But if you insist, you can try to parse ambiguous input such as CDT but this is a guessing game that may fail depending on the input.

ZonedDateTime.parse(
        "09/11/20 00:00:00 AM CDT" ,
        DateTimeFormatter.ofPattern( "MM/dd/uu HH:mm:ss a z" )
)

Parsing

CDT is not a real time zone. It is a localized indicator of whether Daylight Saving Time (DST) is effect.

Do not use localized formats for data exchange. Use localized values only for presentation to the user. For data exchange, use only ISO 8601 standard formats. The standard was invented for just that purpose, data exchange. The java.time classes use the standard formats by default when parsing/generating strings, so no need to specify formatting patterns.

Do not use Calendar and SimpleDateFormat classes. These terrible date-time classes are now legacy, years ago supplanted by the modern java.time classes defined in JSR 310. Search to learn more as this has been covered many many times already on Stack Overflow.

You can ask DateTimeFormatter class to guess what CDT might mean. But those pseudo-zone values are not standardized, and are not even unique! For example CST might mean "China Standard Time" or might mean "Central Standard Time" (in North America).

I recommend against accepting such poor inputs as yours, as playing guessing games in your code makes for unreliable apps. But if you insist:

String input = "09/11/20 00:00:00 AM CDT";
DateTimeFormatter f = DateTimeFormatter.ofPattern( "MM/dd/uu HH:mm:ss a z" );
ZonedDateTime zdt = ZonedDateTime.parse( input , f );

zdt.toString() = 2020-09-11T00:00-05:00[America/Chicago]

The text generated by ZonedDateTime#toString is actually an extension to the ISO 8601 standard format, appending the name of the zone in square brackets.

Calculating elapsed time

Apparently you want to calculate the amount of time elapsed between the moment represented by your input and the current moment.

To calculate elapsed time in terms of hours-minutes-seconds, use Duration while capturing the current moment as seen in UTC (an offset from UTC of zero hours-minutes-seconds).

Duration elapsed = Duration.between( zdt.toInstant() , Instant.now() ) ;

To calculate elapsed time in terms of years-months-days, use Period. Access the time zone contained in our ZonedDateTime to get the same timeframe.

Period elapsed = Period.between( zdt , ZonedDateTime.now( zdt.getZone() ) ; 
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

I have rewritten the code in the below format and that worked.

if(lastLogon == null || lastLogon.equalsIgnoreCase("never")){
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    Calendar cur_time = Calendar.getInstance();
    cur_time.setTime(new Date());
    String output = sdf.format(cur_time.getTime());
              
    SimpleDateFormat dateParser = new SimpleDateFormat("MM/dd/yy HH:mm:ss a zzz");
    Date date = dateParser.parse(startDate);
    SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyyMMddHHmmss");
    String output2 = dateFormatter.format(date);
              
    int diff = dateDifference(output2);
    if(diff>0){}