0

I'm trying to avoid reinstalling Eclipse to solve this, so I hope someone can help with this date-parsing problem. Here's what I do.

DateFormat dateFormat; 

That's a variable in my class. I set it to the following.

dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
// Example input: 2010-11-07 16:00:00

When another method is called, I create a new java.sql.Date instance using strings that are passed in. For the sake of clarity, here's a shortened version.

public void aMethod (String activateTime, String expireTime) {
    Date example = new Date(dateFormat.parse(activateTime).getTime());
    Date example2 = new Date(dateFormat.parse(expireTime).getTime());
}

When I look at the strings resulting from these operations (just adding .toString() to instantiations above), I get output like the following

2010-10-30
2010-11-29

... instead of the input strings, which are reported to be...

2010-10-30 17:00:00
2010-11-29 16:00:00

Anyone know why it doesn't give me a date in the pattern I specified?

Community
  • 1
  • 1
Danny
  • 3,670
  • 12
  • 36
  • 45

3 Answers3

3

The java.sql.Date contains only the date part of datetime. You probably want to use java.sql.Timestamp instead.

Timestamp example = new Timestamp(dateFormat.parse(activateTime).getTime());
Timestamp example2 = new Timestamp(dateFormat.parse(expireTime).getTime());

It also works like that in the average SQL database. The DATE data type represents only the date, the TIME data type represents only the time. The TIMESTAMP data type (sometimes called DATETIME) represents the date and time.

Note that your hour pattern is incorrect. With the given examples, you'd rather like to use HH instead of hh. See also the SimpleDateFormat javadoc.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • The `java.sql.Timestamp` toString method returns the format "yyyy-mm-dd hh:mm:ss.fffffffff" - still not what the OP wants because it contains nanosecond precision. – Cameron Skinner Nov 09 '10 at 02:03
  • Although the dateFormat Cameron mentioned did in fact report more than just the date part of the datetime, you're completely right about the HH. Thanks for that! – Danny Nov 09 '10 at 02:06
  • @Cameron: Since the OP is using `java.sql.Date` instead of `java.util.Date`, I didn't expect him to care about the *format*. The sole point of using `java.sql.Date` is to be able to persist a date using JDBC in a SQL database. Using it for presentation purposes is plain wrong. Note that when you're about to *actually* persist it in DB, the time part will get lost. So, when you query it back from DB again and format it, the time part will appear as `00:00:00`. – BalusC Nov 09 '10 at 02:06
  • That's a good warning and I wonder if it's related to another problem I have. I'm wondering now if I have to move around symbols and numbers to satisfy the "date" format that I see in PL/SQL. For example, does 2010-11-25 16:00:00 need to become 11/25/2010 16:00:00? It seems like a really stupid thing for the class to need. It also makes me wonder if it's a headache I can avoid with TimeStamp. – Danny Nov 09 '10 at 02:33
  • @BalusC: Yes, you're right, this is an unusual usage of `java.sql.Date`. For the purposes of display the OP seemed to want a particular format, hence my comment. – Cameron Skinner Nov 09 '10 at 11:40
  • @Danny: The `java.sql.Date` class will abstract away all the details of what your particular database wants, so you don't need to worry about it when talking to the DB. When you present the information to a user you can use the DateFormat class to format it in the way you want (and treat it as a `java.util.Date`). – Cameron Skinner Nov 09 '10 at 11:41
  • Works and IMHO is the more likely culprit in this situation. Or rather, once you got the formatting correct, you would discover that the values for time were all zero because java.sql.Date does not populate them (which is the answer i was looking for when i found this, thanks!) – jsh Dec 21 '11 at 21:31
2

The javadocs for java.sql.Date specify that the toString method returns a string in the format "yyyy-mm-dd", so you are seeeing the correct behaviour. The Date object has no knowledge of the formatter you used to create it. To get output in the same format use the DateFormat.format method rather than toString:

public void aMethod (String activateTime, String expireTime) {
    Date example = new Date(dateFormat.parse(activateTime).getTime());
    Date example2 = new Date(dateFormat.parse(expireTime).getTime());
    System.out.println(example.toString() + " vs " + dateFormat.format(example));
    System.out.println(example2.toString() + " vs " + dateFormat.format(example2));
}
Cameron Skinner
  • 51,692
  • 2
  • 65
  • 86
0

Did you try to change java.sql.Date to java.util.Date?