fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.text.*;
  7.  
  8. /* Name of the class has to be "Main" only if the class is public. */
  9. class Ideone
  10. {
  11. public static void main (String[] args) throws java.lang.Exception
  12. {
  13. try {
  14. Calendar cal = Calendar.getInstance();
  15.  
  16. cal.setTime(sdf.parse("Jun"));
  17. System.out.println("Jun=" + (cal.get(Calendar.MONTH) + 1));
  18. cal.setTime(sdf.parse("June"));
  19. System.out.println("June=" + (cal.get(Calendar.MONTH) + 1));
  20. cal.setTime(sdf.parse("6"));
  21. System.out.println("6=" + (cal.get(Calendar.MONTH) + 1));
  22. } catch (ParseException e) {
  23. e.printStackTrace();
  24. }
  25.  
  26. try {
  27. SimpleDateFormat sdf = new SimpleDateFormat("M", Locale.JAPAN);
  28. Calendar cal = Calendar.getInstance();
  29.  
  30. cal.setTime(sdf.parse("6"));
  31. System.out.println("6=" + (cal.get(Calendar.MONTH) + 1));
  32. cal.setTime(sdf.parse("06"));
  33. System.out.println("06=" + (cal.get(Calendar.MONTH) + 1));
  34. cal.setTime(sdf.parse("Jun"));
  35. System.out.println("Jun=" + (cal.get(Calendar.MONTH) + 1));
  36. } catch (ParseException e) {
  37. e.printStackTrace();
  38. }
  39. }
  40. }
Success #stdin #stdout #stderr 0.18s 321472KB
stdin
Standard input is empty
stdout
Jun=6
June=6
6=6
06=6
stderr
java.text.ParseException: Unparseable date: "6"
	at java.text.DateFormat.parse(DateFormat.java:366)
	at Ideone.main(Main.java:21)
java.text.ParseException: Unparseable date: "Jun"
	at java.text.DateFormat.parse(DateFormat.java:366)
	at Ideone.main(Main.java:35)