2

I have a TimeStamp '2013-06-24 10:46:11.0' and I need to cut off the .0 part, so what I did was to use the SimpleDateFormat to parse it to String and back then parse it to date, the first conversion was fine but the second (string to date) throws a java date time.

public void convert(Object object) {
    Date date;
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    date = object().getDate();
    String formated = format.format(date);
    try {
       date = format.parse(formated);
    } catch (ParseException ex) {
       Logger.getLogger(DlgConsultaFactura.class.getName()).log(Level.SEVERE, null, ex);
    }
}

What I expect is a date like this 2013-06-24 10:46:11, but what I got is this date Mon Jun 24 10:46:11 CDT 2013

Any help will be appreciated. Thanks.

4
  • 1
    @Andeas : he doesn't use object, he uses object(), which is a method reference. And very likely a bug. Commented Jul 8, 2013 at 19:40
  • @Erik yepp, recognized it - deleted my comment... nevertheless it looks wrong :) unless he has an object() method ... Commented Jul 8, 2013 at 19:42
  • Yes. Also the method doesn't return anything, it takes the wrong input parameter. I don't think this can work. Commented Jul 8, 2013 at 19:43
  • 1
    My bad, my method actually gets a list as parameter, I only tried to simplify the code. Commented Jul 8, 2013 at 19:47

3 Answers 3

2

Mon Jun 24 10:46:11 CDT 2013 and 2013-06-24 10:46:11 is actually same value. Mon Jun 24 10:46:11 CDT 2013 is as per your default locale.

You're getting confused between date's internal representation and its display format.

To print in 2013-06-24 10:46:11 you can use same SimpleDateFormat object again.

You can use DateFormat#format(Date) to print the date or return the String representation in your desired format i.e. "yyyy-MM-dd HH:mm:ss". Something like this:

String myDt = format.format(date);
// 2013-06-24 10:46:11
Sign up to request clarification or add additional context in comments.

4 Comments

But the problem is that I want the right format in a date, if I use SimpleDateformat again it will return a String.
@Blackmore: Pls read the first line of my answer. With the return value of parse method You already got a date as 2013-06-24 10:46:11 OR Mon Jun 24 10:46:11 CDT 2013. Both values are same.
I think I gave myself to misunderstood, I know 2013-06-24 10:46:11, Mon Jun 24 10:46:11 CDT 2013 are the same, the problem is the format that returns, When I got the string (2013-06-24 10:46:11) i'm inmediatley trying to parse it to Date, but when I do it returns a different format, I need the right format to get displayed in the screen. I really appreciate your help.
I thought I explained in my answer. To display a Date object in a particular format you need to call: SimpleDateFormat#format(Date) method (see example above).
0

Not the best way but a quick and easy one if you want just the string representation of the date ...

    formated = formated.substring(0, formated.length()-2);

:)

2 Comments

Then, How can I parse it to Date keeping the format? Thanks for the help.
@Blackmore The format only makes sense when you have a date represented as a String. When you parse the String to a Date, talking about "format" no longer makes any sense.
0

DateFormat i.e. SimpleDateFormat just formats the date as per your need.

Parse returns the Date representation of the String (or timestamp in your case) passed in. Format returns the String representation of the Date object passed in.

In both the cases , you see the same date just the representation is different.

1 Comment

I know, What I'm trying to do is get an specific format of my date. Thanks for the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.