Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Fetching date from databse

 
Ranch Hand
Posts: 43
Notepad Chrome Java
  • 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
 
author & internet detective
Posts: 40239
819
Eclipse IDE VI Editor Java
  • 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.
 
Master Rancher
Posts: 4694
49
  • 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.
 
Marshal
Posts: 70591
287
  • 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.
 
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




 
Sheriff
Posts: 7108
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • 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.
 
Whoever got anywhere by being normal? Just ask this exceptional tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic