• 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

Passing date and time using Timestamp.

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Ok, i am in a bit of a fix here. Here is my problem:
I have date and time coming to me in String type. I need to pass this date and time to a single Oracle table Date field. Hence i meed to combine the values of the date and time string variables to a single variable to pass it on to the Oracle Date field. Once passed the date in the Oracle date field should be MM/DD/YYYY HH:MI:SS. To solve this problem i used the java.sql.Timestamp class.
And to initlialize the same I used it constructor method: Timestamp(int year, int month, int date, int hour, int minute, int second, int nano).
Hence i would manually break the data in the string date and time variables and pass it onto the Timestamp constructor. And this is working great. One problem though: The above Constructor is deprecated hence cant use it. Therefore i need to use the second constructor: Timestamp(long time).
But i cant figure out how to use this one. How do i pass the string date and time variable as one long variable to the Timestamp constructor? Or did i get that wrong? Is there any other way to solve this problem?

TIA
Dhanesh.
 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dhanesh,

From reading you description my understanding it that you need to parse a String into a Date.

A simple way of doing this is to use java.text.SimpleDateFormat. Once you have a Date you can then format in into any new String format you want. Or you could just get a long that represents the data and time.

Let me know if I have misunderstood you problem.

Chris.
 
Dhanesh Gesota
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Chris for the prompt reply.

What i want to exactly know is that how can I get a long that represents the data and time? This date and time are in 2 string variables.

TIA
Dhanesh.
 
Chris Harris
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using the SimpleDateFormat class you can parse you two Strings to make a Date object. With this Date object try calling getTime(). Here is an Example.




Hope that helps.

Chris.
 
Dhanesh Gesota
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Chris. That was helpful too.
I did the following to contruct a Timestamp object:
Date dte;
Timestamp tstamp;
GregorianCalendar gCal;
gCal = new GregorianCalendar(
Integer.parseInt(date.substring(0,4)),
Integer.parseInt(date.substring(4,6)) - 1,
Integer.parseInt(date.substring(6,8)),
Integer.parseInt(time.substring(0,2)),
Integer.parseInt(time.substring(2,4)),
Integer.parseInt(time.substring(4,6)) );

dte = new Date(gCal.getTime().getTime());
tstamp = new Timestamp(dte.getTime());

Thanks for the help!
-Dhanesh
 
reply
    Bookmark Topic Watch Topic
  • New Topic