문제

Im receiving util.Date 's from twitter4j getCreatedAt() method.

Date d = twitter.getCreatedAt();
System.out.println(d);

Out: 'Mon Feb 17 00:10:34 PST 2014'

I'm trying to convert that to a sql.Date including the time. With the code I'm using right now, querying mysql from terminal shows a date like:

2014-02-16 00:00:00

Here's the code in the Tweet Model. (Using Play 2, btw)

@Id
public long id;
@Formats.DateTime(pattern="yyyy-MM-dd HH:mm:ss")
public Date date;

When a status comes in I construct it like so:

this.date = new java.sql.Date(status.getCreatedAt().getTime());

I think it could possibly be something to do with the time zones?

도움이 되었습니까?

해결책

java.sql.Date just keeps the normalized date, see this from Oracle documentation:

To conform with the definition of SQL DATE, the millisecond values wrapped by a java.sql.Date instance must be 'normalized' by setting the hours, minutes, seconds, and milliseconds to zero in the particular time zone with which the instance is associated.

If you want to use time with the date you have to use java.sql.Timestamp instead of java.sql.Date

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top