问题描述:
    1、每个员工的周历编号格式为:姓名_部门_年月_第x周。例如:狄仁杰_开发部_201010_第1周。
    2、每个周历的开始时间(startTime)和结束时间(endTime),均是每周的星期一和星期日所对应的时间。例如:2010-09-27(星期一)至2010-10-03(星期日)。
    3、本月末和下月初在同一个星期内,如果本月末的天数大于等于四天,则算本月的最后一周;反之,如果下月初的天数大于等于四天,则算下月的第一个周。例如:2010-09-27(星期一)至2010-10-03(星期日),9月末有四天在这个星期内,则该周历编号为:狄仁杰_开发部_201009_第5周。又如:2010-08-30(星期一)至2010-09-05(星期日),9月初有四天在这个周,则该周历编号为:狄仁杰_开发部_201009_第1周。
项目要求:
    1、封装一个方法,从前台获取开始时间(startTime)和结束时间(endTime),传给该方法,即可实现规定格式的周历编号。
    2、由于姓名和部门,是我在后台获取的,所以只需实现:“年月_第x周”这部分,即可。
求助理由:
    我已经折腾好几个小时了,对日期的运算,是在把握不好。望好心人相助一二。先谢谢了!

解决方案 »

  1.   

    public static void test6() throws ParseException {
    String s = "2010-09-27";
    String e = "2010-10-03";
    String DATE_TIME_FORMAT = "yyyy-MM-dd";
    String DATE_TIME_FORMAT2 = "yyyyMM";
    DateFormat format = new SimpleDateFormat(DATE_TIME_FORMAT);
    DateFormat format2 = new SimpleDateFormat(DATE_TIME_FORMAT2);
    Date startTime = format.parse(s);
    Date endTime = format.parse(e);
    long time = (endTime.getTime() - startTime.getTime())/2 + startTime.getTime();
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(time);
    //201009_第1周
    System.out.println(format.format(cal.getTime()));
    System.out.println(format2.format(cal.getTime()) + "_第" + (cal.get(Calendar.WEEK_OF_MONTH)) + "周");
    }
      

  2.   

    Calendar  有空看看这个类java.util.Calendar
      

  3.   

    首先,衷心的感谢菜菜小宝的帮助。
    其次,我把我整理后的方法,发上来,做个纪念。
    /**
     * @method   calculateDateOfThisMonthIsWeeks
     * @param startTime 开始时间,星期一
     * @param endTime 结束时间,星期日
     * @return
     * @throws ParseException
     * @方法描述:根据开始时间和结束时间,确定周
     */
    public static String calculateDateOfThisMonthIsWeeks(String startTime, String endTime) throws ParseException {
    if(startTime == null || endTime == null) {
    return null;
    }

    Calendar cal = Calendar.getInstance();

    SimpleDateFormat format2 = new SimpleDateFormat("yyyyMM");

    Date start = GeneralApproachDateAndTime.convertStringAsDate(startTime);
    Date end = GeneralApproachDateAndTime.convertStringAsDate(endTime);

    cal.setTime(start);

    int s = cal.get(Calendar.DAY_OF_WEEK);

    cal.setTime(end);

    int e = cal.get(Calendar.DAY_OF_WEEK);

    if(s == 2 && e == 1) {
    long time = (end.getTime() - start.getTime())/2 + start.getTime();

    System.out.println((end.getTime() - start.getTime())/2/86400000);

    cal.setTimeInMillis(time);
    System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime()));
    String weekDay = format2.format(cal.getTime()) + "_第" + (cal.get(Calendar.WEEK_OF_MONTH)) + "周";

    return weekDay.toString();
    } else {
    System.out.println("开始时间不为星期一,或者结束时间不为星期日");

    return null;
    }
    }/**
     * @method   formatCurrentDate
     * @param date
     * @return
     * @throws ParseException
     * @方法描述:将string转换为date
     */
    public static Date convertStringAsDate(String date) throws ParseException {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    Date d = sdf.parse(date);

    return d;
    }
      

  4.   


    /**
     * 
     * @param startTime - 某周的第一天日期
     * @throws ParseException
     */
    public static void test6(Date startTime) throws ParseException {
    //参数合法性判断
    if (startTime == null) {
    //抛异常
    //返回null
    return;
    }
    Calendar cal = Calendar.getInstance();
    cal.setTime(startTime);
    if (cal.get(Calendar.DAY_OF_WEEK) != 1) {
    //开始时间错误,不是某周的第一天
    //抛异常
    //返回null
    return;
    }
    String DATE_TIME_FORMAT = "yyyyMM"; //在类中定义为常量
    long oneDay = 24 * 3600 * 1000L; //在类中定义为常量

    DateFormat format = new SimpleDateFormat(DATE_TIME_FORMAT);
    long time = startTime.getTime() + 3 * oneDay; //开始时间+3天,为此周时间中间点
    cal.setTimeInMillis(time);
    System.out.println(format.format(cal.getTime()) + "_第" + (cal.get(Calendar.WEEK_OF_MONTH)) + "周");
    }
      

  5.   

    原来这个处理还可以更简单些:public static String getTheWeekOfDay(Date startDate) {
    if (startDate == null) {
    return null;
    }
    Calendar cal = Calendar.getInstance();
    cal.setTime(startDate);
    cal.set(Calendar.DAY_OF_WEEK, 1);
    DateFormat format = new SimpleDateFormat("yyyyMM");
    return format.format(cal.getTime()) + "_第" + (cal.get(Calendar.WEEK_OF_MONTH)) + "周";
    }