File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes Timestamp to date Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Timestamp to date" Watch "Timestamp to date" New topic
Author

Timestamp to date

vivek ja
Ranch Hand

Joined: Feb 24, 2005
Posts: 80
I am trying to figure out what a database field stands for,....
I have fields like 00000001153858800000, 0000009223372036854775807 etc.,

I thought this could be a timestamp and tried to get a date out of it
This is the code I am using.
When i put in 1153858800000L, it gives me
Day: 2
Month:6
Year 2006

But when I enter 9223372036854775807, looks like i am way off the mark.
it gives me year:292278994, day :0


public class TimestampTest
{

public static void main(String[] args)
{
//1153858800000L
long startTime =9223372036854775807L ;

Date startDate = new Date(startTime);
int year = startDate.getYear()+1900;

System.out.println("Day: "+startDate.getDay());
System.out.println("Month: "+startDate.getMonth());
System.out.println("Year: "+year);
System.out.println("time : "+startDate.getTime());
System.out.println("Hours : "+startDate.getHours());
System.out.println("Minutes: "+startDate.getMinutes());
System.out.println("Seconds: "+startDate.getSeconds());

}
}
Brian Mozhdehi
Ranch Hand

Joined: Aug 17, 2006
Posts: 81
What platform is it? If it is a common platform, such as MSSQL, Oracle, Sybase, DB2, MySQL (and there are many more that this can work for) then there should be a JDBC driver for it. If you can find one, then get teh JDBC driver add it to you classpath and then you can use it to find out the type of column using the JDBC API.

If there is no JDBC driver, but there is an ODBC driver, then the JDBC ODBC bridge **should** work with the following as well.

For example, on MSSQL, the following will do it:

//register the driver
try
{
Class driver = Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
DriverManager.registerDriver((Driver)driver.newInstance());
}
catch(Throwable e)
{}

//get a connection using the JDBC specific URL
//and a valid username and password
Connection dbconn = DriverManager.getConnection(url, userName, password);

//get the meta data
DatabaseMetaData aMeta = dbconn.getMetaData();

//get the columns where tableName is the name of the table
ResultSet rs = aMeta.getColumns(null, null, tableName, null);

while(rs.next())
{
rs.getString("COLUMN_NAME")); //name of the column
rs.getShort("DATA_TYPE")); //the data type as a java.sql.Type
rs.getString("TYPE_NAME")); //the name of the data type
}

rs.close();

Please note, if the name of the data type doesnt cut it, then use the data type noted above and use the java.sql.Types object to convert it.

This will be much easier than what it looks like you are trying to do. I dont think that the fact that the raw data found, presumably in some query tool, will convert straight off necessarily to a java type. You may in fact be correct that its a timestamp (or maybe not) but the above should tell you asap.

Hope that helps.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Why even bother with JDBC? You could just query the catalog directly to find out what data type your field uses.

Tell us which data base it is you are using and someone may tell you the SQL to do this.

(Also, this is not a Java question really. We have a JDBC forum where this is better suited, so I'm moving it there)


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Timestamp to date