• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Fetching date from databse

 
tushar attar
Ranch Hand
Posts: 36
Chrome Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I want to fetch data from my sql database. Knowing that sql store date in yyyy-mm-dd format, I want to fetch this date in form of day, month and year separately in java (may be in form of string, three different variables).

How can I do that? Please tell

Thanks
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tushar,
JDBC allows you to get the date from the database in the form of a Date object. Then you can use SimpleDateFormat in Java to parse it.
 
Dave Tolls
Ranch Hand
Posts: 2095
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tushar attar wrote:Knowing that sql store date in yyyy-mm-dd format ...


Not so.
That's simply the default display format for whatever database workbench or tool you happen to be using.
Different databases store Dates in different ways.
 
Campbell Ritchie
Sheriff
Posts: 51368
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your SQL query probaby allows you to create an instance of this Date class. You might do well to create a LocalDate object (Java8 only) and use its methods to get day month or year. More details in the Java™ Tutorials; try the Date Classes section.

Sorry for delay in replying; I wrote this post hours ago and forgot to push submit.
 
grace christopher
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tushar attar wrote:Hi
I want to fetch data from my sql database. Knowing that sql store date in yyyy-mm-dd format, I want to fetch this date in form of day, month and year separately in java (may be in form of string, three different variables).

How can I do that? Please tell

Thanks




In particular, it means you don't need to worry about the time zone of the Calendar vs the time zone of a SimpleDateFormat - you can just parse to a LocalDate, which is what the data really shows you. It also means you don't need to worry about months being 0-based




 
Knute Snortum
Bartender
Pie
Posts: 2895
62
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi grace, and Welcome to the Ranch!  Thanks for your contribution.  Just one thing, if you could UseCodeTags (that's a link) when you post code, it will be easier to read and reference lines.  Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic