Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6184437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:32:07+00:00 2026-05-24T01:32:07+00:00

I have encountered a very weird behavior while using SimpleDateFormat for parsing a string

  • 0

I have encountered a very weird behavior while using SimpleDateFormat for parsing a string to a date. Consider the following unit test:

@Test
public void testParse() throws ParseException
{
    DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");

    String dateStr = "2012-12-21";
    Date parsedDate = dateFormat.parse(dateStr);
    Calendar date = Calendar.getInstance();
    date.setTime(parsedDate);

    Assert.assertEquals(2012, date.get(Calendar.YEAR));
    Assert.assertEquals(11, date.get(Calendar.MONTH)); // yeah, Calendar sucks
    Assert.assertEquals(21, date.get(Calendar.DAY_OF_MONTH));
}

As it can be seen there is an intentional mistake in the above code: the SimpleDateFormat is initialized with "yyyyMMdd" but the string to be parsed is in the format "yyyy-MM-dd". I would expect that such thing results in a ParseException, or at least be parsed on a best-effort basis correctly. Instead, for some weird reason the date is parsed as 2011-11-02. Eh?!

This is unacceptable as one single mistake while handling the inputs would result in something totally unexpected / devastating. Switched to JodaTime in the meantime, but it would be nice to understand what went wrong there.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T01:32:08+00:00Added an answer on May 24, 2026 at 1:32 am

    Well, the input would be split into 3 components: year, month, day and you’d get month = -12 and day = -21 (for correction see below). Try to parse 2012/12/21 and you’ll get the exception 🙂

    Edit: excerpt from the JavaDoc:

    Month: If the number of pattern letters is 3 or more, the month is interpreted as text; otherwise, it is interpreted as a number.

    Edit2: Correction

    Looking at the source of SimpleDateFormat it seems that 2012-12-21 is actually split into this:

    year = "2012"
    month = "-1"
    day = "2-" 
    

    The source comments state that a - following a number might either denote a negative number (depending on the locale) or be a delimiter. In your case it seems to be taken as a delimiter, thus day = "2-" results in day = 2, hence the second of November.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have encountered odd behavior when using document.getElementById tonight. Duplicated in Firefox 3 and
I have encountered a weird situation while updating/upgrading some legacy code. I have a
I am debugging some code and have encountered the following SQL query (simplified version):
I am using ajaxForm. Now I have encountered a problem. My idea is when
We have encountered a very strange class not found problem in our web app
I have encountered a very strange issue. Whenever I use the .NET membership provider
I have just encountered very strange problem - my GWT app hosted on Google
I have encountered a weird problem twice in the past 2 weeks and it's
I have encountered something very strange, simple WPF application <Window x:Class=ListBoxSelection.MainWindow xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml Title=MainWindow
I have encountered a situation where it would be very useful to specify a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.