java生成最近n天的时间

生成最近n天的时间,
格式为:
所有
今天
昨天
前天
2011-08-07 星期日
2011-08-06 星期六
public static List<Map<String, Object>> initDate(int dayOfago, String dbkey, String dbName)
	{
		List<Map<String, Object>> dateList = new ArrayList<Map<String, Object>>();

		Map<String, Object> dateMap = new HashMap<String, Object>();
		dateMap.put(dbkey, "");
		dateMap.put(dbName, "所有");
		dateList.add(dateMap);
		for (int i = 0; i < dayOfago; i++)
		{
			dateMap = new HashMap<String, Object>();
			dateMap.put(dbkey, formatDate(i));
			if (i == 0)
			{
				dateMap.put(dbName, "今天");
				dateList.add(dateMap);
				continue;
			}
			if (i == 1)
			{
				dateMap.put(dbName, "昨天");
				dateList.add(dateMap);
				continue;
			}
			if (i == 2)
			{
				dateMap.put(dbName, "前天");
				dateList.add(dateMap);
				continue;
			}
			String showTime = getWeekStr(formatDate(i));
			dateMap.put(dbName, showTime);
			dateList.add(dateMap);
		}
		return dateList;
	}
	
	public static String formatDate(int i)
	{
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		calendar.add(Calendar.DAY_OF_YEAR, -i);
		Date date = calendar.getTime();
		return sdf.format(date);
	}

	public static String getWeekStr(String sdate)
	{
		String str = "";
		str = getWeek(sdate);
		if ("1".equals(str))
		{
			str = "星期日";
		}
		else if ("2".equals(str))
		{
			str = "星期一";
		}
		else if ("3".equals(str))
		{
			str = "星期二";
		}
		else if ("4".equals(str))
		{
			str = "星期三";
		}
		else if ("5".equals(str))
		{
			str = "星期四";
		}
		else if ("6".equals(str))
		{
			str = "星期五";
		}
		else if ("7".equals(str))
		{
			str = "星期六";
		}

		return sdate + "  " + str;
	}

	public static String getWeek(String sdate)
	{
		Date date = strToDate(sdate);
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return new SimpleDateFormat("EEEE").format(c.getTime());
	}

	public static Date strToDate(String strDate)
	{
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		Date strtodate = null;
		try
		{
			strtodate = formatter.parse(strDate);
		}
		catch (ParseException e)
		{
			e.printStackTrace();
		}
		return strtodate;
	}

你可能感兴趣的:(java)