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 14 of 14

Thread: Unparseable Date

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Unparseable Date

    Hello,

    I'm new in Java and currently developing a project which requires to use dates for some of its functionality.

    When trying to get the difference between the current date vs the email was sent I'm encountering the issue below.

    *****:java.text.ParseException: Unparseable date: "29 May 2014 18:34:29"

    Here is what my code looks like:

    1. I'm getting the date of the email was sent by using mail.header. Btw I'm coding under JBPM studio.

    String messageDate = (String) mail.headers["Date"];

    The outpout for this looks like this: Mon, 26 May 2014 14:37:51 +0000

    2. After i gather the date I parsed it to remove the day and the timezone.

    String[] l_messageDate = messageDate.split(delim : ",");

    String[] l_messageDatex;

    if (l_messageDate.length() > 1) {
    l_messageDatex = l_messageDate[1].trim().split(delim : "+");
    }

    String l_messageDate1 = l_messageDatex[0].trim();

    Output: 29 May 2014 18:34:29

    3. After parsing I'm using the following codes to minus the current date and the date of the email.

    Time l_dtCurrentDate = 'now';

    SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

    Time d1 = null;
    Time d2 = null;

    d1 = format.parse(l_messageDate1.trim());
    d2 = format.parse((String)l_dtCurrentDate);

    long diff = d2.DATE_TIME - d1.DATE_TIME;

    long diff2 = d1.DATE_TIME - d2.DATE_TIME;

    long diffMinutes = diff / (60 * 1000) % 60;

    long diffMinutes2 = diff2 / (60 * 1000) % 60;

    When the these statement run I'm encountering the said issue. I would like to request for your help in correcting my code if there are any issues.

    Regards,
    Kenneth


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

    Default Re: Unparseable Date

    Unparseable date: "29 May 2014 18:34:29"
    Is that the input date you are trying to parse?

    Where is the format String for parsing that date?

    Please edit your post and wrap your code with code tags:
    [code=java]
    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.

  3. #3
    Member
    Join Date
    Sep 2013
    Posts
    68
    My Mood
    Confused
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default Re: Unparseable Date

     SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
    here you are using MM/dd/yyyy HH:mm:ss format to parse a string to date wich is in dd MMM yyyy HH:mm:ss format.

    use
     SimpleDateFormat format = new SimpleDateFormat("dd MMM yyyy HH:mm:ss");
    and store it in a Date object and aftre that use your logic to find the difference d/w the parsed date object and the current date object.

  4. #4
    Junior Member
    Join Date
    Apr 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Unparseable Date

    Thank you all for your replies,

    @aprabhat how about the format of the current date. When I extract it the output looks like this:

    2014-05-30 06:39:11.482000-05:00

    So basically i need to create 3 SimpleDateFormat, 1 for the email date/time, 1 for the current date and the last one is for the final format?

    Please correct me if I'm wrong the SimpleDateFormat is used to change the format type of a date?

    Many Thanks,

    Regards,
    Kenneth

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Unparseable Date

    You need a SimpleDateFormat instance for EACH date to be parsed and for EACH date to be formatted if they are different.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Apr 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Unparseable Date

    @Norm thanks,

    would you know what format is this in? 2014-05-30 06:39:11.482000-05:00

    Many Thanks,
    Kenneth

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Unparseable Date

    Not really. I'd look at the API doc to see what parsing characters are available for that date.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Apr 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Unparseable Date

    @Norms, thanks I will look it up,

  9. #9
    Member
    Join Date
    Sep 2013
    Posts
    68
    My Mood
    Confused
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default Re: Unparseable Date

    Here is your SimpleDateFormat that you need "yyyy-mm-dd HH:mm:ss.SSSZ" to parse "2014-05-30 06:39:11.482000-05:00".
    Hope this will solve your problem and for more detail try This

  10. #10
    Junior Member
    Join Date
    Apr 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Unparseable Date

    @aprabhat thanks so much...i will bookmark the link for future reference.

    --- Update ---

    Here is now my updated code:

    // Subtract the minutes of system vs email recieved. Start
    SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

    SimpleDateFormat format2 = new SimpleDateFormat("dd MMM yyyy HH:mm:ss");

    SimpleDateFormat format3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSZ");

    Time l_CurrDate = null;
    Time l_MailDate = null;
    Time d1 = null;
    Time d2 = null;

    l_MailDate = format2.parse(l_messageDate1.trim());
    l_CurrDate = format3.parse((String)l_dtCurrentDate);

    d1 = format.parse((String)l_MailDate);
    d2 = format.parse((String)l_CurrDate);

    long diff = d2.DATE_TIME - d1.DATE_TIME;

    long diff2 = d1.DATE_TIME - d2.DATE_TIME;

    long diffMinutes = diff / (60 * 1000) % 60;

    long diffMinutes2 = diff2 / (60 * 1000) % 60;

    I will post the result once tested..Thank you so much for your inputs.

  11. #11
    Member
    Join Date
    Sep 2013
    Posts
    68
    My Mood
    Confused
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default Re: Unparseable Date

    @kennethdizon you'r welcome.

  12. #12
    Junior Member
    Join Date
    Apr 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Unparseable Date

    Hi All,

    I was able to make the code work by simply using Interval function and didn't use d1 and d2.

    SimpleDateFormat format1 = new SimpleDateFormat("dd MMM yyyy HH:mm:ss");
    SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    Time l_MailDate = null;
    Time l_CurrDate = null;

    l_MailDate = format1.parse(l_messageDate1.trim());
    l_CurrDate = format2.parse(l_dtCurrentDate3.trim());

    Interval diff = l_CurrDate - l_MailDate;

    Output: 12h55m20s

    May I ask if you know how to convert this to milliseconds?

    Thanks so much for your help,

    Regards,
    Kenneth

  13. #13
    Member
    Join Date
    Sep 2013
    Posts
    68
    My Mood
    Confused
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default Re: Unparseable Date

    Don't know about any class or method to convert 12h55m20s to millisecond but you can do it easily by some string manipulation and change the seconds to millisecond as 1 second = 1000 millisecond.

  14. #14
    Junior Member
    Join Date
    Apr 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Unparseable Date

    Hello All,

    I tried converting the message date to the same timezone of the current date inorder to get the correct time difference between the 2, however upon conversion I have encountered another error.

    I created another thread for the new error titled "java.lang.IllegalArgumentException: Cannot format given Object as a Date"

    Hope for your help,

    Thanks so much,
    Kenneth

Similar Threads

  1. Start Date should be exactly 1 week before to End Date
    By bhanuchandar in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 26th, 2013, 08:07 AM
  2. Java Date Format in Date Object
    By Ashr Raza in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 13th, 2012, 10:47 AM
  3. Parsing a full date/time/timezone date to "yyyy-MM-dd"
    By Occidentally in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 4th, 2012, 08:57 AM
  4. Replies: 1
    Last Post: July 22nd, 2011, 07:08 AM
  5. same date should entered in another date field
    By shashib09 in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: July 14th, 2011, 08:42 AM