有这样一个需求,在页面中选择周一至之日(复选框),保存进数据库的形式是:如果哪天没选中就是0,如果哪天选中了就是相应的数字。
比如,选中了周一,周二,周四,周六,保存时的一串数字为1204060,
与此同时页面种要显示选中的日期为:周一至周二、周四、周六。
比如,选中了周一,周二,周四,周六,保存时的一串数字为1204060,
与此同时页面种要显示选中的日期为:周一至周二、周四、周六。
可用如下的代码实现
import java.util.*;
public class GetWeekDays {
public GetWeekDays () {
}
public static void main(String[] args) {
System.out.println("转换结果: " + transfer2WeekDay("1034007"));
}
public static String transferAlarmLevel(String string) {
StringBuffer stringBuffer = new StringBuffer("");
for (int i = 0; i < string.length(); i++) {
if (string.charAt(i) != '0') {
stringBuffer.append((i + 1) + "");
stringBuffer.append("、");
}
}
return stringBuffer.substring(0, stringBuffer.length() - 1);
}
public static String transfer2WeekDay(String processedDate) {
StringBuffer stringBuffer = new StringBuffer("");
String[] periodsDate = processedDate.split("0");
List<String> newPeriodsDate = new ArrayList<String>();
for (String periodDate : periodsDate) {
if (!"".equals(periodDate))
newPeriodsDate.add(periodDate);
}
for (String periodDate : newPeriodsDate) {
if (periodDate.length() == 1) {
//从枚举获得对应周几
String singleDate = WeekDaysEnum.getTypeNameById(Integer.valueOf(periodDate));
stringBuffer.append(singleDate);
stringBuffer.append("、");
} else if (periodDate.length() > 1) {
Character start = periodDate.charAt(0);
Character end = periodDate.charAt(periodDate.length() - 1);
Integer startNum = Integer.valueOf(start.toString());
Integer endNum = Integer.valueOf(end.toString());
String weekStart = WeekDaysEnum.getTypeNameById(startNum);
String weekEnd = WeekDaysEnum.getTypeNameById(endNum);
stringBuffer.append(weekStart);
stringBuffer.append("至");
stringBuffer.append(weekEnd);
stringBuffer.append("、");
}
}
return stringBuffer.substring(0, stringBuffer.length() - 1);
}
}
上面用到了一个枚举类,如下:
import java.util.Map;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.List;
public enum WeekDaysEnum {
Mon(1, "周一"), Tus(2, "周二"), Wen(3, "周三"), Thu(4, "周四"), Fri(5, "周五"), Sat(6, "周六"), sun(7, "周日");
private int typeId;
private String typeName;
WeekDaysEnum(int typeId, String typeName) {
this.typeId = typeId;
this.typeName = typeName;
}
public int getTypeId() {
return typeId;
}
public String getTypeName() {
return typeName;
}
/*public static JSONArray toList() {
JSONArray list = new JSONArray();
JSONObject item = null;
WeekDaysEnum[] values = values();
for(int i = 0; i < values.length; ++i) {
item = new JSONObject();
WeekDaysEnum value = values[i];
item.put("typeId", Integer.valueOf(value.getTypeId()));
item.put("typeName", value.getTypeName());
list.add(item);
}
return list;
}*/
public static List toList() {
List list = new ArrayList();
Map item = new HashMap();
WeekDaysEnum[] values = values();
for (int i = 0; i < values.length; ++i) {
//item = new HashMap();
WeekDaysEnum value = values[i];
item.put("typeId", Integer.valueOf(value.getTypeId()));
item.put("typeName", value.getTypeName());
list.add(item);
}
return list;
}
public static String getTypeNameById(int typeId) {
String value = "未知" + typeId;
WeekDaysEnum[] values = values();
for(int i = 0; i < values.length; ++i) {
WeekDaysEnum type = values[i];
if(type.getTypeId() == typeId) {
value = type.getTypeName();
break;
}
}
return value;
}
}