Vpp Man
Vpp Man

Reputation: 2546

Mysql datetime to Calendar

My code:

Calendar c = Calendar.getInstance();    
c.setTime(res.getDate("my_date")); //res is ResultSet

System.out.println(c.getTime());
System.out.println(res.getString("my_date"));

Output:

Thu Oct 11 00:00:00 IST 2012 
2012-10-11 02:50:00.0

In calendar, it is not considering the time part. I used Calendar because I have to make some comparisons.

Why time part is omitted?

Upvotes: 1

Views: 2257

Answers (1)

alain.janinm
alain.janinm

Reputation: 20065

You should use something like :

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
out.println(sdf.format(rs.getTimestamp("your_date_field").getTime()));

By the way, you have to create a SimpleDateFormat that fit your SQL date format.

Upvotes: 2

Related Questions