Java使用列舉轉換星期幾到對應漢字
阿新 • • 發佈:2019-01-09
有這樣一個需求,在頁面中選擇週一至之日(複選框),儲存進資料庫的形式是:如果哪天沒選中就是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; } }