- import java.util.Calendar;
- import java.util.Date;
- public class DateUtil {
- /**
- * 凌晨
- * @param date
- * @flag 0 返回yyyy-MM-dd 00:00:00日期<br>
- * 1 返回yyyy-MM-dd 23:59:59日期
- * @return
- */
- public static Date weeHours(Date date, int flag) {
- Calendar cal = Calendar.getInstance();
- cal.setTime(date);
- int hour = cal.get(Calendar.HOUR_OF_DAY);
- int minute = cal.get(Calendar.MINUTE);
- int second = cal.get(Calendar.SECOND);
- //时分秒(毫秒数)
- long millisecond = hour*60*60*1000 + minute*60*1000 + second*1000;
- //凌晨00:00:00
- cal.setTimeInMillis(cal.getTimeInMillis()-millisecond);
- if (flag == 0) {
- return cal.getTime();
- } else if (flag == 1) {
- //凌晨23:59:59
- cal.setTimeInMillis(cal.getTimeInMillis()+23*60*60*1000 + 59*60*1000 + 59*1000);
- }
- return cal.getTime();
- }
- public static void main(String[] args) {
- System.out.println(weeHours(new Date(), 0));
- System.out.println(weeHours(new Date(), 1));
- }
- }
Date日期转化
最新推荐文章于 2023-09-20 15:27:26 发布