3 Replies - 549 Views - Last Post: 03 November 2012 - 08:26 PM Rate Topic: -----

#1 Occidentally  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 03-November 12

Parsing a full date/time/timezone date to "yyyy-MM-dd"

Posted 03 November 2012 - 06:37 PM

When I use getDate() on my JDateChooser I get something like:

Fri Nov 02 23:20:32 EST 2012


I've tried using SimpleDateFormat but I can't figure out how to turn the above date into something like: "2012-11-02"

I've tried using google to find the answer but couldn't find anything that solved this problem. The documentation for this JDateChooser is not clear on how to override what format the getDate() method spits out so I'm left with trying to parse what it gives me.

This is the API of the JDateChooser I'm using: http://www.toedter.c...ateChooser.html

I'd appreciate it if anyone could clue me in on Date parsing.

Is This A Good Question/Topic? 0
  • +

Replies To: Parsing a full date/time/timezone date to "yyyy-MM-dd"

#2 RodgerB  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 66
  • View blog
  • Posts: 2,284
  • Joined: 21-September 07

Re: Parsing a full date/time/timezone date to "yyyy-MM-dd"

Posted 03 November 2012 - 07:30 PM

It says in the API that the getDate() method returns a Date type, not a String. I assume you've used its toString() method in the above example.

The SimpleDateFormat class can be used to format a String to appear the way you would like, here is some example code for the current date.

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
// An empty constructor for the Date class gives the current date/time.
Date today = new Date();
String todayStr = dateFormat.format(today);
System.out.println(todayStr);



Adapting this code to the needs of your own should be a matter of changing the assignment of the today variable to your myDateChooser.getDate() object as such.

Hope that all goes well for you.

Cheers,
-RodgerB
Was This Post Helpful? 1
  • +
  • -

#3 Occidentally  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 03-November 12

Re: Parsing a full date/time/timezone date to "yyyy-MM-dd"

Posted 03 November 2012 - 08:18 PM

Thank you! I was using SimpleDateFormat incorrectly. It seems to obvious now.
Was This Post Helpful? 0
  • +
  • -

#4 RodgerB  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 66
  • View blog
  • Posts: 2,284
  • Joined: 21-September 07

Re: Parsing a full date/time/timezone date to "yyyy-MM-dd"

Posted 03 November 2012 - 08:26 PM

No worries! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1