aspose file tools
The moose likes Beginning Java and the fly likes Problem with switches Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Problem with switches" Watch "Problem with switches" New topic
Author

Problem with switches

David Moncada
Greenhorn

Joined: Feb 26, 2013
Posts: 9
Hello guys i'm new to this forums and I hope someone can help me, I have this switch:

String arrivalDate;
arrivalDate = "20 Nov 2013";
String input = arrivalDate;
Date d;
switch (arrivalDate) {

case "dd MMM yyyy":
d = new SimpleDateFormat("dd MMM yyyy", Locale.US).parse(input);
break;
case "dd-MMMM-yyyy":
d = new SimpleDateFormat("dd-MMMM-yyyy", Locale.US).parse(input);
break;
case "ddMMMMyyyy":
d = new SimpleDateFormat("ddMMMMyyyy", Locale.US).parse(input);
break;
case "dd-MMMM-yy":
d = new SimpleDateFormat("dd-MMMM-yy", Locale.US).parse(input);
break;
case "dd/MMMM/yy":
d = new SimpleDateFormat("dd/MMMM/yy", Locale.US).parse(input);
break;
default:
d = new SimpleDateFormat("MM/dd/yyyy", Locale.US).parse(input);
break;
}
String output = new SimpleDateFormat("MM/dd/yyyy").format(d);
System.out.println("this " + output);

}

It always go rigth to the default and well it sends me a parse error, why is this I have tryed to make it work but it doesn't thanks in advance guys
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 9956
    
    6

If your case statement has "dd MMM yyyy", and your input is "20 Nov 2013", those strings are not a match. one starts with a little 'd', the other starts with a digit '2'. The same is true for all your cases - it is matching on the LITERAL string you provide, not some interpretation of what your string means.


Never ascribe to malice that which can be adequately explained by stupidity.
David Moncada
Greenhorn

Joined: Feb 26, 2013
Posts: 9
But then how can I introduce a date to compare to?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56229
    
  13

The switch statement is going to do any pattern matching. Perhaps you need regular expressions to see what pattern your sting matches.


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
David Moncada
Greenhorn

Joined: Feb 26, 2013
Posts: 9
Ok I will try that thanks!
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Problem with switches
 
Similar Threads
convert a string to java.sql.Date object
comparing date
java.text.ParseException: Unparseable date: ""
Date Conversion
Date Confusion