在以下代码输入中:
Enter Date: 3/2/2011
输出:
Entered Date is February 3, 2011
Entered Month is 02
问题是,当我输入这个日期3/14/2012
时,日期格式功能会自动将月份更改为12+2
(二月)。如果我把13/15/2011
,它会改变月份3(12+3)
。
它应该在14
那个“无效月份”上给出一个错误
package lesson4;
import java.util.*;
import java.text.*;
public class ConvertDate {
static String Month;
static String fulldate;
static int month;
static int[] montharray={1,2,3,4,5,6,7,8,9,10,11,12};
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.print("Enter Date: ");
String ind = sc.nextLine();
//Date now = new Date();
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat f = new SimpleDateFormat("dd");
SimpleDateFormat m = new SimpleDateFormat("MM");
Date d = null;
Date e=null;
Date g=null;
try {
d=df.parse(ind);
e=df.parse(ind);
g=df.parse(ind);
DateFormat df3 = DateFormat.getDateInstance(DateFormat.LONG);
fulldate = df3.format(d);
Month=m.format(g);
month =Integer.parseInt(Month);
String date =f.format(e);
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("The entered date is: " + fulldate);
System.out.println("The entered month is: " + Month);
}
}