views:

1366

answers:

3

hi all,

i want to format date to string in hql select, for example i have purchasing data with transaction date in it:

class Purchase {

  private Date datePurchase

}

and i want to select date in a certain format, for example yyyyMMdd, can i do that in hql?

actually i can iterate through all purchase data returned by query, and start to format the date using SimpleDateFormat, but i don't want do that, i want to do it in hql, is it posible?

fyi, i just want to return the id and date string only, not all Purchase field.

thak you all for any help.

A: 

The date data will be returned to your application at instances of the Date class. Using the DateFormat classes you can format the date into any format you want.

It's not the database's job to prepare your data for presentation.

landon9720
+1  A: 

You could use a TypeConverter and return a string or use another getter where you would return the date in the appropriate format.

John Doe
A: 

i just want to return the id and date string only, not all Purchase field.

Hmm... You're populating a Date in your model class. The format is not a concern, here.

But for presentation, why not just add a method to Purchase that returns the datePurchase as a String in the format that you want? If in your case that would violate any ground rules regarding what can/can't go into model classes, you could always have that sort of method on a corresponding view model that gets populated with the state of a Purchase (or even populate a view model field directly with the formatted date string).

bug11