How to change a long variable to a Timestamp in Java?

前端 未结 3 948
时光说笑
时光说笑 2021-01-17 06:29

How do I change a long variable to a Timestamp variable? I can convert it to a String but I need to convert it to Timestamp in order to use it in a database.

3条回答
  •  日久生厌
    2021-01-17 07:15

    Timestamp extends java.util.Date and it has a constructor that accepts a long.

    Like this:

    import java.sql.Timestamp;
    
    public class Main {
    
        public static void main(String[] args) {
            long inputLong = 1234567890l * 1000l;  // Constructor expects a milliseconds value
    
            Timestamp outputTimestamp = new Timestamp(inputLong);
    
            System.out.println (outputTimestamp);
        }
    
    }
    

提交回复
热议问题