简体   繁体   English

Java /从long到SimpleDateFormat到Timestamp

[英]Java/ From long to SimpleDateFormat and to Timestamp

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: 我现在发现在下一个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 ). 我尝试找到如何将qu转换回SimpleDateFormat,然后转换为Timestamp,或直接转换:从long转换为Timestamp(但是where long was created after these two steps )。

eariler, I ask how to convert long to Timestamp. 伯爵, 请问如何将long转换为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). 给出的答案是正确的,但我担心当我将其从SimpleDateFormat转换为long时,情况并非如此(更改后该函数不起作用)。

Thanks. 谢谢。

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 输出:2011-12-24 18:54:23.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM