虽然java自带的Calendar类似乎很好用,但是作为小白的我还不会用。朋友说有时需要计算任意两个日期内具有几个星期一或者星期三。问我能不能实现,我想想,能实现,可能代码不够优雅。具体代码放在下面。欢迎朋友与我分享更好的思路或工具吧~
package cn.example.mapper; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class DateUtil { public static void main(String[] args) { try { System.out.println("有" + getMondayNumber("2017-02-03","2017-02-16",5) + "个"); } catch (ParseException e) { e.printStackTrace(); } } /** * 查询日期间有几天一周中的某一天 * 日期格式 yyyy-MM-dd yyyy-MM-dd 1-7(表示周一到周日) * @param startDay 准备查询的起始日期 * @param endDay 准备查询的结束日期 * @param dayOfWeek 准备查的一周中的某一天(准备查周几?) * @return 包含所查周几的天数 * @throws ParseException 不支持跨年查询、不支持结束日期早于起始日期、周几输入错误等 */ public static int getMondayNumber(String startDay,String endDay,int dayOfWeek) throws ParseException{ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); int differenceDay = 0; //转换起始日期 Date startDate = sdf.parse(startDay); //转换结束日期 Date endDate = sdf.parse(endDay); //实例化起始和结束Calendar对象 Calendar startCalendar = Calendar.getInstance(); Calendar endCalendar = Calendar.getInstance(); //分别设置Calendar对象的时间 startCalendar.setTime(startDate); endCalendar.setTime(endDate); //定义起始日期和结束日期分别属于第几周 int startWeek = startCalendar.get(Calendar.WEEK_OF_YEAR); int endWeek = endCalendar.get(Calendar.WEEK_OF_YEAR); //拿到起始日期是星期几 int startDayOfWeek = startCalendar.get(Calendar.DAY_OF_WEEK); if(startDayOfWeek == 1) { startDayOfWeek = 7; startWeek--; }else startDayOfWeek--; //拿到结束日期是星期几 int endDayOfWeek = endCalendar.get(Calendar.DAY_OF_WEEK); if(endDayOfWeek == 1) { endDayOfWeek = 7; endWeek--; }else endDayOfWeek--; //计算相差的周数 int differenceWeek = endWeek - startWeek; //开始计算 if(startDayOfWeek <= dayOfWeek) { if(endDayOfWeek >= dayOfWeek) differenceDay = differenceWeek + 1; }else if(startDayOfWeek > dayOfWeek) { if(endDayOfWeek < dayOfWeek) differenceDay = differenceWeek-1; }else { differenceDay = differenceWeek; } return differenceDay; } }
觉得没啥技术含量,就此记录下吧,有时间再优化下,比如能够跨年查询。
时间: 2024-10-25 20:31:51