获取年、季度、周的开始日期和结束日期工具类
程序员文章站
2022-05-02 15:52:19
...
package com.hogwartsrow.test; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; /** * 复杂得日期工具类 * @author HogwartsRow * */ public class MOEDateUtils { /** * 获取某周得第一天 * @param year 年 * @param week 周 * @return */ public static Date getFirstDayOfWeek(int year, int week){ Calendar c = new GregorianCalendar(); c.set(Calendar.YEAR, year); c.set(Calendar.WEEK_OF_YEAR, week); c.setFirstDayOfWeek(Calendar.MONDAY); c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); c.set(Calendar.HOUR, 0); c.set(Calendar.MINUTE, 0); c.set(Calendar.SECOND, 0); return c.getTime(); } /** * 获取某周得最后一天 * @param year 年 * @param week 周 * @return */ public static Date getLastDayOfWeek(int year, int week){ Calendar c = new GregorianCalendar(); c.set(Calendar.YEAR, year); c.set(Calendar.WEEK_OF_YEAR, week); c.setFirstDayOfWeek(Calendar.MONDAY); c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); c.set(Calendar.HOUR, 23); c.set(Calendar.MINUTE, 59); c.set(Calendar.SECOND, 59); return c.getTime(); } /** * 获取某月得第一天 * @param year 年 * @param month 月 * @return */ public static Date getFirstDayOfMonth(int year, int month){ Calendar c = new GregorianCalendar(); c.set(year, month, 1); c.set(Calendar.HOUR, 0); c.set(Calendar.MINUTE, 0); c.set(Calendar.SECOND, 0); return c.getTime(); } /** * 获取某月得最后一天 * @param year 年 * @param month 月 * @return */ public static Date getLastDayOfMonth(int year, int month){ Calendar c = new GregorianCalendar(); c.set(year, month, 1); c.roll(Calendar.DATE, -1); c.set(Calendar.HOUR, 23); c.set(Calendar.MINUTE, 59); c.set(Calendar.SECOND, 59); return c.getTime(); } /** * 获取某季度得第一天 * @param year 年 * @param quarter 季度 * @return */ public static Date getFirstDayOfQuarter(int year, int quarter){ Calendar c = new GregorianCalendar(); int month = 0; switch (quarter) { case 1: month = 1-1; break; case 2: month = 4-1; break; case 3: month = 7-1; break; case 4: month = 10-1; break; default: month = c.get(Calendar.MONTH); break; } return getFirstDayOfMonth(year, month); } /** * 获取某季度得最一天 * @param year 年 * @param quarter 季度 * @return */ public static Date getLastDayOfQuarter(int year, int quarter){ Calendar c = new GregorianCalendar(); int month = 0; switch (quarter) { case 1: month = 3-1; break; case 2: month = 6-1; break; case 3: month = 9-1; break; case 4: month = 12-1; break; default: month = c.get(Calendar.MONTH); break; } return getLastDayOfMonth(year, month); } /** * 获取某年得第一天 * @param year 年 * @return */ public static Date getFirstDayOfYear(int year){ return getFirstDayOfMonth(year, 0); } /** * 获取某年得最后一天 * @param year 年 * @return */ public static Date getLastDayOfYear(int year){ return getLastDayOfMonth(year, 11); } public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("周一: " + getFirstDayOfWeek(2016, 25)); System.out.println("周末: " + getLastDayOfWeek(2016, 25)); System.out.println("月初:" + getFirstDayOfMonth(2016, 5)); System.out.println("月末:" + getLastDayOfMonth(2016, 4)); System.out.println("季初:" + getFirstDayOfQuarter(2016, 1)); System.out.println("季末:" + getLastDayOfQuarter(2016, 2)); System.out.println("年初:" + getFirstDayOfYear(2016)); System.out.println("年末:" + getLastDayOfYear(2016)); } }
下一篇: Java中util包大总结