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、解决办法
时间: 2024-11-11 09:09:16