English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

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. :)

2007-08-23 11:51:39 · 2 answers · asked by David G 5 in Computers & Internet Programming & Design

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.

2007-08-24 01:37:58 · update #1

2 answers

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);

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

2007-08-23 12:16:33 · answer #1 · answered by McFate 7 · 1 1

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/SimpleDateFormat.html

2007-08-23 12:13:57 · answer #2 · answered by richarduie 6 · 0 1

fedest.com, questions and answers