1年4个季度,每个季度3个月,每季度第一个月5周,其余4周,1年包括52周(544,544,544,544排列) 
想实现如:传入2008-12-29 得到 2009年1月1周、传入2009-12-27 得到 2009年12月52周、传入2009-12-28 得到 2010年1月1周 的算法。calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.setMinimalDaysInFirstWeek(4);
均不能满足需求。

解决方案 »

  1.   

    已实现通过日期得到自定义日历中的周的算法如下:public static int customWeek(Date date){
    Calendar calendar = Calendar.getInstance();
    calendar.setFirstDayOfWeek(Calendar.MONDAY);
    calendar.setMinimalDaysInFirstWeek(1);
    calendar.setTime(date);
    int week = calendar.get(Calendar.WEEK_OF_YEAR);
    return week;

    求月及年比较好的算法。(目前的算法很不优雅且没有考虑闰年,很多if else 而且可读性差,希望能整合至此Calendar对象中直接获得)
      

  2.   


    import java.util.*;
    import java.text.*;public class Test38
    {
    public static void main(String... args) throws ParseException{
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    Date d = df.parse("2009-1-10");
    System.out.println(getC(d));
    } public static String getC(Date d) {
    StringBuilder sb = new StringBuilder();
    Calendar c = Calendar.getInstance();
    c.setFirstDayOfWeek(Calendar.MONDAY); 
    c.setTime(d);
    int weekNum = c.get(Calendar.WEEK_OF_YEAR);
    if(weekNum == 1) 
    if(c.get(Calendar.MONTH) == 11) {
    c.roll(Calendar.MONTH,1);
    c.roll(Calendar.YEAR,1);
    } sb.append(c.get(Calendar.YEAR) + "年");
    sb.append(c.get(Calendar.MONTH) + 1 + "月");
    sb.append(weekNum + "周");
    return sb.toString(); }
    }