首页 > 代码库 > Exception in thread "main" java.lang.IllegalArgumentException

Exception in thread "main" java.lang.IllegalArgumentException

1、错误描述

Exception in thread "main" java.lang.IllegalArgumentException: Cannot format given Object as a Date
	at java.text.DateFormat.format(DateFormat.java:301)
	at java.text.Format.format(Format.java:157)
	at com.you.util.DateBefore.getMonthBefore(DateBefore.java:24)
	at com.you.util.DateBefore.main(DateBefore.java:33)

2、错误原因

/**
 * 
 */
package com.you.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * @author YouHaidong
 *
 */
public class DateBefore 
{
	/**
	 * 
	 * @return
	 */
	public static String getMonthBefore(String subStr)
	{
		Calendar calendar = Calendar.getInstance(); 
		calendar.add(Calendar.MONTH, -12);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
		subStr = sdf.format(calendar);
		return subStr;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) 
	{
		String date = getMonthBefore("2014-02");
		System.out.println(date);
	}

}

3、解决办法


Exception in thread "main" java.lang.IllegalArgumentException