繁体   English   中英

如何将 Timestamp 转换为 Date 或 DateTime 对象?

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

我正在使用ResultSet.getTimestamp()从数据库中检索时间戳对象,但我想要一种简单的方法来获取格式为MM/DD/YYYY的日期和格式为HH:MM xx的时间。 我正在修补,看起来我可以通过使用 Java 中的 Date 和/或 DateTime 对象来做到这一点。 这是最好的方法,还是我什至需要转换时间戳来完成这个? 任何建议都会有所帮助。

....
while(resultSet.next()) {
    Timestamp dtStart = resultSet.getTimestamp("dtStart");
    Timestamp dtEnd = resultSet.getTimestamp("dtEnd");

    // I would like to then have the date and time
    // converted into the formats mentioned...
    ....
}
....
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));
    }
}

java.sql.Timestampjava.util.Date的子类。 所以,只是向上推它。

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

从这一点开始,使用SimpleDateFormat和创建 Joda DateTime应该很简单。

您还可以从时间戳获取 DateTime 对象,包括您当前的夏令时:

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);
}    

时间

现代答案:使用 java.time,现代 Java 日期和时间 API,用于您的日期和时间工作。 早在 2011 年,使用Timestamp类是正确的,但从 JDBC 4.2 开始,不再建议使用它。

对于您的工作,我们需要一个时区和几个格式化程序。 我们也可以将它们声明为静态:

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

现在代码可以是例如:

    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);
    }

示例输出(使用您提出问题的时间):

日期:09/20/2011; 时间:18:13 -0400

在您的数据库中timestamp with time zone建议timestamp with time zone时间戳作为时间戳。 如果这是您所拥有的, OffsetDateTime像我在代码中所做的那样检索OffsetDateTime 在分别格式化日期和时间之前,我还将检索到的值转换为用户的时区。 作为时区,我以 America/Marigot 为例,请提供您自己的时区。 当然,如果您不想要时区转换,也可以省略。

如果 SQL 中的数据类型只是没有时区的timestamp ,则取而代之的是检索LocalDateTime 例如:

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

不管细节如何,我相信你会对dtEnd做类似的dtEnd

我不确定你所说的HH:MM xxxx是什么意思。 我只是把它留在格式模式字符串中,它以小时和分钟为单位产生 UTC 偏移量,没有冒号。

链接: Oracle 教程:解释如何使用 java.time 的日期时间

LocalDateTime dtStart = rs.getTimestamp("dtStart").toLocalDateTime();<\/code>

将此 Timestamp 对象转换为代码 LocalDateTime。转换会创建一个代码 LocalDateTime,它表示与本地时区中的此代码 Timestamp 相同的年、月、日、小时、分钟、秒和 nanos 日期时间值。

从 1.8 开始

"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM