Param-Ganak
Param-Ganak

Reputation: 5875

Query gives different output on different OS

I have written the following function in Java. This function returns current timestamp by executing a query. The function code is as follows :

private  String getTimeStamp(){
    String timeStamp=null;      
    try{
        String strQuery="select current_timestamp";
        PreparedStatement pmtQuery=con.prepareStatement(strQuery);
        ResultSet rsQuery=pmtQuery.executeQuery();
        rsQuery.next();
        timeStamp=rsQuery.getString(1);
        JOptionPane.showMessageDialog(null, "Value of timeStamp : "+timeStamp);
    }catch(SQLException e){
        System.out.println("SQL Exception in the getTimeStamp()");
    }       
    return timeStamp;
} 

When I use this function on windows It gives proper out put and works fine. Ex.

If execute above function in widows it gives timestamp like ex. 2011-06-01 17:05:03

but when I execute this function in Debina linux it gives timestamp as 2011-06-01 17:05:03.0

It appends .0 to timestamp

Please guide me in this problem

1.why such different output comes on different system?

2.How to avoid this problem?

3.How to solve this problem?

I am using following configurations

windows

windows 7, Mysql database, Java 6

Linux

Debian linux, Mysql Database, Java 6

Thank You!

Upvotes: 0

Views: 204

Answers (4)

user85421
user85421

Reputation: 29700

The problem probably are different (default) settings of the JDBC drivers.

I would prefer fetching the timestamp as a Timestamp (subclass of java.util.Date):

import java.sql.Timestamp;
...
Timestamp timeStamp;
...
timeStamp = rsQuery.getTimestamp(1); 

This way you can control how to format it in the Java code as/if needed (e.g. using SimpleDateFormat or String.format).

Upvotes: 1

Jigar Joshi
Jigar Joshi

Reputation: 240928

Use this

SELECT DATE_FORMAT(CURDATE(), '%d/%m/%Y %hh%mm%ss')

Note:

I wonder why you are querying to get the currentDate

Upvotes: 2

Talha Ahmed Khan
Talha Ahmed Khan

Reputation: 15453

you can also use

SimpleDateFormat

Its a simple date formatter.

Upvotes: 0

edoloughlin
edoloughlin

Reputation: 5891

It's not portable, but you can use the DATE_FORMAT function:

SELECT DATE_FORMAT(CURRENT_TIMESTAMP, '%d/%m/%Y %h:%i:%s');

Documentation is here

Upvotes: 0

Related Questions