how to convert string into time format and add two hours

后端 未结 9 1132
轻奢々
轻奢々 2020-12-13 05:04

I have the following requirement in the project.

I have a input field by name startDate and user enters in the format YYYY-MM-DD HH:MM:SS.

9条回答
  •  旧时难觅i
    2020-12-13 05:12

    This example is a Sum for Date time and Time Zone(String Values)

    String DateVal = "2015-03-26 12:00:00";
    String TimeVal = "02:00:00";
    
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm:ss");
    
    Date reslt = sdf.parse( DateVal );
    Date timeZ = sdf2.parse( TimeVal );
    //Increase Date Time
    reslt.setHours( reslt.getHours() + timeZ.getHours());
    reslt.setMinutes( reslt.getMinutes() + timeZ.getMinutes());
    reslt.setSeconds( reslt.getSeconds() + timeZ.getSeconds());
    
    System.printLn.out( sdf.format(reslt) );//Result(+2 Hours):  2015-03-26 14:00:00 
    

    Thanks :)

提交回复
热议问题