2

forum member

I am having one problem with date time in java. Actually I am receiving the startdate in format 2012-02-27T01:10:10 and I want to insert the received date to my database having datetime datatype.

Actually I tried to convert the startdate received to datetime by below code

String sDate = jsonObject.get("StartDate").toString();
String eDate = jsonObject.get("EndDate").toString();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
Date startD = sdf.format(sDate);
Date endD = sdf.format(eDate);

but with the above code only date gets added to my database like 2012-02-27 00:00:00

I want to add the time also to my database but when I change the SimpleDateFormat to SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); nothing works.

please suggest me some solution I can apply so my time also gets added to database. I am using Hibernate JPA as my persistence layer.

Yogendra Singh
  • 553
  • 4
  • 12
  • 33

5 Answers5

3

SimpleDateFormat's format() method doesn't return a Date type.

try this:

Date startDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse(sDate);
Kent
  • 189,393
  • 32
  • 233
  • 301
0

Try this,

yyyy-MM-dd'T'HH:mm:ss
Vaandu
  • 4,857
  • 12
  • 49
  • 75
0

you can try like this....

DateFormat format = new SimpleDateFormat("MMddyyHHmmss");
Date date = format.parse("022310141505");
kleopatra
  • 51,061
  • 28
  • 99
  • 211
0
String sDate = jsonObject.get("StartDate").toString();
String eDate = jsonObject.get("EndDate").toString();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
Date startD = sdf.format(sDate);
Timestamp startTime = new Timestamp(startD.getTime());

Date endD = sdf.format(eDate);
Timestamp endTime = new Timestamp(endD.getTime());
Sunil Kumar B M
  • 2,735
  • 1
  • 24
  • 31
0

Of course only the date is parsed, since the pattern you provided to the SimpleDateFormat constructor only contains the date part! Add the time part to it and it will parse the time too just fine.

vagelis
  • 334
  • 3
  • 9