博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.Date;
import java.util.regex.Pattern;
import java.util.StringTokenizer;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class VCalendar
{

    private int [] days = new int[42];
    private int [][]allDays = new int[6][7];
    
    private static final Locale DEFAULT_LOCALE = Locale.CHINESE;
    
    private static final String YEAR_SUFFIX = "年";
    
    private static final String MONTH_SUFFIX = "月";
    
    private static final String DAY_SUFFIX = "日";

    private Calendar currentCalendar;
    
    public int getFirstDayOfWeek()
    {
        Calendar cal = Calendar.getInstance(DEFAULT_LOCALE);
        cal.setTime( this.currentCalendar.getTime() );
        cal.set( Calendar.DATE, 1 );
        return  cal.get(Calendar.DAY_OF_WEEK);
    }
    
    public VCalendar(Calendar newCalendar)
    {
        if ( newCalendar != null )
        {
            this.currentCalendar = newCalendar;
        }
    }
    public VCalendar ()
    {
        this( Calendar.getInstance(DEFAULT_LOCALE));
    }
    
    public VCalendar(int year, int month, int day)
    {
        this();
        this.setYear(year);
        this.setMonth(month);
        this.setDay(day);
    }
    
    public void setYear(int year)
    {
        this.currentCalendar.set( Calendar.YEAR, year );
    }
    
    public void setMonth(int month)
    {
      this.currentCalendar.set( Calendar.MONTH, month - 1 );
    }
    
    public void setDay(int day)
    {
        this.currentCalendar.set( Calendar.DAY_OF_MONTH, day );
    }
    
    public int  getYear()
    {
        return this.currentCalendar.get(Calendar.YEAR);
    }
    
    public int  getMonth()
    {
        return this.currentCalendar.get(Calendar.MONTH) + 1;
    }
    
    public int getDay()
    {
        return this.currentCalendar.get(Calendar.DAY_OF_MONTH);
    }
    
    public int [] getDays()
    {
        return this.days;
    }
    public int [][] getAllDays()
    {
        return this.allDays;
    }
    public String getNextMonthString()
    {
        Calendar cal = Calendar.getInstance(DEFAULT_LOCALE);
        cal.setTime( this.currentCalendar.getTime() );
        cal.add(Calendar.MONTH, 1);
        return String.format("%d-%d-%d", cal.get(Calendar.YEAR),cal.get(Calendar.MONTH),cal.get(Calendar.DATE));
    }
    
    public String getPreMonthString()
    {
        Calendar cal = Calendar.getInstance(DEFAULT_LOCALE);
        cal.setTime( this.currentCalendar.getTime() );
        cal.add(Calendar.MONTH, -1);
        return   String.format("%d-%d-%d", cal.get(Calendar.YEAR),cal.get(Calendar.MONTH),cal.get(Calendar.DATE));
    }
    public void initDays()
    {
        for(int i = 0 ; i < this.days.length ; i++)
        {
            this.days[i]=0;
        }
    }
    public void setDays()
    {    
        this.initDays();
        int i = 0;
        int firstDayOfWeek = this.getFirstDayOfWeek();
        final int lastDayOfMonth = this.currentCalendar.getActualMaximum( Calendar.DAY_OF_MONTH );
        for (i=1; i <= lastDayOfMonth; firstDayOfWeek ++,i++)
        {
            this.days[firstDayOfWeek-1]=i;
        }
        for(i=0; i<6;i++)
        {
            for(int j=0;j<7;j++)
            {
                this.allDays[i][j]=this.days[(i+1)*j];
            }
        }
        int  j=-1;
        int k=0;
        for( i=0;i<this.days.length;i++)
        {
            if( i%7 == 0)
            {   j++;
               // System.out.println();
                k=0;
            }
            this.allDays[j][k]=this.days[i];
            k++;
            //System.out.print(this.days[i]+" \t");
        }
    }
    
    public void showDays()
    {
        int  j=-1;
        int k=0;
        for(int i=0;i<this.days.length;i++)
        {
            if( i%7 == 0)
            {   j++;
                System.out.println();
                k=0;
            }
            this.allDays[j][k]=this.days[i];
            k++;
            System.out.print(this.days[i]+" \t");
        }
        System.out.println();
        System.out.println("----------------------------------");
        for(int i=0; i<6;i++)
        {
            for( j=0;j<7;j++)
            {
               // this.allDays[i][j]=this.days[(i+1)*j];
                System.out.print(this.allDays[i][j]+" \t");
            }
            System.out.println();
        }
    }
    
    public String getCalendarString()
    {
        return String.format("%d%s%d%s%d%s", this.getYear(),YEAR_SUFFIX,this.getMonth(),MONTH_SUFFIX,this.getDay(),DAY_SUFFIX);
    }
    
    public static void main(String argc[])
    {
       VCalendar c = new VCalendar();
        //VCalendar c = new VCalendar(2010,6,14);
       // Calendar cal = Calendar.getInstance();
        //VCalendar c = new VCalendar(cal.get(Calendar.YEAR),cal.get(Calendar.MONTH)+1,Calendar.DATE);
        c.setDays();
        c.showDays();
        System.out.println();
        String test =c.getCalendarString();
        System.out.println(test);
        System.out.println(c.getFirstDayOfWeek());
       // System.out.println(Calendar.SATURDAY);
    }
}


  VCalendar vc = new VCalendar();
        vc.setDays();
        int a[][]= vc.getAllDays();
        ArrayList row1 =  new ArrayList<Integer>();
        for(int i=0;i<7;i++)
        {
            row1.add(new Integer(a[0][i]));
        }
        ArrayList row2 =  new ArrayList<Integer>();
        for(int i=0;i<7;i++)
        {
            row2.add(new Integer(a[1][i]));
        }
        ArrayList row3 =  new ArrayList<Integer>();
        for(int i=0;i<7;i++)
        {
            row3.add(new Integer(a[2][i]));
        }
        ArrayList row4 =  new ArrayList<Integer>();
        for(int i=0;i<7;i++)
        {
            row4.add(new Integer(a[3][i]));
        }
        ArrayList row5 =  new ArrayList<Integer>();
        for(int i=0;i<7;i++)
        {
            row5.add(new Integer(a[4][i]));
        }
        ArrayList row6 =  new ArrayList<Integer>();
        for(int i=0;i<7;i++)
        {
            row6.add(new Integer(a[5][i]));
            System.out.println(a[5][i]);
        }
        /*  add that list to a VelocityContext  */
        VelocityContext context = new VelocityContext();
        String curYear = vc.getCalendarString();
        String nextMonth = vc.getNextMonthString();
        String prevMonth = vc.getPreMonthString();
        
        context.put("petList", list);
        context.put("row1", row1);
        context.put("row2", row2);
        context.put("row3", row3);
        context.put("row4", row4);
        context.put("row5", row5);
        context.put("row6", row6);
        context.put("curYear", curYear);
        context.put("nextMonth", nextMonth);
        context.put("prevMonth", prevMonth); 

 

   #foreach( $pet in $petList )
    $pet.name for only $pet.price
  #end
   Call Today!      
   #macro( tablerows  $rows )
    #foreach( $row in $rows )
        <td >
        #if($row !=0)
        $row
        #end
        </td>
    #end
   #end
<table border=1>
<tr><td>prevMonth</td><td colspan=4>$curYear</td><td>nextMonth</td></tr>
#tablerows($row1)
<tr> #foreach( $something in $row2 )
        <td>#if($something !=0 )$something#end</td>
     #end
     
</tr>
<tr> #foreach( $something in $row3 )
        <td>#if($something !=0 )$something#end</td>
     #end
     
</tr>
<tr> #foreach( $something in $row4 )
        <td>#if($something !=0 )$something#end</td>
     #end
     
</tr>
<tr> #foreach( $something in $row5 )
        <td>#if($something !=0 )$something#end</td>
     #end
</tr>
<tr> #foreach( $something in $row6 )
        <td>#if($something !=0 )$something#end</td>
     #end
</tr>
</table>

posted on 2010-05-14 19:46  Likwo  阅读(536)  评论(0编辑  收藏  举报