Win a copy of Bad Programming Practices 101 (e-book) this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Passing date and time using Timestamp.  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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
 
You can thank my dental hygienist for my untimely aliveness. So tiny:
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!