java日历使用
根据日期获取周,年,本周一,周日 通过给定周,年获取本周一,周日/五
设置下日历其实就可以了...
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
if(map.get("ID")==null||map.get("ID").toString().equals("")||map.get("RELOAD")!=null){
Date d = new Date();
//重新加载的话,日期可能会改变
if(map.get("RELOAD")!=null){
String now = map.get("NOW").toString();
try {
d = sdf.parse(now);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//获取指定日期的年周
Calendar c = Calendar.getInstance(Locale.CHINA);
c.setTime(d);
String week = Integer.toString(c.get(Calendar.WEEK_OF_YEAR));
if (week.length() == 1)
week = "0" + week;
String year = Integer.toString(c.get(Calendar.YEAR));
map.put("YEAR", year);
map.put("WEEK", week);
//找到周一和周日/周五
int day = c.get(Calendar.DAY_OF_WEEK);
if(map.get("FULL")!=null&&map.get("FULL").equals("0"))
c.add(Calendar.DAY_OF_YEAR, -1);
else
c.add(Calendar.DAY_OF_YEAR, 8-day);
map.put("SUNDAY", sdf.format(c.getTime()));
c.add(Calendar.DAY_OF_YEAR, -6);
map.put("MONDAY", sdf.format(c.getTime()));
}
if(map.get("EDITWEEK")!=null||map.get("DETAILWEEK")!=null){
//根据制定的年周找到该周的星期一和星期天/周五
Calendar c = Calendar.getInstance(Locale.CHINA);
c.setTime(new Date());
String week = map.get("WEEK").toString();
if(week.indexOf('0')==0)
week = week.substring(1);
c.set(Calendar.WEEK_OF_YEAR, Integer.parseInt(week));
String year = map.get("YEAR").toString();
c.set(Calendar.YEAR, Integer.parseInt(year));
c.set(Calendar.DAY_OF_WEEK, 2);
//找到周一和周日/周五
int day = c.get(Calendar.DAY_OF_WEEK);
map.put("MONDAY", sdf.format(c.getTime()));
if(map.get("FULL")!=null&&map.get("FULL").equals("0"))
c.add(Calendar.DAY_OF_WEEK, 4);
else
c.add(Calendar.DAY_OF_WEEK, 6);
map.put("SUNDAY", sdf.format(c.getTime()));
}