Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Pasrsing error when comparing two dates

  1. #1
    Junior Member
    Join Date
    Jan 2022
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Pasrsing error when comparing two dates

    Im creating a date from Calendar with format ("EEE MMM dd kk:mm:ss z yyyy"); example Mon Nov 29 10:10:50 CST 2021. Im then grabbing a date from linked string/data object in format ("MM/dd/yyyy");. In to convert it to the same format as the Calendar date for use to compare the dates. Im not sure why Im getting this errors. Any help would be greatly appreciated

                          import java.text.DateFormat;
                          import java.text.SimpleDateFormat;
                          import java.util.ArrayList;
                          import java.util.Collections;
                          import java.util.Date;
                          import java.util.Iterator;
                          import java.util.List;
    		      import java.time.*;
                          import java.time.format.*;
     
                          import org.apache.log4j.Level;
                          import org.apache.log4j.Logger;
     
     
                         private Date getDateNDaysAgo(int numDays) {
                          log.debug("getDateNDaysAgo 1 " + numDays);
                          Calendar cal = Calendar.getInstance();
                          Date returnDate = null;
                          cal.add(Calendar.DATE, -(numDays));
                          returnDate = cal.getTime();
                          log.debug("getDateNDaysAgo 2" + returnDate);
                          return (returnDate);
    		   }	
     
                          if(getDateNDaysAgo != null) {	
                            System.out.println("getDateNDaysAgo 2" + getDateNDaysAgo);					  
     
                        } 
     
                          Date thresholdDate = null;
    		      boolean isAccountCreatedOver25Days = false; 
                          String EffectiveHireDateString = link.getAttribute("EffectiveHireDate");  // MM/dd/yyyy
     
    				  if(Util.isNotNullOrEmpty(EffectiveHireDateString)) {
     
                      DateTimeFormatter mdy = DateTimeFormatter.ofPattern("MM/dd/yyyy");
                      DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM dd kk:mm:ss z yyyy");
     
     
                      try {
     
    		   LocalDate EffectiveHireDate = mdy.parse(EffectiveHireDateString, LocalDate::from);
                        LocalDateTime ldt = LocalDateTime.of(EffectiveHireDate, LocalTime.of(0, 0));
                        ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.systemDefault);
     
    		   String formatted = formatter.format(zdt);
                        ZonedDateTime parsedEffectiveHire = formatter.parse(formatted, ZonedDateTime::from);
                        log.debug("parsedEffectiveHire" + parsedEffectiveHire);
     
                        thresholdDate = getDateNDaysAgo(25);
                        log.debug("thresholdDate" + thresholdDate);
     
                       if(parsedEffectiveHire != null) {	
                            System.out.println("parsedEffectiveHire" + parsedEffectiveHire);					  
    					  }
     
     
                      if(parsedEffectiveHire.compareTo(thresholdDate) < 0){
                      log.debug("parsedEffectiveHire 2" + parsedEffectiveHire);
     
                      isAccountCreatedOver25Days = true; 
                      }
                      }
                          catch (ParseException e) {
                          log.error("Error attempting to parse EffectiveHireDate Date");
                          }
                          }
                          }
    Last edited by spacefrowns; January 31st, 2022 at 05:03 PM. Reason: code tags

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Pasrsing error when comparing two dates

    why Im getting this errors
    Please copy the full text of the error messages and paste it here.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    Make sure the posted code can be compiled and executed for testing. I do not see a declaration for the class or a main method for execution.
    http://sscce.org/
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2022
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Pasrsing error when comparing two dates

    Its beanshell with code passing in in xml object.

    Exception: sailpoint.tools.GeneralException: BeanShell script error: bsh.ParseException: Parse error at line 93, column 65. Encountered: ( BSF info: script at line: 0 column: columnNo
    sailpoint.tools.GeneralException: sailpoint.tools.GeneralException: BeanShell script error: bsh.ParseException: Parse error at line 93, column 65.

    which is this block


    try {

    LocalDate acuEffectiveHireDate = mdy.parse(acuEffectiveHireDateString, LocalDate::from);
    LocalDateTime ldt = LocalDateTime.of(acuEffectiveHireDate, LocalTime.of(0, 0));
    ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.systemDefault);

    String formatted = formatter.format(zdt);
    ZonedDateTime parsedAcuEffectiveHire = formatter.parse(formatted, ZonedDateTime::from);
    log.debug("parsedAcuEffectiveHire" + parsedAcuEffectiveHire);

    thresholdDate = getDateNDaysAgo(25);
    log.debug("thresholdDate" + thresholdDate);

    if(parsedAcuEffectiveHire != null) {
    System.out.println("parsedAcuEffectiveHire" + parsedAcuEffectiveHire);
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Pasrsing error when comparing two dates

    Sorry, I do not know anything about beanshell.
    If you have a java programming problem please make a SSCCE and paste it here.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jan 2022
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Pasrsing error when comparing two dates

    Basically. Im trying to get a string date format MM/dd/yyyy converted to Date with format EEE MMM dd kk:mm:ss z yyyy for use to compare to another date format EEE MMM dd kk:mm:ss z yyyy. Failing miserable it seems.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Pasrsing error when comparing two dates

    Failing miserable it seems.
    Make a sscce that demonstrates the problem and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jan 2022
    Posts
    20
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Pasrsing error when comparing two dates

    I couldn't get it to recognize ZoneId without the specific import:
    import java.time.ZoneId;

    Next in the try block you are using systemDefault but that is a method and needs the ()
    ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.systemDefault());


    A little further down I ran into another problem.
    First I needed the calendar import:
    import java.util.Calendar;

    After that the compareTo line is fouled. You cannot compare Date with ZonedDateTime.
    (I also have no idea how to implement the &lt generics comparison thing so I skipped that until I can learn more about it)
    I ran into bracket nightmare so I split it into two lines:
            ZonedDateTime z = ZonedDateTime.ofInstant(thresholdDate.toInstant(), ZoneId.systemDefault());
            if(parsedEffectiveHire.compareTo(z) < 0){

    The following is the entire code I ran using the online compiler found here
    Please excuse the default class name HelloWorld and the fact that I changed some of the variable names you used.
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Date;
    import java.util.Iterator;
    import java.util.List;
    import java.time.*;
    import java.time.format.*;
    import java.time.ZoneId; 
    import java.util.Calendar; 
     
    public class HelloWorld{
     
         public static void main(String []args){
            //System.out.println("Hello World");
            String stringDate = "01/12/21";
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM dd kk:mm:ss z yyyy");
            DateTimeFormatter mdy = DateTimeFormatter.ofPattern("MM/dd/yy");
            Date date = new Date("SAT DEC 28 01:12:32 PST 1996");
            //Date newDate = mdy.parse()
            LocalDate d = mdy.parse(stringDate, LocalDate::from);  
            LocalDateTime ldt = LocalDateTime.of(d, LocalTime.of(0, 0));
            ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.systemDefault());
            System.out.println(d);
            System.out.println(ldt);
            System.out.println(zdt);
     
            String formatted = formatter.format(zdt);
            ZonedDateTime parsedEffectiveHire = formatter.parse(formatted, ZonedDateTime::from);
            System.out.println("parsedEffectiveHire " + parsedEffectiveHire);
     
            Date thresholdDate = getDateNDaysAgo(25);         
            System.out.println("threshold date " + thresholdDate);
            ZonedDateTime z = ZonedDateTime.ofInstant(thresholdDate.toInstant(), ZoneId.systemDefault());
            if(parsedEffectiveHire.compareTo(z) < 0){
                System.out.println("older than 25 days");
            }
         }
         private static Date getDateNDaysAgo(int numDays) {
                          //log.debug("getDateNDaysAgo 1 " + numDays);
                          Calendar cal = Calendar.getInstance();
                          Date returnDate = null;
                          cal.add(Calendar.DATE, -(numDays));
                          returnDate = cal.getTime();
                          //log.debug("getDateNDaysAgo 2" + returnDate);
                          return (returnDate);
    		   }	
    }

    It really is better to cut out enough code for the forum viewer to simply cut and paste and run.
    This gives you far quicker and better results.
    Last edited by LeslieS; February 1st, 2022 at 11:22 PM.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Pasrsing error when comparing two dates

    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Jan 2022
    Posts
    20
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Pasrsing error when comparing two dates

    Quote Originally Posted by Norm View Post
    Looks like it. Its a shame he might miss the answer here, but over there they seem to know a lot more than I do about the various date types in java.

  10. #10
    Member
    Join Date
    Jan 2024
    Posts
    75
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Pasrsing error when comparing two dates

    It seems you're encountering a parsing error when comparing two dates in your Java code. The issue may lie in how you're parsing and comparing the dates. Let's break down the potential solutions:

    1. Parsing Error:
    - Ensure that the date formats used for parsing (`"MM/dd/yyyy"` and `"EEE MMM dd kk:mm:ss z yyyy"`) match the actual formats of the dates you're trying to parse. Any discrepancy will lead to parsing errors.
    - In your code, you're using `DateTimeFormatter` for parsing the date string into a `LocalDate` and then formatting it into a string again. This step might not be necessary if your goal is just to compare the dates. Instead, directly parse the date string into a `ZonedDateTime`.

    2. Comparison Error:
    - When comparing dates, make sure both dates are of the same type. In your case, both `parsedEffectiveHire` and `thresholdDate` should be of type `ZonedDateTime` since you're dealing with time zones.
    - Check if `parsedEffectiveHire` and `thresholdDate` are not null before attempting the comparison to avoid `NullPointerException`.
    - Use `compareTo` method correctly for date comparison. Ensure you're using `compareTo` method properly according to your comparison logic.

    3. Debugging:
    - Add debug statements to print out the parsed dates (`parsedEffectiveHire` and `thresholdDate`) just before the comparison to ensure they are parsed correctly and have expected values.
    - Check the log messages for any additional information or errors that might help pinpoint the issue.

    Here's a revised version of the comparison part of your code:

    ```java
    if (parsedEffectiveHire != null && thresholdDate != null) {
    System.out.println("parsedEffectiveHire: " + parsedEffectiveHire);
    System.out.println("thresholdDate: " + thresholdDate);

    if (parsedEffectiveHire.compareTo(thresholdDate) < 0) {
    log.debug("parsedEffectiveHire is before thresholdDate");
    isAccountCreatedOver25Days = true;
    } else {
    log.debug("parsedEffectiveHire is after or equal to thresholdDate");
    }
    }
    ```

    Make sure to adjust the comparison logic (`< 0`, `== 0`, or `> 0`) according to your specific requirements. Also, ensure that the date formats and types are consistent throughout the code. If you encounter any errors or unexpected behavior, review the debug output and log messages for further insights.

    In case you need further assistance with your Java assignment and resolving the date parsing error, there are resources available online that provide programming homework help like ProgrammingHomeworkHelp.com. You might find useful insights on platforms that offer educational support for Java programming tasks.

Similar Threads

  1. comparison between 3 dates :
    By Neha211 in forum Loops & Control Statements
    Replies: 1
    Last Post: June 19th, 2014, 02:52 PM
  2. problem Comparing date and month from dates
    By desire7696 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 6th, 2012, 10:08 AM
  3. Comparing Two Dates
    By aussiemcgr in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 27th, 2011, 02:09 PM
  4. error when comparing 2 string objects
    By amr in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 9th, 2011, 07:36 PM
  5. UTC Dates
    By PedroCosta in forum Java Theory & Questions
    Replies: 4
    Last Post: April 1st, 2010, 11:39 AM

Tags for this Thread