大家好,我一直在尝试解决此问题,但无法解决,问题是删除在 hashmap 中不起作用。
请看下面的代码。
package com.org.common;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@SuppressWarnings("unused")
class Workindays {
public static int findNoOfDays(int year, int month, int day) {
Calendar calendar = Calendar.getInstance();
calendar.set(year, month - 1, day);
int days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
return days;
}
public static Map<String, String> getHolydaysMap(int year, int month, int day) {
//connect with database and check whether the date is holyday query is = SELECT * FROM holiday_calendar h WHERE date >='2008-10-01' AND date <='2008-10-30' AND type='Fixed';
Map<String, String> holydaysMap = new LinkedHashMap<String,String>();
holydaysMap.put("20","17-04-2012");
holydaysMap.put("10","25-04-2012");
return holydaysMap;
}
public static Map<String, String> getWorkingDaysMap(int year, int month, int day){
int totalworkingdays=0,noofdays=0;
String nameofday = "";
ConcurrentHashMap<String,String> workingDaysMap = new ConcurrentHashMap<String,String>();
Map<String,String> holyDayMap = new LinkedHashMap<String,String>();
noofdays = findNoOfDays(year,month,day);
holyDayMap = getHolydaysMap(year,month,day);
for (int i = 1; i <= noofdays; i++) {
Date date = (new GregorianCalendar(year,month - 1, i)).getTime(); // year,month,day
SimpleDateFormat f = new SimpleDateFormat("EEEE");
nameofday = f.format(date);
String formatedDate = i+"-"+month+"-"+year;
if(!(nameofday.equals("Saturday") || nameofday.equals("Sunday"))){
workingDaysMap.put(String.valueOf(i),formatedDate);
totalworkingdays++;
}
}
workingDaysMap.put("totalworkingdays", String.valueOf(totalworkingdays));
System.out.println("removeHolyday : "+removeHoliday(workingDaysMap,holyDayMap));
return workingDaysMap;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Map removeHoliday(ConcurrentHashMap daysMap, Map holydayMap) {
Iterator<Map.Entry> holyDayiterator = holydayMap.entrySet().iterator();
while (holyDayiterator.hasNext()) {
Map.Entry holyDayEntry = holyDayiterator.next();
Iterator<Map.Entry> daysiterator = daysMap.entrySet().iterator();
while (daysiterator.hasNext()) {
Map.Entry daysEntry = daysiterator.next();
if(daysEntry.getKey().equals(holyDayEntry.getKey()))
daysMap.remove(holyDayEntry.getKey());
}
}
return daysMap;
}
@SuppressWarnings({ "rawtypes" })
public static void main(String[] args) {
String delimiter = null, dateValues[] = null, startDate = "01-04-2012";
int year = 0,month=0,day=0,totalworkingdays = 0;
Map workingDaysMap = new LinkedHashMap<String,String>();
startDate = "01-04-2012";
delimiter = "-";
dateValues = startDate.split(delimiter);
year = Integer.parseInt(dateValues[2]);
month = Integer.parseInt(dateValues[1]);
day = Integer.parseInt(dateValues[0]);
workingDaysMap = getWorkingDaysMap(year, month, day);
//System.out.println("workingdays map : "+workingDaysMap);
}
}
这是我用来删除值的方法
removeHolyday
我刚刚给出了示例值,以通过以下方法进行测试。
getHolydaysMap
请帮忙。
编辑:感谢所有花费宝贵时间回答,赞成和反对的人。我已经更改了代码,现在工作正常,
更新的工作代码。
问候安东尼