How to get today's Date?

后端 未结 8 2021
予麋鹿
予麋鹿 2021-01-31 14:28

In other words, I want functionality that provides Joda-Time:

today = today.withTime(0, 0, 0, 0);

but without Joda-Time, only with java.util.Da

8条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-31 15:15

    If you want midnight (0:00am) for the current date, you can just use the default constructor and zero out the time portions:

    Date today = new Date();
    today.setHours(0); today.setMinutes(0); today.setSeconds(0);
    

    edit: update with Calendar since those methods are deprecated

    Calendar today = Calendar.getInstance();
    today.clear(Calendar.HOUR); today.clear(Calendar.MINUTE); today.clear(Calendar.SECOND);
    Date todayDate = today.getTime();
    

提交回复
热议问题