1. 程式人生 > >給一個時間獲取,今日凌晨、昨日凌晨、本週開始時間、本週結束時間、本月開始時間、本月結束時間

給一個時間獲取,今日凌晨、昨日凌晨、本週開始時間、本週結束時間、本月開始時間、本月結束時間

下面的時間預設是今日,如果是時間不是今日可以把new DateTime()替換成你的時間,但這個方法的前提型別是dataTime型別,結束後你可以toDate()轉換為date型別,希望這個筆記能幫到大家!



 Date todayTime = new DateTime().withTimeAtStartOfDay().toDate();//今天凌晨


 Date tomorrowTime = new DateTime().plusDays(1).withTimeAtStartOfDay().toDate();//昨日凌晨


Date theWeekStartTime = new DateTime().minusDays(new DateTime().getDayOfWeek()-1).withTimeAtStartOfDay().toDate();//本週開始時間



Date theWeekEndTime = new DateTime().plusDays(8-new DateTime().getDayOfWeek()).withTimeAtStartOfDay().toDate();//本週結束時間



Date theMonthStartTime = new DateTime().minusDays(new DateTime().getDayOfMonth()-1).withTimeAtStartOfDay().toDate();//本月開始時間



Date theMonthEndTime = new DateTime().minusDays(new DateTime().getDayOfMonth()-1).plusMonths(1).withTimeAtStartOfDay().toDate();//本月結束時間






如果有更好的方法,請不要默默離開