Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Oracle Date to Java Calendar

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am an Oracle veteran, but fairly new to Java and JDBC.

I am trying to retrieve a date column from an Oracle Database and map it to
a java.util.Calendar type to populate an XML document.

Can someone point me in the proper direction ?

Thanks.

Marc
 
Sheriff
Posts: 67620
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"MarcL",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
JavaRanch Sheriff
 
Marc LeClerc
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry . It has been changed
 
Marc LeClerc
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay,

I've gotten this far:

Calendar calStart = Calendar.getInstance();
calStart.setTime(rs.getDate("eff_from_date"));

However, the hour is defaulting to 04, and the minutes and seconds both to 00.

How can I stuff the time stamp portion of the Oracle Date into the calendar ?
 
Sheriff
Posts: 26968
84
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to see the time component of the database column, then use rs.getTimestamp() instead of rs.getDate(). The API documentation explains the difference between the two, I believe.
 
Marc LeClerc
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay now I can see it.. but how do I convert it to a Calendar item?

I want to select an Oracle Date column, and have it end up as a Java Calendar type with both the date and time portion.

Can someone please provide an example of this ?

If I use rs.getTimeStamp, how do I then populate my calendar with this ?

Calendar calEnd = Calendar.getInstance();
calEnd.setTime(rs.getTimeStamp("eff_thru_date"),calEnd);

This has a compile time error on the setTime method telling with incompatible types i.e. getTimeStamp is not of type Long.

If I use calEnd.setTime, this truncates the timeStamp portion.

Please, can someone help. I cannot believe that I could not find an example out there someplace, but I couldn't.
 
Paul Clapham
Sheriff
Posts: 26968
84
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Marc LeClerc
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That syntax truncates the time.

That's what I tried 1st .

I am so confused.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Calendar takes a java.util.Date, and java.sql.Timestamp should NOT be treated as a java.util.Date (see the API doc on the former). That MAY be your problem. Try using calendar.setTime( rs.getTimestamp().getTime() )
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic