How to get today's Date?

后端 未结 8 1931
予麋鹿
予麋鹿 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();
    
    0 讨论(0)
  • 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()

    0 讨论(0)
提交回复
热议问题