日期相关操作
程序员文章站
2022-05-02 15:53:01
...
代码在java8测试过,默认返回的日期都是零点时间戳
1. 获取某一周的第一天
public long getWeekMonday(long millis) { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(millis); // 设置一周的第一天是星期一 cal.setFirstDayOfWeek(Calendar.MONDAY); cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); return cal.getTimeInMillis(); }
2. 获取某一周的最后一天
public long getWeekSunday(long millis) { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(millis); // 设置一周的第一天是星期一 cal.setFirstDayOfWeek(Calendar.MONDAY); cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); return cal.getTimeInMillis(); }
3. 获取某个月的第一天
public long getFirstDayOfMonth(long millis) { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(millis); cal.set(Calendar.DAY_OF_MONTH, 1); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); return cal.getTimeInMillis(); }
4. 获取某个月的最后一天
public long getLastDayOfMonth(long millis) { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(millis); cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH)); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); return cal.getTimeInMillis(); }
5. 获取某个季度第一天
public long getFirstDayOfQuarter(long millis) { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(millis); int month = cal.get(Calendar.MONTH); int firstMonth = month / 3 * 3; cal.set(Calendar.MONTH, firstMonth); cal.set(Calendar.DAY_OF_MONTH, 1); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); return cal.getTimeInMillis(); }
6. 获取每个季度最后一天
public long getLastDayOfQuarter(long millis) { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(millis); int month = cal.get(Calendar.MONTH); int lastMonth = month / 3 * 3 + 2; cal.set(Calendar.MONTH, lastMonth); cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH)); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); return cal.getTimeInMillis(); }
7. 获取某一年第一天
public long getFirstDayOfYear(long millis) { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(millis); cal.set(Calendar.MONTH, Calendar.JANUARY); cal.set(Calendar.DAY_OF_MONTH, 1); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); return cal.getTimeInMillis(); }
8. 获取某一年最后一天
public long getLastDayOfYear(long millis) { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(millis); cal.set(Calendar.MONTH, Calendar.DECEMBER); cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH)); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); return cal.getTimeInMillis(); }
上一篇: Java中Calendar的坑
下一篇: GNOME 2.23.3 稳定版