有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何转换GMT?

我试图从数据库中获取数据,如

       while(rs.next())
               {
                java.sql.Timestamp t=rs.getTimestamp("date");

                }

现在我想把时间戳“t”转换成java。util。日期类型。 在这方面我正在努力

Date d=new Date(t);//but hear given error
System.out.println(d);
System.out.println(d.toGMTString());

共 (1) 个答案

  1. # 1 楼答案

    ^{}已经扩展了^{},因此您无需执行任何操作:

    Date date = t;
    

    您应该阅读Timestamp的文档,了解一些潜在意外行为的详细信息,尤其是:

    Due to the differences between the Timestamp class and the java.util.Date class mentioned above, it is recommended that code not view Timestamp values generically as an instance of java.util.Date. The inheritance relationship between Timestamp and java.util.Date really denotes implementation inheritance, and not type inheritance.

    (我将其翻译为“哦,我们在应该使用组合的地方使用了继承。”)

    要解决此问题,您可能需要使用:

    Date date = new Date(t.getTime());