How to get today's Date?

后端 未结 8 2071
予麋鹿
予麋鹿 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:16

    Date today = new Date();
    today.setHours(0); //same for minutes and seconds
    

    Since the methods are deprecated, you can do this with Calendar:

    Calendar today = Calendar.getInstance();
    today.set(Calendar.HOUR_OF_DAY, 0); // same for minutes and seconds
    

    And if you need a Date object in the end, simply call today.getTime()

提交回复
热议问题