muthu kumar
muthu kumar

Reputation: 99

How to get Timestamp with AM/PM in java

I have a date as String , which needs to be converted in to Time Stamp with AM/PM . I tried the below way, I'm getting the proper date format but didn't get in AM/PM.

Can any one please help ?

code Snippet:

String dateString = "10/10/2010 11:23:29 AM";

SimpleDateFormat sfdate = new SimpleDateFormat("MM/dd/yyy HH:mm:ss a");
    Date date = new Date();
    date = sfdate.parse(dateString);
    System.out.println(new Timestamp(date.getTime()));

Which gives me the output as below :

2010-10-10 11:23:29.0

But I needs it like this

2010-10-10 11:23:29.00000000 AM

Kindly help me please.

Upvotes: 6

Views: 22483

Answers (4)

Joe
Joe

Reputation: 6827

What you're seeing is the result of Timestamp.toString(). The actual value in the Timestamp object instance is valid.

If you're getting an error in a subsequent SQL operation, please post that error along with the code you're using.

Upvotes: 0

femtoRgon
femtoRgon

Reputation: 33341

Timestamp.toString() prints to a specific format: yyyy-mm-dd hh:mm:ss.fffffffff. The Timestamp object itself should be correct, if that's all you are looking for.

If you then want to define another format in order to print it as you like, that would require you to format Date object, using an appropriate pattern for the output format you are looking for.

Upvotes: 0

user1596371
user1596371

Reputation:

Try:

System.out.println(sfdate.format(date));

As your last line rather than the one that you have at current.

Upvotes: 1

rocketboy
rocketboy

Reputation: 9741

Why create a timestamp ? When you can just :

            SimpleDateFormat sfdate = new SimpleDateFormat("MM/dd/yyy HH:mm:ss a");
            Date date = new Date();
            date = sfdate.parse(dateString);
            System.out.println(sfdate.format(date) );

Output:

10/10/10 11:23:29 AM

Upvotes: 3

Related Questions