• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need to get Date in dd-MM-yyyy format

 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to get date in dd-MM-yyyy format, but in a Date object type.

SimpleDateFormat class method format return the date into String object, where as its parse method returns date , but not in dd-MM-yyyy format.

I need to use the date for querying records saved in db (in date object)
 
Matthew Brown
Bartender
Posts: 4566
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kunal Lakhani wrote:I need to get date in dd-MM-yyyy format, but in a Date object type.

No you don't. It's a common misunderstanding, but Date objects don't have any formatting, so it doesn't make sense to ask for a Date object in a particular format. Once you've got a Date object, just use that wherever you need a Date object. At the point you need a String, then use one of the DateFormat classes to get the String in whatever format you want.

I think one source of the misunderstanding is that if you use the toString() method on a Date object, it will use a default format. But it does not mean that the Date object itself is that format.
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kunal

The Date class is exactly what it says on the tin - a date in time. It is the responsibility of other classes (see those suggested by Matthew) to display the date in a particular format. Given the vast number of formats, it makes no sense for a Date object to hold this information.
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your replies
I have saved date related data as date type in database. And i need to query it by using a date type.

Now, the problem i am facing is that, in database, date is saved as 27-Jun-2012, and while querying for the same date, it returns no value found. On little bit research i found that, its querying for this value "Wed Jun 27 00:00:00 IST 2012". i am using Hibernate for database works
 
Paul Clapham
Sheriff
Pie
Posts: 20978
31
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, I see, so the question was actually about the wrong way to do that. If your database column is a Date type, then java.sql.Date (NOT java.util.Date) is the corresponding type in Java. Don't use the java.sql.Timestamp type because that corresponds to a Timestamp database type, which contains both date and time components.

And don't try to match the column using text representations of the dates, either. Use a PreparedStatement and use its setDate() method to insert the parameter which represents the value you're comparing to.
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kunal

Can you post the entity class which holds the date field in question? Also, post the code which inserts and queries this field in the database.
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your replies

Entity class



I am using hibernate with swing.
Swing code



Dao code



Date i am querying for is 27-Jun-2012 (which is stored in db), but the date passed to this dao class method is "Wed Jun 27 00:00:00 IST 2012"
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul, then, i need to change all the codes (around 50 classes) where i have used java.util.Date. what all things i need to change if i go for java.sql.Date?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're using Hibernate then you don't need to use java.sql.Date explicitly; Hibernate automatically translates it from and to java.util.Date.
 
Winston Gutkowski
Bartender
Pie
Posts: 10257
59
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kunal Lakhani wrote:Paul, then, i need to change all the codes (around 50 classes) where i have used java.util.Date. what all things i need to change if i go for java.sql.Date?

And further to what Jesper said, sometimes you just have to bite the bullet. If you got something wrong, far better to put it right NOW than wait until 50 classes become 500.

Luckily for you, it seems that Hibernate has been kind enough to handle that particular case.

Winston
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kunal

Could you also include your hibernate XML configuration. I'm assuming you have one as you are not using annotations.
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kunal

Instead of using equality, you could try using a date range.



where d1Start is the date with time 00:00:00 and d1End is the same date with time 23:59:59.

Perhaps not ideal but it handles database types which contain just a date and those with a date and time.
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your replies

Yes, i am using hibernate, but the problem is not solved. Moreover, i am using JDateChooser for date related stuffs, which has a getDate() method which returns a Date.

This is my swing code


Above code adds the list of dates (fetched from db and properly formatted in "dd-MM-yyyy", but as String Type and later on converted to date, by using parse()) to a a combobox.

Here is my hbm file


James Boswell, date range doesn't seems to be ideal. There would be a direct solution.


Once again, thanks to all of you.>
 
Winston Gutkowski
Bartender
Pie
Posts: 10257
59
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kunal Lakhani wrote:Above code adds the list of dates (fetched from db and properly formatted in "dd-MM-yyyy", but as String Type and later on converted to date, by using parse()) to a a combobox.

Well, that sounds redundant to me for a start. If the field in the database is an SQL DATE, just read it into a Java Date (as I believe has already been suggested). And if the field in the database is a String, then whoever did it deserves to be shot (and they wouldn't have done it on my watch, I can tell you).

Tip: Never, ever, ever, EVER use Strings to store other data types - at least not until you're a lot further down the Java road.
It's error-prone, redundant; and almost always space-hogging and SLOW.

In your specific case, keep the values from your db as Dates (java.sql.Date or java.util.Date; doesn't really matter which), until you're actuallly ready to put them into a display field (and to be honest, I'm quite surprised there aren't Swing widgets for displaying Dates directly after all this time).

Parsing is another matter: often you're forced to accept String (or character-based) input, but the same rule applies in reverse - convert it to the correct datatype as fast as you possibly can, and forget about the String.

HIH

Winston
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply Winston Gutkowski. And Thanks for sharing the knowledge.

If i directly add(without formatting it as a string) the dates retrieved from the db to the combobox, it shows dates like "2012-06-27 18:15:09.0 "
I wonder if there is a way in which i can simply add dates (as a date object) into the combobox in a proper format (dd-MM-yyyy)
 
Winston Gutkowski
Bartender
Pie
Posts: 10257
59
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kunal Lakhani wrote:If i directly add(without formatting it as a string) the dates retrieved from the db to the combobox, it shows dates like "2012-06-27 18:15:09.0 "
I wonder if there is a way in which i can simply add dates (as a date object) into the combobox in a proper format (dd-MM-yyyy)

That, unfortunately, is beyond my ken. I'm no GUI expert. Hopefully there are others out there who are. All I can tell you is that if you have to convert it to a String for display, do it at the last possible moment.

Winston
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Winston Gutkowski.

Thanks to all of you. Couldn't find the perfect solution, so i am now using date range.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic