Exception in thread "main" java.lang.IllegalArgumentException

时间:2022-05-23 21:10:11

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、解决办法