• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
Sheriffs:
  • Devaka Cooray
  • paul wheaton
  • Mark Herschberg
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
  • Jj Roberts
Bartenders:
  • Carey Brown
  • salvin francis
  • Piet Souris

Problem with switches

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
lowercase baba
Posts: 12963
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
David Moncada
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But then how can I introduce a date to compare to?
 
Sheriff
Posts: 67502
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The switch statement is going to do any pattern matching. Perhaps you need regular expressions to see what pattern your sting matches.
 
David Moncada
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok I will try that thanks!
 
reply
    Bookmark Topic Watch Topic
  • New Topic