简体   繁体   中英

Saving date in GMT format using Joda-time - Java

I collect Date from my Spring MVC/ Hibernate app using the following code:

String startMembershipDate = request.getParameter("startmembershipdate");

and I am using the following field definition for this field in my entity

@Column(name="MEMBERSHIP_DATE", nullable=false, unique=false)
private Date membershipDate;

So I was wondering if someone can please provide me with an example on how to convert the received date in the JSP to GMT format using Joda-time?

Also shall I change the field definition from Date to DateTime? Timestamp?

See this question how to convert between Date and DateTime : Convert from java.util.date to JodaTime

This question tells you how to get format the DateTime using GMT: JodaTime DateTime, ISO8601 GMT date format

You can change the field definition to DateTime if you need to. You just have to write a custom Hibernate mapper then. We do that here and it works well (especially since we save dates as DECIMAL(16) so we don't get stupid bugs because of odd SQL DATE implementations, driver bugs, ...).

java.sql.Timestamp is something else. It's derived from java.util.Date and only adds the notion of nano seconds - which introduces some new subtleties that you need to be aware of. In my code, I stay away from it.

But unless you have a good reason (like finding that you convert to DateTime all the time), leave it alone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM