views:

753

answers:

1

Hello good people, I need some little help about a confusion i'm having. since i came from C# .NET i was expeting java to have something similar to C# DateTime. While i manage to create a helper method to convert string representation of date like eg "2009-10-16 11:14:34" to date object. here is the method

  public static Date convertToDateTime(String stringDate) { 
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date;
    try {
        date = df.parse(stringDate);
    } catch(ParseException e){
        e.printStackTrace();
    }
    return date;
 }

now i just want to persist now (i mean now date, like actual date) like New Date() and have that date in the database have the same representation yyyy-MM-dd HH:mm:s. Unless i'm not getting something fundamental

Since i'm using hibernate is it due to the mapping file? if so i have 2 question 1 How to then map my property so that New Date will be persited in this format yyyy-MM-dd HH:mm:s 2 How to have a default datetime mapping so if the property is null it still will be persited in the yyyy-MM-dd HH:mm:s format Thanks for reading

+1  A: 

You don't want your dates being stored in any format, you want them to be stored as a date.

This mapping definition could work (not sure about the default=now() part):

<property name="foo" not-null="false">
    <column name="foo" default="now()" not-null="true" sql-type="datetime" />
</property>
sfussenegger
hi thanks for the answer i'm using hibernate 3.3.2.GA and i don't thing there is sql-type property and value like datime.i have here time, date, timestamp, calendar ,calendar_date, and their prefixes with imm_ as typem
black sensei
the type depends on what you want to store in the database. only date, use type="data, only time, use "time", outherwise use type="timestamp"
Salandur
thanks a lot.Actually i've tried sfussenegger suggested and everything works except the default value.Salandur yours works as well so now i need how to do default value
black sensei