Results 1 to 9 of 9
Like Tree3Likes
  • 1 Post By jim829
  • 1 Post By DarrylBurke
  • 1 Post By maca

Thread: Bug in SimpleDateFormat???

  1. #1
    maca is offline Member
    Join Date
    Jul 2014
    Location
    UK
    Posts
    16
    Rep Power
    0

    Default Bug in SimpleDateFormat???

    So, I've been tracking down a bug in my program, and I think the bug is actually in SimpleDateFormat.

    Rather than posting my whole program, I have created a minimal program that replicates the bug and pasted it below:

    Java Code:
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    
    public class Main {
    
    	public static void main(String[] args) {
    
    		SimpleDateFormat ft = new SimpleDateFormat ("E dd MMM Y");
    
    		Date date = new Date("Sun Dec 28 16:07:09 GMT 2014");
    		System.out.println(date);
    		System.out.println(ft.format(date));
    		
    		date = new Date("Mon Dec 29 16:07:09 GMT 2014");
    		System.out.println(date);
    		System.out.println(ft.format(date));
    	}
    }
    The output of the above program is:

    Sun Dec 28 16:07:09 GMT 2014
    Sun 28 Dec 2014
    Mon Dec 29 16:07:09 GMT 2014
    Mon 29 Dec 2015
    As you can see, the result of the second output from SimpleDateFormat says 2015 when it should say 2014.

    Before I make a bug report, is there anything that I have done wrong? I know I have used a deprecated Date constructor, but I don't do that in my actual program where I first detected the bug.

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default Re: Bug in SimpleDateFormat???

    For me, the result of the first formatted output was also 2015. You should use 'y' instead of 'Y'. I found the following
    which is a good read. Go down to week-based-year for the explanation. I also presume that you in a different locale
    than I am so the first date printed correctly.

    IsoFields (Java Platform SE 8 )


    Regards,
    Jim
    Last edited by jim829; 08-30-2014 at 07:28 PM.
    maca likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: Bug in SimpleDateFormat???

    Quote Originally Posted by maca View Post
    Before I make a bug report, is there anything that I have done wrong?
    Yup. You've posted code that throws a java.lang.IllegalArgumentException: Illegal pattern character 'Y'

    This proves that the code posted isn't the code you ran for testing. So how about showing us the code that actually produced the output shown?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default Re: Bug in SimpleDateFormat???

    That's very strange because it ran for me without exception. I'm running Java 8 but that shouldn't matter.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    maca is offline Member
    Join Date
    Jul 2014
    Location
    UK
    Posts
    16
    Rep Power
    0

    Default Re: Bug in SimpleDateFormat???

    Quote Originally Posted by jim829 View Post
    For me, the result of the first formatted output was also 2015. You should use 'y' instead of 'Y'. I found the following
    which is a good read. Go down to week-based-year for the explanation. I also presume that you in a different locale
    than I am so the first date printed correctly.

    IsoFields (Java Platform SE 8 )


    Regards,
    Jim
    Thanks Jim, I haven't read the link yet, but I'll do that as soon as I stop writing this post. :) I get what you are saying about locale, but surely it couldn't change the answer by a year? I thought locale changes make a difference of minutes/hours? That aside, the fact that we are getting different outputs seems to suggest undefined behaviour???

    Quote Originally Posted by DarrylBurke View Post
    Yup. You've posted code that throws a java.lang.IllegalArgumentException: Illegal pattern character 'Y'

    This proves that the code posted isn't the code you ran for testing. So how about showing us the code that actually produced the output shown?

    db
    Weirdly, that is the exact code that I used to generate the output, I haven't modified it at all. Both the code and the result were copied and pasted from Eclipse Luna running with the Java 8 JDK.

    After using 'y' as Jim suggested, it now behaves as expected.

    The thing that concerns me now though is that we are all getting different behaviour from the same bit of code, Jim getting 2015 in the first formatted output when I got 2014 and you getting the IllegalArgumentException. I realise I picked the wrong case of 'y', but even so, 'Y' seems to at least be a valid argument according to the link below.

    SimpleDateFormat (Java Platform SE 7 )

    Do either of you think there could be a bug? According to the documentation 'Y' should have done something valid - there are no apparent rules that would make its inclusion invalid that I have noticed. Even if 'Y' doesn't do what I expected, it should surely have consistent behaviour on all of our platforms?

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: Bug in SimpleDateFormat???

    Quote Originally Posted by jim829 View Post
    That's very strange because it ran for me without exception. I'm running Java 8 but that shouldn't matter.

    Regards,
    Jim
    Yup. I get the error with Java SE 6. Took a look at the docs and 'Y' was introduced as 'Week year' from Java 7.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: Bug in SimpleDateFormat???

    Quote Originally Posted by maca View Post
    Do either of you think there could be a bug? According to the documentation 'Y' should have done something valid
    Jim already answered that.

    Quote Originally Posted by jim829 View Post
    Go down to week-based-year for the explanation.
    db
    maca likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default Re: Bug in SimpleDateFormat???

    Quote Originally Posted by DarrylBurke View Post
    Yup. I get the error with Java SE 6. Took a look at the docs and 'Y' was introduced as 'Week year' from Java 7.
    Well, clearly my assumption was wrong. I think I will re-install 6 and 7 and remember to check them out when similar issues arise.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    maca is offline Member
    Join Date
    Jul 2014
    Location
    UK
    Posts
    16
    Rep Power
    0

    Default Re: Bug in SimpleDateFormat???

    Quote Originally Posted by DarrylBurke View Post
    Jim already answered that.
    Yeah, I didn't realise at the time I was replying because I hadn't read the link that Jim provided. I've read it now and it all makes sense - there is no bug.

    Thanks Jim for posting the link - it was really helpful.
    DarrylBurke likes this.

Similar Threads

  1. SimpleDateFormat()
    By ajetrumpet in forum New To Java
    Replies: 8
    Last Post: 04-02-2013, 10:48 AM
  2. JTextField and SimpleDateFormat
    By mine0926 in forum New To Java
    Replies: 15
    Last Post: 07-06-2010, 03:10 AM
  3. JFormattedTextField + SimpleDateFormat
    By Ralphw in forum AWT / Swing
    Replies: 1
    Last Post: 07-07-2009, 10:53 AM
  4. Locale with SimpleDateFormat
    By swati.jyoti in forum New To Java
    Replies: 4
    Last Post: 07-02-2009, 02:34 PM
  5. Using SimpleDateFormat
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 07-18-2008, 08:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •