user182944
user182944

Reputation: 8067

Timestamp format match

I wrote this bit of code for converting string to Timestamp:

String timeStamp = "2012-04-13 09:00:30";

    try{

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        java.util.Date parsedDate = sdf.parse(timeStamp);
        System.out.println(parsedDate.toString());
        java.sql.Timestamp timestamp = new java.sql.Timestamp(parsedDate.getTime());
        System.out.println(timestamp.toString());           

    }catch (Exception e) {
        // TODO: handle exception
    }

The output i am getting is :

2012-04-13 09:00:30.0

But i want the output to be:

2012-04-13 09:00:30

i.e. i dont need the .0 at the end

Please suggest

Thanks,

Upvotes: 0

Views: 3730

Answers (3)

Mark Reed
Mark Reed

Reputation: 95252

The output of format() is always going to be a String. What exactly are you looking for here?

The ".0" you are complaining about is only there in the string representation of the object. When it gets inserted into the database, it's a timestamp, not a string. (You may or may not see extra decimals when you query the database directly; it depends on how you write our SQL.)

Upvotes: 0

Win Myo Htet
Win Myo Htet

Reputation: 5457

Use your sdf again like this

 System.out.println(sdf.format(parsedDate));

Upvotes: 0

Jigar Joshi
Jigar Joshi

Reputation: 240898

You need to use format() method of SimpleDateFormat

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    //parsing string to date
    java.util.Date parsedDate = sdf.parse(timeStamp);
    //invoking toString() method which has its fixed format output
    System.out.println(parsedDate.toString());

Now use

    System.out.println(sdf.format(parsedDate));

Upvotes: 1

Related Questions