Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
20.9.2014 - We have just updated our course layouts and descriptions and added our 2015 schedule.

Java date and calendar classes

Posted by jill (jill), 13 December 2004
I need to get a date from an (oracle) database, call it startdate, then get from another table a number of days, a number of hours and a number of minutes and add these to startdate to get a new date and time, call it session.  Then I have to store 'session' back in the database.  

I am very new to Java,  and struggling - I think I have to use the calendar class to do the addition - but can you point me to a nice easy to follow example?  

Also what is the difference between the java.util.date class and the java.sql.date class?

TIA for any help

Jill


Posted by admin (Graham Ellis), 14 December 2004
Dates seem to be over-featured and over-complex in Java:

The java.util.Date class has been largely deprecated, but does include a few useful methods. It has been largely replaced by the java.util.Calendar class.

To take a date and move forward from it, I would
     Calendar demo = Calendar.getInstance();
     demo.set (appropriate parameters);
     long now  = demo.getTimeinMillis;
     long finish = perform maths on "now" to set end time
     demo.setTimeinMillis(finish)

This is "pseudocode" ... and I'm not near an appropriate system to test with, but hopefully it may point you in a reasonable direction.

The java.sql.Date class deals with date objects to and from SQL databases - so you may need to get involved with that too!

Posted by jill (jill), 14 December 2004
Thanks, but I am still stuck!  I can get the date out of the database with

Date thisdate = resultset.getDate("field")

But how do I get that into the Calendar instance?

How do I convert the date into milliseconds?

In your line

demo.set (appropriate parameters);

how do I get the parameters, that is without using deprecated methods?

TIA
Jill

Posted by jill (jill), 14 December 2004
Further to my last posting - I just found

calendar.setTime(date)

which I think is going to get me a bit further.

Sorry
Jill

Posted by jill (jill), 14 December 2004
Getting a bit further - a piece of code which I do not understand seems to be able to get me a java.sql.Date from a calendar  namely

java.sql.Date jsqlD =  new java.sql.Date(cal.getTime().getTime() );

So don't worry, and thanks for the input - I will struggle on.

Jill

Posted by admin (Graham Ellis), 14 December 2004
OK ... do follow up if you hit another wall, and I'll see if I can shed further light. This is a heavy week though ... and virtually all my machines are locked away overnight on site in Oxford so I can't experiment as much as usual.



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho