1

I'm using Hibernate. From the UI I'm getting the datetime as 2019-04-29 19:00:00. The same value will be stored in my Oracle database but it is being saved as 2019-04-29 07:00:00.

In Database camp_start_time Datatype is Timestamp

Adding code snippet:

{ "camp_id":292,"camp_name": "Tata","camp_desc":"Tata","camp_type": 1,"camp_start_time":"2019-04-29 19:00:00"} @Entity public class Campaign_Sms implements Serializable{ @Column(name = "CAMP_START_TIME") private Date camp_start_time; } 
1
  • Adding code snippet: { "camp_id":292,"camp_name":"Tata","camp_desc":"Tata","camp_type": 1,"camp_start_time":"2019-04-29 19:00:00"} @Entity public class Campaign_Sms implements Serializable{ @Column(name = "CAMP_START_TIME") private Date camp_start_time; } Commented Apr 29, 2019 at 5:45

2 Answers 2

2

Try the @Temporal(TemporalType.TIMESTAMP) annotation with it.

@Temporal(TemporalType.TIMESTAMP) private Date camp_start_time; 

TemporalType.DATE annotation value omit the time, as well as TemporalType.TIME exclude the date. Check the document here.

UPDATE

Output format should be this yyyy-MM-dd hh:mm:ss a and if output required as it is format should be yyyy-MM-dd HH:mm:ss.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks sachith for reply.Still I could not able to get SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); java.util.Date date1 = sdf.parse(dt); System.out.println("After SimpleDateFormat --"+date1); String pattrn=sdf.format(date1); System.out.println("After Apply pattern --"+pattrn); O/P: After SimpleDateFormat --Mon Apr 29 19:00:00 IST 2019 After Apply pattern --2019-04-29 07:00:00
if you're parsing 2019-04-29 19:00:00, your input pattern should be yyyy-MM-dd HH:mm:ss. Once you use yyyy-MM-dd hh:mm:ss (note simple hh) pattern, it won't support 24h based time format and only support 12h time. Then you have to use AM/PM in the time. So, in order to support that new output, format should be yyyy-MM-dd hh:mm:ss a and it's give you 2019-04-29 07:00:00 PM
After updating input pattern yyyy-MM-dd HH:mm:ss and TemporalType.TIMESTAMP, still record is being persisted as 29/APR/19 7:00:00. PM.Could you please help me on this
If the database keeping in that format, I think, that won't be a problem as long as when you fetch back from the DB your time correctly being fetched without any data lose. I guess you database field data type is DATETIME or something (depends on the database). If that so, you don't need to worry about the value in the database as long you get the right value out. Just check with your program saving an fetching a value whether you get the same value as you inserted.
0

use the @Temporal(TemporalType.DATE) annotation with it.

@Column @Temporal(TemporalType.DATE) private Date camp_start_time; 

1 Comment

Thanks Rajesh for reply.Still I could not able to get SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); java.util.Date date1 = sdf.parse(dt); System.out.println("After SimpleDateFormat --"+date1); String pattrn=sdf.format(date1); System.out.println("After Apply pattern --"+pattrn); O/P: After SimpleDateFormat --Mon Apr 29 19:00:00 IST 2019 After Apply pattern --2019-04-29 07:00:00

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.