给定一个日期,求该日期的前一天和后一天(Java中日期类的用法)

参考:http://www.cnblogs.com/Johness/archive/2012/09/01/2666163.html
Java中的日期处理
public String getNextDay(String specialDate) 
	{
		Date utilDate = null;
		try
		{
			utilDate = new SimpleDateFormat("yyyyMMdd").parse(specialDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		Calendar cl = Calendar.getInstance();
		
		cl.setTime(utilDate);
		
		int day = cl.get(Calendar.DATE);
		System.out.println("getNextDay:" + day);
		cl.set(Calendar.DATE, day + 1);
		
		String nextDay = new SimpleDateFormat("yyyyMMdd").format(cl.getTime());
		return nextDay;
	}
	
	public String getBeforeDay(String specialDate)
	{
		Calendar cl = Calendar.getInstance();
		Date utilDate = null;
		try 
		{
			utilDate = new SimpleDateFormat("yyyyMMdd").parse(specialDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		cl.setTime(utilDate);
		int day = cl.get(Calendar.DATE);
		System.out.println("getBeforeDay: " + day);
		cl.set(Calendar.DATE, day - 1);
		
		String beforeDay = new SimpleDateFormat("yyyyMMdd").format(cl.getTime());
		return beforeDay;
	}
	@Test
	public void test()
	{
		Scanner sc = new Scanner(System.in);
		String specialDate = sc.nextLine();
		
		System.out.println(getNextDay(specialDate));
		System.out.println(getBeforeDay(specialDate));
	}

你可能感兴趣的:(java相关,笔试题)