0

I wrote a function that got a Timestamp (year, month, day, hour, min and sec) as a parameter.

I just now find that instead of getting a Timestamp type, I got a long type - after the next stpes:

The constructor is:

protected SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");

and then:

long qu = dateFormat.parse("24.12.2011 18:54:23").getTime();

I try to find how to convert the qu, back to SimpleDateFormat and then to Timestamp, or directly: from long to Timestamp (But where long was created after these two steps).

eariler, I ask how to convert long to Timestamp. The answer was given is correct, but Im afraid that this is not the case when I convert it from SimpleDateFormat to long (the function doesn't work after the change).

Thanks.

Community
  • 1
  • 1
Adam Sh
  • 8,137
  • 22
  • 60
  • 75
  • You haven't told us what a Timestamp is in this question, and the other question is less than clear. You want to take a long and convert it to a timestamp, but you appear to have a constructor that does just that. What are you asking? – andersoj May 14 '12 at 21:03
  • OH... a java.sql.Timestamp... for some reason I thought you had rolled your own. – andersoj May 14 '12 at 21:06

1 Answers1

4

This works for me:

public class Test {
  public static void main(String... args) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    try {
      long qu = dateFormat.parse("24/12/2011 18:54:23").getTime();
      Timestamp ts = new Timestamp(qu);
      System.out.println(ts);
    } catch (ParseException e) {
      e.printStackTrace();
    }
  }
}

Outputs: 2011-12-24 18:54:23.0

GriffeyDog
  • 8,186
  • 3
  • 22
  • 34