Set time to 00:00:00

前端 未结 12 2172
时光说笑
时光说笑 2020-11-29 20:04

I have a problem resetting hours in Java. For a given date I want to set the hours to 00:00:00.

This is my code :

/**
     * Resets milliseconds, se         


        
12条回答
  •  悲哀的现实
    2020-11-29 20:49

    If you need format 00:00:00 in string, you should use SimpleDateFormat as below. Using "H "instead "h".

    Date today = new Date();
    SimpleDateFormat ft = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss"); 
    //not SimpleDateFormat("dd-MM-yyyy hh:mm:ss")
    Calendar calendarDM = Calendar.getInstance();
    calendarDM.setTime(today);
    calendarDM.set(Calendar.HOUR, 0);
    calendarDM.set(Calendar.MINUTE, 0);
    calendarDM.set(Calendar.SECOND, 0);
    System.out.println("Current Date: " + ft.format(calendarDM.getTime()));
    
    //Result is: Current Date: 29-10-2018 00:00:00
    

提交回复
热议问题