DECEMBER 11
/** * 取得当天0点时间 2017-02-04 00:00:00 * @return 毫秒级时间戳 */ public static long getDayZero(long date) { Calendar calendar = Calendar.getInstance(); //设置为入参时间 calendar.setTimeInMillis(date); //取得当天的起始时间 calendar.set(Calendar.HOUR_OF_DAY,0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); //取得当天的结束时间 calendar.add(Calendar.DATE,1); calendar.add(Calendar.MILLISECOND,-1); return calendar.getTimeInMillis(); } /** * 入参时间所在月份的第一天 * @param date 入参时间 毫秒 * @return */ public static long getFirstDayOfMonth(long date){ Calendar calendar = Calendar.getInstance(); //设置为入参时间 calendar.setTimeInMillis(date); calendar.set(Calendar.DAY_OF_MONTH,1); //可以设置当天的起始时间或者结束时间 //起始时间 0点0分0秒0毫秒 calendar.set(Calendar.HOUR_OF_DAY,0); calendar.set(Calendar.MINUTE,0); calendar.set(Calendar.SECOND,0); calendar.set(Calendar.MILLISECOND,0); //结束时间 23点59分59秒999毫秒 calendar.set(Calendar.HOUR_OF_DAY,23); calendar.set(Calendar.MINUTE,59); calendar.set(Calendar.SECOND,59); calendar.set(Calendar.MILLISECOND,999); return calendar.getTimeInMillis(); } /** * 入参时间所在月份的最后一天 有很多方式实现 * @param date 入参时间 毫秒 * @return */ public static long getLastDayOfMonth(long date){ Calendar calendar = Calendar.getInstance(); //设置为入参时间 calendar.setTimeInMillis(date); /*---------------------------------------------*/ //当前时间月份加一,设置天是月份的第一天,再减一天就是当月的最后一天 calendar.add(Calendar.MONTH,1); calendar.set(Calendar.DAY_OF_MONTH,1); calendar.add(Calendar.DATE,-1); //可以设置当天的起始时间或者结束时间 //起始时间 0点0分0秒0毫秒 calendar.set(Calendar.HOUR_OF_DAY,0); calendar.set(Calendar.MINUTE,0); calendar.set(Calendar.SECOND,0); calendar.set(Calendar.MILLISECOND,0); /*--------------------如果不关心具体时间也可以根据毫秒减一获得-------------------------*/ calendar.add(Calendar.MONTH,1); calendar.set(Calendar.DAY_OF_MONTH,1); //设置为当天的起始时间 0点0分0秒0毫秒 ,再将时间减掉一毫秒 calendar.set(Calendar.HOUR_OF_DAY,0); calendar.set(Calendar.MINUTE,0); calendar.set(Calendar.SECOND,0); calendar.set(Calendar.MILLISECOND,0); calendar.add(Calendar.MILLISECOND,-1); return calendar.getTimeInMillis(); } /** * 入参时间上个月份的最后一天 * @param date 入参时间 毫秒 * @return */ public static long getPreviousMonthLastDay(long date){ Calendar calendar = Calendar.getInstance(); //设置为入参时间 calendar.setTimeInMillis(date); calendar.set(Calendar.DAY_OF_MONTH,0); return calendar.getTimeInMillis(); } /** * 入参时间的前一天 * 获得当前时间的前几天,几分钟,几秒 直接使用 calendar.add()填写需要增加或者减少的属性 和值 即可 * @param date 入参时间 毫秒 * @return */ public static long getPreviousDay(long date){ Calendar calendar = Calendar.getInstance(); //设置为入参时间 calendar.setTimeInMillis(date); calendar.add(Calendar.DATE,-1); //calendar.add(Calendar.MONDAY,-1); //calendar.add(Calendar.YEAR,-1); //calendar.add(Calendar.HOUR,-1); //calendar.add(Calendar.MINUTE,-1); //calendar.add(Calendar.SECOND,-1); //calendar.add(Calendar.MILLISECOND,-1); return calendar.getTimeInMillis(); } /** * 入参时间是当月的第几个week * @param date * @return */ public static int getWeekOfMonth(long date){ Calendar calendar = Calendar.getInstance(); //设置为入参时间 calendar.setTimeInMillis(date); return calendar.get(Calendar.WEEK_OF_MONTH); } /** * 入参时间是当年的第几个week * @param date * @return */ public static int getWeekOfYear(long date){ Calendar calendar = Calendar.getInstance(); //设置为入参时间 calendar.setTimeInMillis(date); return calendar.get(Calendar.WEEK_OF_YEAR); } /** * 入参时间是当年的第几天 * @param date * @return */ public static int getDayOfYear(long date){ Calendar calendar = Calendar.getInstance(); //设置为入参时间 calendar.setTimeInMillis(date); return calendar.get(Calendar.DAY_OF_YEAR); } /** * 入参时间是当月的第几天 * @param date * @return */ public static int getDayOfMonth(long date){ Calendar calendar = Calendar.getInstance(); //设置为入参时间 calendar.setTimeInMillis(date); return calendar.get(Calendar.DAY_OF_MONTH); } /** * 获取前N个月的起始时间,n为0表示当前月,1表示前一个月 * @param n * @return 单位毫秒 */ public static long getLastNMonthStartTime(int n) { Calendar calendar = Calendar.getInstance(); 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); calendar.add(Calendar.MONTH, 0 - n); return calendar.getTimeInMillis(); } /** * 获取前N个月的终止时间,n为0表示当前月,1表示前一个月 * @param n * @return 单位毫秒 */ public static long getLastNMonthEndTime(int n) { Calendar calendar = Calendar.getInstance(); 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); calendar.add(Calendar.MONTH, 0 - n + 1); calendar.add(Calendar.MILLISECOND ,-1); return calendar.getTimeInMillis(); } public static void main(String[] args){ System.out.println(DateTimeUtil.getDateTimeString(getDayZero(System.currentTimeMillis()))); System.out.println(getWeekOfMonth(System.currentTimeMillis())); System.out.println(getWeekOfYear(System.currentTimeMillis())); System.out.println(getDayOfMonth(System.currentTimeMillis())); System.out.println(getDayOfYear(System.currentTimeMillis())); }
新闻热点
疑难解答