Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Time help needed

Status
Not open for further replies.

bunnyweb

MIS
Jan 13, 2003
42
IE
Hello Everyone. I am attempting to create a JSP application and having problems with formatting a time field.

I have a time stored in my database (SQL Server) as a Date/Time field and when I query the field I get somthing like:

1900-01-01 06:00:00.000

Below is how I'm getting the time value. I'm placing the time into a String, but I'm not even sure if this is correct.

String ArrivalTime = rsVisitor.getString("visitarrivaltime");

My problem is that when I output this value onto my web page it looks like:

1900-01-01 06:00:00.000

and it should look like:

06:00

I would normally just parse the string and extract the piece that I want to display, except that not all of our times in the database are stored this way. Some are stored in the shorter version. I know in ASP there is a way to format time. Can someone give me the same in JSP? I don't even need the time in a String necessarily, as long as I can print it out.

Thanks to anyone who can help me out!


 
Why not just use :

Code:
java.sql.Timestamp ArrivalTime = rsVisitor.getTimeStamp("visitarrivaltime");

And then just extract the fields you need via the various getter methods ?


--------------------------------------------------
Free Database Connection Pooling Software
 
Thanks for the suggestions! I tried:


java.sql.Timestamp ArrivalTime = rsVisitor.getTimeStamp("visitarrivaltime");



but I get an error message:



C:\Tomcat\work\Standalone\localhost\SafeWebV21\bookconf_jsp.java:1797: cannot resolve symbol
symbol : method getTimeStamp (java.lang.String)
location: interface java.sql.ResultSet
java.sql.Timestamp ArrivalTime = rsVisitor.getTimeStamp("visitarrivaltime");



I have imported
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.lang.*" %>
<%@ page import ="java.util.*" %>


Am I missing something?


I was also trying things with

java.text.SimpleDateFormat formatTime = new java.text.SimpleDateFormat( "HH:mm:ss" );

But no luck there either.


Help Please!! :(
 
Thank you sedj! A bit of tweeking and it worked.

For anyone else with this same problem, here is my solution:



java.text.SimpleDateFormat formatTime = new java.text.SimpleDateFormat("HH:mm");

java.sql.Timestamp ArrivalTime = null;
String strArrivalTime = "";

... query database ....

ArrivalTime = rsVisitor.getTimestamp("visitarrivaltime");
strArrivalTime = formatTime.format(ArrivalTime);

out.println(strArrivalTime)


And I go from a format of:
1900-01-01 21:00:00.000

To a format of:
21:00

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top