• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Date Confusion

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am building a form that allows a user to select two dates(input boxes)in which to perform a search on. The users entered criteria will always be formatted to the MM/dd/yyyy format. However, in order for my query to work I need to format the date as follows: yyyy/MM/dd.

Otherwise I get the following exception:


I have googled, search SDN forums and here but I haven't found the solution to my problem.

I have tried the following in my servlet(from examples online):



What I dont understand is the out print lines I am logging which are as follows:



Which API should I be looking at? It seems like I need to use both java.text.SimpleDateFormat and java.text.DateFormat or perhaps one or the other. Why are my print outs so completely off my actual input dates? And is there a way to format a date string from MM/dd/yyyy to date yyyy/MM/dd?

I would appreciate any clarification on the above issues.

Thanks in advance - regards,
Crystal Bazil
[ December 09, 2005: Message edited by: Crystal Bazil ]
 
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since your input is in MM/dd/yyyy format, the SimpleDateFormat object that parses that string should be created like so:

// input to parse is in MM/dd/yyyy format, not yyyy/MM/dd
SimpleDateFormat mmddyyyy = new SimpleDateFormat("MM/dd/yyyy");
Date start = mmddyyyy.parse(inputStr);

// If you want to output as yyyy/MM/dd, you need another SimpleDateFormat:
SimpleDateFormat yyyymmdd = new SimpleDateFormat("yyyy/MM/dd");
System.out.println(yyyymmdd.format(start));
[ December 09, 2005: Message edited by: Junilu Lacar ]
 
Junilu Lacar
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or you change the pattern by passing in a new format string to the applyPattern() method of the same SDF object.

The thing to remember is that parse() uses the pattern to determine what format the input (the String you are parsing) should be in. On the other hand, format() uses the pattern to determine the format of the output.
 
Crystal Bazil
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Junilu - Your solution solved my issue. I appreciate the explanation and you time!

Crystal
 
No one can make you feel inferior without your consent - Eleanor Roosevelt. tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic