package
test;
import
java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar; import java.util.List;
/**
* @author - yy
* @time - Dec 9, 2008 3:42:38 PM
*/
public class Test4 {
/**
* @param args
* @throws ParseException
*/
public static void main(String[] args) throws ParseException {
List < DayInfo > list = new Test4().genDayInfoList( 2008 , 12 );
System.out.println(list);
// 输出结果
// [< id = 1 name = 星期一 selected = false>
// , < id = 2 name = 星期二 selected = false> // , < id = 3 name = 星期三 selected = false>
// , < id = 4 name = 星期四 selected = false>
// , < id = 5 name = 星期五 selected = false>
// , < id = 6 name = 星期六 selected = false>
// , < id = 7 name = 星期日 selected = false>
// , < id = 8 name = 星期一 selected = false>
// , < id = 9 name = 星期二 selected = true>
// , < id = 10 name = 星期三 selected = false> // , < id = 11 name = 星期四 selected = false>
// , < id = 12 name = 星期五 selected = false>
// , < id = 13 name = 星期六 selected = false>
// , < id = 14 name = 星期日 selected = false>
// , < id = 15 name = 星期一 selected = false>
// , < id = 16 name = 星期二 selected = false>
// , < id = 17 name = 星期三 selected = false>
// , < id = 18 name = 星期四 selected = false>
// , < id = 19 name = 星期五 selected = false>
// , < id = 20 name = 星期六 selected = false>
// , < id = 21 name = 星期日 selected = false>
// , < id = 22 name = 星期一 selected = false>
// , < id = 23 name = 星期二 selected = false>
// , < id = 24 name = 星期三 selected = false>
// , < id = 25 name = 星期四 selected = false>
// , < id = 26 name = 星期五 selected = false>
// , < id = 27 name = 星期六 selected = false>
// , < id = 28 name = 星期日 selected = false>
// , < id = 29 name = 星期一 selected = false>
// , < id = 30 name = 星期二 selected = false>
// , < id = 31 name = 星期三 selected = false>
// ]
}
public List < DayInfo > genDayInfoList( int year, int month) throws ParseException {
Calendar calendar = GregorianCalendar.getInstance();
int curDay = calendar.get(Calendar.DATE);
calendar.setTime( new SimpleDateFormat( " yyyy-MM-dd " ).parse(year + " - " + month + " -01 " ));
List < DayInfo > list = new ArrayList < DayInfo > ();
int days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
int weekOfFirstDay = calendar.get(Calendar.DAY_OF_WEEK);
for ( int i = 1 ; i <= days; i ++ ) {
DayInfo dayInfo = new DayInfo();
if (curDay == i) {
dayInfo.setSelected( true );
}
dayInfo.setId(i);
dayInfo.setName( this .getWeekName((i + weekOfFirstDay - 2 ) % 7 ));
list.add(dayInfo);
}
return list;
}
private String getWeekName( int week) {
String[] weeks = new String[] { " 星期日 " , " 星期一 " , " 星期二 " , " 星期三 " , " 星期四 " , " 星期五 " , " 星期六 " };
return weeks[week];
}
}
class DayInfo {
private int id;
private String name;
private boolean selected = false ;
public int getId() {
return this .id;
}
public void setId( int id) {
this .id = id;
}
public String getName() {
return this .name;
}
public void setName(String name) {
this .name = name;
}
public boolean isSelected() {
return this .selected;
}
public void setSelected( boolean selected) {
this .selected = selected;
}
@Override
public String toString() { // AutoGenerate
StringBuilder sb = new StringBuilder();
sb.append( " < " );
sb.append( " id = " ).append( this .id);
sb.append( " name = " ).append( this .name);
sb.append( " selected = " ).append( this .selected);
sb.append( " >/n " );
return sb.toString();
}
}
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar; import java.util.List;
/**
* @author - yy
* @time - Dec 9, 2008 3:42:38 PM
*/
public class Test4 {
/**
* @param args
* @throws ParseException
*/
public static void main(String[] args) throws ParseException {
List < DayInfo > list = new Test4().genDayInfoList( 2008 , 12 );
System.out.println(list);
// 输出结果
// [< id = 1 name = 星期一 selected = false>
// , < id = 2 name = 星期二 selected = false> // , < id = 3 name = 星期三 selected = false>
// , < id = 4 name = 星期四 selected = false>
// , < id = 5 name = 星期五 selected = false>
// , < id = 6 name = 星期六 selected = false>
// , < id = 7 name = 星期日 selected = false>
// , < id = 8 name = 星期一 selected = false>
// , < id = 9 name = 星期二 selected = true>
// , < id = 10 name = 星期三 selected = false> // , < id = 11 name = 星期四 selected = false>
// , < id = 12 name = 星期五 selected = false>
// , < id = 13 name = 星期六 selected = false>
// , < id = 14 name = 星期日 selected = false>
// , < id = 15 name = 星期一 selected = false>
// , < id = 16 name = 星期二 selected = false>
// , < id = 17 name = 星期三 selected = false>
// , < id = 18 name = 星期四 selected = false>
// , < id = 19 name = 星期五 selected = false>
// , < id = 20 name = 星期六 selected = false>
// , < id = 21 name = 星期日 selected = false>
// , < id = 22 name = 星期一 selected = false>
// , < id = 23 name = 星期二 selected = false>
// , < id = 24 name = 星期三 selected = false>
// , < id = 25 name = 星期四 selected = false>
// , < id = 26 name = 星期五 selected = false>
// , < id = 27 name = 星期六 selected = false>
// , < id = 28 name = 星期日 selected = false>
// , < id = 29 name = 星期一 selected = false>
// , < id = 30 name = 星期二 selected = false>
// , < id = 31 name = 星期三 selected = false>
// ]
}
public List < DayInfo > genDayInfoList( int year, int month) throws ParseException {
Calendar calendar = GregorianCalendar.getInstance();
int curDay = calendar.get(Calendar.DATE);
calendar.setTime( new SimpleDateFormat( " yyyy-MM-dd " ).parse(year + " - " + month + " -01 " ));
List < DayInfo > list = new ArrayList < DayInfo > ();
int days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
int weekOfFirstDay = calendar.get(Calendar.DAY_OF_WEEK);
for ( int i = 1 ; i <= days; i ++ ) {
DayInfo dayInfo = new DayInfo();
if (curDay == i) {
dayInfo.setSelected( true );
}
dayInfo.setId(i);
dayInfo.setName( this .getWeekName((i + weekOfFirstDay - 2 ) % 7 ));
list.add(dayInfo);
}
return list;
}
private String getWeekName( int week) {
String[] weeks = new String[] { " 星期日 " , " 星期一 " , " 星期二 " , " 星期三 " , " 星期四 " , " 星期五 " , " 星期六 " };
return weeks[week];
}
}
class DayInfo {
private int id;
private String name;
private boolean selected = false ;
public int getId() {
return this .id;
}
public void setId( int id) {
this .id = id;
}
public String getName() {
return this .name;
}
public void setName(String name) {
this .name = name;
}
public boolean isSelected() {
return this .selected;
}
public void setSelected( boolean selected) {
this .selected = selected;
}
@Override
public String toString() { // AutoGenerate
StringBuilder sb = new StringBuilder();
sb.append( " < " );
sb.append( " id = " ).append( this .id);
sb.append( " name = " ).append( this .name);
sb.append( " selected = " ).append( this .selected);
sb.append( " >/n " );
return sb.toString();
}
}