• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Fetching date from databse

 
Ranch Hand
Posts: 43
Notepad Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author & internet detective
Posts: 41878
909
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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




 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic