为了账号安全,请及时绑定邮箱和手机立即绑定

将一些小时/分钟添加到当前日期时间并获取未来的日期时间

将一些小时/分钟添加到当前日期时间并获取未来的日期时间

慕沐林林 2022-11-02 10:20:04
我需要获取特定时间的未来时间戳值。我将不得不向当前日期时间时间戳添加一个字符串值并获取未来的时间戳值。我正在获取当前时间戳,如下所示:Timestamp timestamp = new Timestamp(System.currentTimeMillis());我有一个字符串值"02:00:00",所以基本上我需要在这个时间上加上 2 小时并获取需要插入的未来时间戳值。例如,如果当前时间戳为:2019-04-29 16:59:21.43 且字符串为"02:00:00".我需要输出为2019-04-29 18:59:21.43.有人可以帮忙吗
查看完整描述

3 回答

?
泛舟湖上清波郎朗

TA贡献1818条经验 获得超3个赞

你可以做这样的事情


    Instant now = Instant.now();

    Duration diff = Duration.between(

            LocalTime.MIN,

            LocalTime.parse("02:00:00")

    );

    Instant res = now.plus(diff);


    System.out.println("res = " + Timestamp.from(res));


查看完整回答
反对 回复 2022-11-02
?
Smart猫小萌

TA贡献1911条经验 获得超7个赞

您可以使用 MySQL 函数TIMESTAMP将给定的时间字符串添加到您的时间戳值:

TIMESTAMP(expr), TIMESTAMP(expr1,expr2)

使用单个参数,此函数将日期或日期时间表达式 expr 作为日期时间值返回。使用两个参数,它将时间表达式 expr2 添加到日期或日期时间表达式 expr1 中,并将结果作为日期时间值返回。

mysql> SELECT TIMESTAMP('2003-12-31 12:00:00','12:00:00');
-> '2004-01-01 00:00:00'

参考


查看完整回答
反对 回复 2022-11-02
?
梵蒂冈之花

TA贡献1900条经验 获得超5个赞

插入两小时后的片刻。


myPreparedStatement                     // Use a `PreparedStatement` to exchange data with your database, to avoid SQL-injection risk. Use JDBC 4.2 or later for *java.time* support.

.setObject(                             // Fill a placeholder `?` in your SQL statement.

    … ,                                 // Specify which placeholder. 

    OffsetDateTime                      // Use `OffsetDateTime` to specify a moment in JDBC 4.2. Optionally, your JDBC might support `Instant` or `ZonedDateTime` types, while support for `OffsetDateTime` is required.

    .now(                               // Capture the current moment.

        ZoneOffset.UTC                  // Set the offset-from-UTC to zero. We do not need to account for any time zone in this particular business scenario.

    )                                   // Returns an `OffsetDateTime` object.

    .plus(                              // Adds a span-of-time to the moment held in the `OffsetDateTime` object.

        Duration.parse( "PT2H" )        // Specify the span-of-time using standard ISO 8601 format for a duration.

    )                                   // Per Immutable Objects pattern, returns a new `OffsetDateTime` rather than changing ("mutating") the original.

)                          

细节

我有一个字符串值为“02:00:00”,所以基本上我需要在这个时间上加上 2 小时并获取需要插入的未来时间戳值


这是传达与时间线无关的时间跨度的糟糕方式。


标准方式是标记开始的地方,并将年PnYnMnDTnHnMnS-月-日与小时-分钟-秒分开。所以2小时是。PTPT2H


要解析这样的字符串,请使用Durationclass for hours-minutes-seconds(或Periodfor years-months-days)。


String input = "PT2H" ;

Duration d = Duration.parse( input ) ;

您可以生成这样的字符串。


String output = Duration.ofHours( 2 ).toString() ;  // Yields "PT2H" string.

以UTC捕获当前时刻。


OffsetDateTime odt = OffsetDateTime.now( ZoneOffset.UTC ) ;

使用标准ISO 8601 符号添加两个小时的持续时间。


Duration d = Duration.parse( "PT2H" ) ;

ZonedDateTime odtLater = odt.plus( d ) ;  // Add 2 hours to the current moment.

使用 JDBC 4.2 或更高版本将其提交到您的数据库。


myPreparedStatement.setObject( … , odtLater ) ;

恢复。


OffsetDateTime odt = myResultSet.getObject( … , OffsetDateTime.class ) ;


查看完整回答
反对 回复 2022-11-02
  • 3 回答
  • 0 关注
  • 151 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信