how to convert string into time format and add two hours

后端 未结 9 1133
轻奢々
轻奢々 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条回答
  •  情书的邮戳
    2020-12-13 05:28

    This will give you the time you want (eg: 21:31 PM)

    //Add 2 Hours to just TIME
    SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss a");
    Date date2 = formatter.parse("19:31:51 PM");
    Calendar cal2 = Calendar.getInstance();
    cal2.setTime(date2);
    cal2.add(Calendar.HOUR_OF_DAY, 2);
    SimpleDateFormat printTimeFormat = new SimpleDateFormat("HH:mm a");
    System.out.println(printTimeFormat.format(cal2.getTime())); 
    

提交回复
热议问题