Java JDBC How to - Convert a Timestamp into either Date or DateTime object








Question

We would like to know how to convert a Timestamp into either Date or DateTime object.

Answer

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
/*  ww  w .  j  av a  2s .  com*/
public class Main {

    public static void main(String[] args) {
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        Date date = new Date(timestamp.getTime());

        // S is the millisecond
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy' 'HH:mm:ss:S");

        System.out.println(simpleDateFormat.format(timestamp));
        System.out.println(simpleDateFormat.format(date));
    }
}