how to convert datetime to timestamp in java

17,013

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);
Share:
17,013
Yogendra Singh
Author by

Yogendra Singh

Updated on June 04, 2022

Comments

  • Yogendra Singh
    Yogendra Singh over 1 year

    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.