JDBC Timestamp & Date GMT issues

前端 未结 5 1094
夕颜
夕颜 2021-02-06 05:20

I have a JDBC Date column, which if a i use getDate is get the \'date\' part only 02 Oct 2009 but if i use getTimestamp i get the full \'date\

5条回答
  •  终归单人心
    2021-02-06 05:40

    You should be aware that java.util.Date (and also java.sql.Date and java.sql.Timestamp, which are subclasses of java.util.Date) don't know anything about timezones, or rather, they are always in UTC.

    java.util.Date and its subclasses are nothing more than a container for a "number of milliseconds since 01-01-1970, 12:00 AM UTC" value.

    To display a date in a specific timezone, convert it to a string by using a java.text.DateFormat object. Set the timezone on that object by calling the setTimeZone() method. For example:

    Date date = ...;  // wherever you get this from
    
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
    // Make the date format show the date in CET (central European time)
    df.setTimeZone(TimeZone.getTimeZone("CET"));
    
    String text = df.format(date);
    

提交回复
热议问题