Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

SimpleDateFormat...what am I missing?

The editor here is wreaking a little havoc with my code, but I'm going to hope you can decipher what I'm after here. I have simplified this code bit about as far as I can to isolate the problem, while still leaving it executable:

import java.text.*;

class dateTest {

public static void main (String[] args) throws Throwable {

SimpleDateFormat sdf = new SimpleDateFormat("MM/DD/yyyy");

String data = "07/12/2007";

System.out.println(sdf.parse(data));

}

}

The problem is, the output looks like this:

Fri Jan 12 00:00:00 CST 2007

Month parsing doesn't seem to be working at all. I've done this dozens of times and I'm starting to think maybe I'm losing it. :)

Update:

I don't think either of the answers so far has seen the essence of the problem: The seventh month is not JANUARY.

I think this looks like a bug that may have been introduced in a recent Java update. I'm hoping someone has genuine insight into why this is working the way it does.

2 Answers

Relevance
  • McFate
    Lv 7
    1 decade ago
    Favorite Answer

    The issue is 'DD' (Day Of Year) which overwrites the month when parsed. Use 'dd' (Day Of Month) and it will work.

    ====================

    SimpleDateFormat sdf = new SimpleDateFormat( "MM/dd/yyyy");

    String data = "07/12/2007";

    Date d = sdf.parse( data);

    System.out.println( "" + d);

    ====================

  • 1 decade ago

    The parse() method returns a Date object - NOT a formatted date String. You want format(). Check...

    http://java.sun.com/j2se/1.4.2/docs/api/java/text/...

Still have questions? Get your answers by asking now.