How can I convert a Timestamp into either Date or DateTime object?

前端 未结 4 908
攒了一身酷
攒了一身酷 2020-12-15 06:06

I\'m retrieving a timestamp object from a database using ResultSet.getTimestamp(), but I\'d like an easy way to get the date in the format of MM/DD/YYYY

相关标签:
4条回答
  • 2020-12-15 06:27

    You can also get DateTime object from timestamp, including your current daylight saving time:

    public DateTime getDateTimeFromTimestamp(Long value) {
        TimeZone timeZone = TimeZone.getDefault();
        long offset = timeZone.getOffset(value);
        if (offset < 0) {
            value -= offset;
        } else {
            value += offset;
        }
        return new DateTime(value);
    }    
    
    0 讨论(0)
  • 2020-12-15 06:30

    java.sql.Timestamp is a subclass of java.util.Date. So, just upcast it.

    Date dtStart = resultSet.getTimestamp("dtStart");
    Date dtEnd = resultSet.getTimestamp("dtEnd");
    

    Using SimpleDateFormat and creating Joda DateTime should be straightforward from this point on.

    0 讨论(0)
  • 2020-12-15 06:32
    import java.sql.Timestamp;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class DateTest {
    
        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));
        }
    }
    
    0 讨论(0)
  • 2020-12-15 06:48

    java.time

    Modern answer: use java.time, the modern Java date and time API, for your date and time work. Back in 2011 it was right to use the Timestamp class, but since JDBC 4.2 it is no longer advised.

    For your work we need a time zone and a couple of formatters. We may as well declare them static:

    static ZoneId zone = ZoneId.of("America/Marigot");
    static DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("MM/dd/uuuu");
    static DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm xx");
    

    Now the code could be for example:

        while(resultSet.next()) {
            ZonedDateTime dtStart = resultSet.getObject("dtStart", OffsetDateTime.class)
                     .atZoneSameInstant(zone);
    
            // I would like to then have the date and time
            // converted into the formats mentioned...
            String dateFormatted = dtStart.format(dateFormatter);
            String timeFormatted = dtStart.format(timeFormatter);
            System.out.format("Date: %s; time: %s%n", dateFormatted, timeFormatted);
        }
    

    Example output (using the time your question was asked):

    Date: 09/20/2011; time: 18:13 -0400

    In your database timestamp with time zone is recommended for timestamps. If this is what you’ve got, retrieve an OffsetDateTime as I am doing in the code. I am also converting the retrieved value to the user’s time zone before formatting date and time separately. As time zone I supplied America/Marigot as an example, please supply your own. You may also leave out the time zone conversion if you don’t want any, of course.

    If the datatype in SQL is a mere timestamp without time zone, retrieve a LocalDateTime instead. For example:

            ZonedDateTime dtStart = resultSet.getObject("dtStart", LocalDateTime.class)
                     .atZone(zone);
    

    No matter the details I trust you to do similarly for dtEnd.

    I wasn’t sure what you meant by the xx in HH:MM xx. I just left it in the format pattern string, which yields the UTC offset in hours and minutes without colon.

    Link: Oracle tutorial: Date Time explaining how to use java.time.

    0 讨论(0)
提交回复
热议问题