1、Calendar是Java提供的一个util类,在java.util中。我们在开发过程中经常遇到,需要对时间进行处理的方式。而Java中Calendar给我们提供了很好的控制方式。
2、我这里主要是介绍几种Calendar的操作方式来获取不同时间
public class CalendarUtil { /** * 获取当月第一天开始时间和最后一天结束时间 * @return */ public final static List<Long> getMonthStartAndEnd(){ Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.MONTH,0); calendar.set(Calendar.DAY_OF_MONTH,1); calendar.set(Calendar.HOUR_OF_DAY,0); calendar.set(Calendar.MINUTE,0); calendar.set(Calendar.SECOND,0); calendar.set(Calendar.MILLISECOND,0); Long firstDay = calendar.getTime().getTime(); calendar.add(Calendar.MONTH,1); Long lastDay = calendar.getTime().getTime()-1; List<Long> times = new ArrayList<>(); times.add(firstDay); times.add(lastDay); return times; } /** * 获取当天的开始时间 * @return */ public final static Long getDayStart(){ Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.MONTH,0); calendar.set(Calendar.HOUR_OF_DAY,0); calendar.set(Calendar.MINUTE,0); calendar.set(Calendar.SECOND,0); calendar.set(Calendar.MILLISECOND,0); return calendar.getTime().getTime(); } /** * 获取某天的开始时间 * @param day * @return */ public final static Long getDayStart(Integer day){ Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.MONTH,0); calendar.set(Calendar.HOUR_OF_DAY,0); calendar.set(Calendar.MINUTE,0); calendar.set(Calendar.SECOND,0); calendar.set(Calendar.MILLISECOND,0); calendar.set(Calendar.DAY_OF_MONTH,day.intValue()); return calendar.getTime().getTime(); } /** * 获取某天的结束时间 * @param day * @return */ public final static Long getDayEnd(Integer day){ Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.MONTH,0); calendar.set(Calendar.HOUR_OF_DAY,0); calendar.set(Calendar.MINUTE,0); calendar.set(Calendar.SECOND,0); calendar.set(Calendar.MILLISECOND,0); calendar.set(Calendar.DAY_OF_MONTH,day.intValue()+1); return calendar.getTime().getTime()-1; } /** * 获取本月或者次月某天的结束时间 * @param month * @param day * @return */ public final static Long getDayEnd(Integer month,Integer day){ Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.MONTH,month.intValue()); calendar.set(Calendar.HOUR_OF_DAY,0); calendar.set(Calendar.MINUTE,0); calendar.set(Calendar.SECOND,0); calendar.set(Calendar.MILLISECOND,0); calendar.set(Calendar.DAY_OF_MONTH,day.intValue()+1); return calendar.getTime().getTime()-1; } /** * 获取本季度的开始时间和结束时间 * @return */ public final static List<Long> getQuarterStartAndEnd(){ //获取时间 Calendar calendar = Calendar.getInstance(); //判读当前时间是哪个季度 int month = calendar.get(Calendar.MONTH)+1; Integer quarter = 0; if (month>=1 && month <=3) { quarter = 1; } else if (month>=4 && month <=6) { quarter = 2; } else if (month>=7 && month <=9) { quarter = 3; } else if (month>=10 && month <=12) { quarter = 4; } calendar.set(Calendar.MONTH,3*(quarter.intValue()-1)); calendar.set(Calendar.DAY_OF_MONTH,1); calendar.set(Calendar.HOUR_OF_DAY,0); calendar.set(Calendar.MINUTE,0); calendar.set(Calendar.SECOND,0); calendar.set(Calendar.MILLISECOND,0); Long firstDay = calendar.getTime().getTime(); calendar.add(Calendar.MONTH,3); Long lastDay = calendar.getTime().getTime()-1; List<Long> times = new ArrayList<>(); times.add(firstDay); times.add(lastDay); return times; } /** * 获取每个季度第某个月,第某个天数的开始时间 * @return */ public final static List<Long> getQuarterStart(Integer month,Integer day){ List<Long> times = new ArrayList<>(); for (int i = 1; i <= 4; i++) { //获取时间 Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.MONTH,3*(i-1)-1+month.intValue()); calendar.set(Calendar.DAY_OF_MONTH,day); calendar.set(Calendar.HOUR_OF_DAY,0); calendar.set(Calendar.MINUTE,0); calendar.set(Calendar.SECOND,0); calendar.set(Calendar.MILLISECOND,0); times.add(calendar.getTime().getTime()); } return times; } /** * 获取某个季度某个月,某天的结束时间 * @return */ public final static Long getQuarterEnd(Integer month,Integer day){ //获取时间 Calendar calendar = Calendar.getInstance(); //判读当前时间是哪个季度 int currentMonth = calendar.get(Calendar.MONTH)+1; Integer quarter = 0; if (currentMonth>=1 && currentMonth <=3) { quarter = 1; } else if (currentMonth>=4 && currentMonth <=6) { quarter = 2; } else if (currentMonth>=7 && currentMonth <=9) { quarter = 3; } else if (currentMonth>=10 && currentMonth <=12) { quarter = 4; } calendar.set(Calendar.MONTH,3*(quarter.intValue()-1)+month.intValue()); calendar.set(Calendar.DAY_OF_MONTH,day+1); calendar.set(Calendar.HOUR_OF_DAY,0); calendar.set(Calendar.MINUTE,0); calendar.set(Calendar.SECOND,0); calendar.set(Calendar.MILLISECOND,0); return calendar.getTime().getTime()-1; } /** * 获取当年的开始时间和结束时间 * @return */ public final static List<Long> getYearStartAndEnd(){ Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.MONTH,0); calendar.set(Calendar.DAY_OF_MONTH,1); calendar.set(Calendar.HOUR_OF_DAY,0); calendar.set(Calendar.MINUTE,0); calendar.set(Calendar.SECOND,0); calendar.set(Calendar.MILLISECOND,0); Long firstDay = calendar.getTime().getTime(); calendar.add(Calendar.MONTH,12); Long lastDay = calendar.getTime().getTime()-1; List<Long> times = new ArrayList<>(); times.add(firstDay); times.add(lastDay); return times; } /** * 获取当前年第某个月,第某个天数的开始时间 * @return */ public final static Long getYearStart(Integer month,Integer day){ //获取时间 Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.MONTH,month.intValue()-1); calendar.set(Calendar.DAY_OF_MONTH,day.intValue()); calendar.set(Calendar.HOUR_OF_DAY,0); calendar.set(Calendar.MINUTE,0); calendar.set(Calendar.SECOND,0); calendar.set(Calendar.MILLISECOND,0); return calendar.getTime().getTime(); } /** * 获取当前年第某个月,第某个天数的结束时间 * @return */ public final static Long getYearEnd(Integer month,Integer day){ //获取时间 Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.MONTH,month.intValue()-1); calendar.set(Calendar.DAY_OF_MONTH,day.intValue()+1); calendar.set(Calendar.HOUR_OF_DAY,0); calendar.set(Calendar.MINUTE,0); calendar.set(Calendar.SECOND,0); calendar.set(Calendar.MILLISECOND,0); return calendar.getTime().getTime()-1; } }