计算某年某季度某月某周某日的起始结束时间戳

时间:2022-01-26 17:22:57
    public static Long[] getTimeCondition(String createdTimebefore,String createdTimeafter){
        boolean isBeforeTimeNotEmpty = !"".equals(createdTimebefore) && createdTimebefore!=null && !"null".equals(createdTimebefore);
        boolean isAfterTimeNotEmpty = !"".equals(createdTimeafter) && createdTimeafter!=null && !"null".equals(createdTimeafter);

        Long _createdTimebefor=null;
        Long _createdTimeafter=null;

        //如果都是空,返回90天内的时间戳
        if(!isBeforeTimeNotEmpty && !isAfterTimeNotEmpty){
            //返回90天内的时间戳
            Date date = new Date();
            _createdTimeafter = date.getTime();
            _createdTimebefor = DateUtils.addDays(date, SystemCode.HISTORY_ORDER_BEFORE_DAYS).getTime();

            Long[] result = {_createdTimebefor,_createdTimeafter};
            return result;
        }

        if(isBeforeTimeNotEmpty)
        {
            _createdTimebefor=DateTools.toLong(DateTools.yyyy_MM_dd_HH_mm_ss,createdTimebefore.trim()+" 00:00:00");
        }

        if(isAfterTimeNotEmpty)
        {
            _createdTimeafter=DateTools.toLong(DateTools.yyyy_MM_dd_HH_mm_ss,createdTimeafter.trim()+" 23:59:59");
        }else {
            _createdTimeafter=DateTools.toLong(DateTools.yyyy_MM_dd_HH_mm_ss,createdTimebefore.trim()+" 23:59:59");;
        }

        Long[] result = {_createdTimebefor,_createdTimeafter};
        return result;
    }

    // 获取当前时间所在年的周数
    public static int getWeekOfYear(Date date) {
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setMinimalDaysInFirstWeek(7);
        c.setTime(date);

        return c.get(Calendar.WEEK_OF_YEAR);
    }

    // 获取当前时间所在年的最大周数
    public static int getMaxWeekNumOfYear(int year) {
        Calendar c = new GregorianCalendar();
        c.set(year, Calendar.DECEMBER, 31, 23, 59, 59);

        return getWeekOfYear(c.getTime());
    }

    //	日/周/月/季度/年
//	day/week/month/quarter/year
//	日:2016-07-15
//	周:2016,20   2016年第20周
//	月:2016-06
//	季度:2016,1  2016年第1季度
//	年:2016

//    public static void main(String[] args) {
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        long[] long1 = getBeginEndTimestamp("2016-07-15",day);
//        long[] long2 = getBeginEndTimestamp("2016-20",week);
//        long[] long3 = getBeginEndTimestamp("2016-06",month);
//        long[] long4 = getBeginEndTimestamp("2016-2",quarter);
//        long[] long5 = getBeginEndTimestamp("2016",year);
//
//        System.out.println(sdf.format(new Date(long1[0]))+ "   end : " + sdf.format(new Date(long1[1])));
//        System.out.println(sdf.format(new Date(long2[0]))+ "   end : " + sdf.format(new Date(long2[1])));
//        System.out.println(sdf.format(new Date(long3[0]))+ "   end : " + sdf.format(new Date(long3[1])));
//        System.out.println(sdf.format(new Date(long4[0]))+ "   end : " + sdf.format(new Date(long4[1])));
//        System.out.println(sdf.format(new Date(long5[0]))+ "   end : " + sdf.format(new Date(long5[1])));
//    }
}