• Post Reply Bookmark Topic Watch Topic
  • New Topic

Dates as switch cases  RSS feed

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello guys i'm having a problem with a switch, the reason is because I'm trying to use dates (in various formats ex dd/MMMM/yy or MMMMddyy) and it goes straigth to the default value, how can I fix this or modify it? Thanks in advance

example of the code:

 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are not using dates at all. You are using the Strings "dd-mm-yyyy" or similar. If you pass "dd-mm-yyyy" you will get that case, otherwise you get default.
No, you cannot use Dates in case labels.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
you should have used code tags; I shall see if I can add them retrospectively.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You obviously want some way of parsing dates from Strings. Not sure how to pick up different formats.
 
David Moncada
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the welcoming, and well yes I was beginning to realize that because that i'm asking for letters as wht I thougth will be undestood as date formats, but well then how could I make this switch work to compare de Date I'm recieving and then change it, I mean the format changer works I only need to differentiate the format of the dates... O yeah sorry I will use the code format in my future post :$
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For that to work you'd basically have to parse the date before parsing the date, which is tricky .

I try to constrain the date formats I have to use. But if I was trying to do this, here's the approach I use:

- Decide on all the formats I want to use, in order of precedence
- Set up a list of SimpleDateFormat objects using those formats
- Iterate through the list, trying to parse the date using each one in turn (obviously you need to catch and ignore any parsing exceptions within the loop). The first one that doesn't throw an exception is the output. You also need to decide what to do if all of them fail (either a default date or throw a proper exception).


 
David Moncada
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:For that to work you'd basically have to parse the date before parsing the date, which is tricky .

I try to constrain the date formats I have to use. But if I was trying to do this, here's the approach I use:

- Decide on all the formats I want to use, in order of precedence
- Set up a list of SimpleDateFormat objects using those formats
- Iterate through the list, trying to parse the date using each one in turn (obviously you need to catch and ignore any parsing exceptions within the loop). The first one that doesn't throw an exception is the output. You also need to decide what to do if all of them fail (either a default date or throw a proper exception).




Well it sounds really tricky indeed but is the first real option I see to solve this, I'm very thankfull for the suggestion now I will try to make it work.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Moncada wrote: . . . Well it sounds really tricky indeed but is the first real option I see to solve this, I'm very thankfull for the suggestion now I will try to make it work.
It’s not nearly as tricky as it sounds. A bit time‑consuming, however.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Split the String on the delimiters between day month and year. You might find a combination of punctuation and whitespace makes a good regex to split on.
Decide what to do if it says 27th February 2013 rather than 27 February 2013.
Consider a Map for month names, so "1", "01", "January" and "Jan" all map to Calendar.JANUARY.
Parse the day and year into numbers.
Pass those values to a Date or Calendar constructor.

Now the hard part: work out whether next Wednesday is 6/3/13, 3/6/13 or 13/3/6
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!