0

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

2
  • What field type is the field in the database? Typically, you would have a Date field in the database and it would not have any specific format associated with it. Commented Oct 16, 2009 at 11:40
  • it's a date field right now.But what do you thing about the sql-type that sfussenegger suggested Commented Oct 16, 2009 at 11:50

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

3 Comments

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 type m
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"
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.