如何在LocalTime中设置必要的时区

huangapple go评论69阅读模式
英文:

How to set the necessary timezone in LocalTime

问题

我有一个数据库,其中有电影排片信息。当然,有两个这样的列:

CREATE TABLE tableName (
    hall_movies_schedules_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
    hall_id BIGINT UNSIGNED NOT NULL,
    film_id BIGINT UNSIGNED NOT NULL,
    start_at TIME NOT NULL,
    end_at TIME NOT NULL,
    date DATE NOT NULL,
    cost INT UNSIGNED NOT NULL,
    FOREIGN KEY(hall_id) REFERENCES cinema_halls(hall_id),
    FOREIGN KEY(film_id) REFERENCES films(film_id),
    PRIMARY KEY (hall_movies_schedules_id)
);

我在Java代码中有一个实体:

@Entity
@Table(name="tableName")
public class HallMoviesSchedule {

    ...
    @Column(nullable = false, name = "start_at")
    @DateTimeFormat(iso = DateTimeFormat.ISO.TIME)
    private LocalTime startAt;

    @Column(nullable = false, name = "end_at")
    @DateTimeFormat(iso = DateTimeFormat.ISO.TIME)
    private LocalTime endAt;
    ...

}

但是在我的网页上提取信息时,显示的时间比数据库中的时间少了一个小时,这该如何修复?

英文:

I have a database which has a movie schedule. Well, of course there are two such columns:

CREATE TABLE tableName (
	hall_movies_schedules_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
	hall_id	 BIGINT UNSIGNED NOT NULL,
    film_id	 BIGINT UNSIGNED NOT NULL,
    start_at TIME 	NOT NULL,
    end_at	 TIME 	NOT NULL,
    date	 DATE 	NOT NULL,
    cost	 INT 	UNSIGNED NOT NULL,
    FOREIGN KEY(hall_id) REFERENCES cinema_halls(hall_id),
    FOREIGN KEY(film_id) REFERENCES films(film_id),
    PRIMARY KEY (hall_movies_schedules_id)
);	

I have entity in java code:

@Entity
@Table(name="tableName")
public class HallMoviesSchedule {

    ...
    @Column(nullable = false, name = "start_at")
    @DateTimeFormat(iso = DateTimeFormat.ISO.TIME)
    private LocalTime startAt;

    @Column(nullable = false, name = "end_at")
    @DateTimeFormat(iso = DateTimeFormat.ISO.TIME)
    private LocalTime endAt;
    ...

}

But pulling information to my web page gives one hour less than is in the database for my region. How can this be fixed?

答案1

得分: 2

OffsetTime

还有一个名为OffsetTime的类。但是那个类对我和其他人来说都没有意义。我猜想这个类仅仅被包含在 java.time 中是为了提供一个与SQL标准中定义的某种类型对应的类。在SQL中的那种类型也没有意义。我只是为了完整性而提到这些类型;我不建议使用它们。

LocalTime

对于标准SQL中的TIME和Java中的LocalTime,该值通常表示一天中的时间,没有任何时区或偏移量的概念。因此,在其存储中不应涉及任何时区。如果你将中午十二点 12:00:00.0 写入数据库,你应该能够获得中午十二点。

myPreparedStatement.setObject( … , myLocalTime ) ;

以及检索。

LocalTime myLocalTime = myResultSet.getObject( … , LocalTime.class ) ;

ZonedDateTime

如果你正在销售电影院的某个场次的电影票,你必须将那个时间与日期和时区结合起来。

ZoneId z = ZoneId.of( "Africa/Tunis" );
LocalDate tomorrow = LocalDate.now(z).plusDays(1);
ZonedDateTime zdtTicket = ZonedDateTime.of(tomorrow, myLocalTime, z);

在UTC中查看同一时刻。

Instant instant = zdtTicket.toInstant();
英文:

OffsetTime

There is also a class OffsetTime. But that class makes no sense to me and to other folks. I presume he class was included in java.time merely to provide a counterpart to such a type defined in the SQL standard. The type in SQL also makes no sense. I mention these types only for the sake of completeness; I do not recommend their use.

LocalTime

For both TIME in standard SQL and LocalTime in Java, the value represents a time-of-day generically, without any concept of time zone or offset. So there should be no time zones involved in its storage. If you write twelve noon 12:00:00.0 to the database, you should get back twelve noon.

myPreparedStatement.setObject( … , myLocalTime ) ;

And retrieval.

LocalTime myLocalTime = myResultSet.getObject( … , LocalTime.class ) ;

ZonedDateTime

If you are selling tickets to a particular showing of a movie in a cinema, you must combine that time of day with a date and a time zone.

ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
LocalDate tomorrow = LocalDate.now( z ).plusDays( 1 ) ;
ZonedDateTime zdtTicket = ZonedDateTime.of( tomorrow , myLocalTime , z ) ;

See that same moment in UTC.

Instant instant = zdtTicket.toInstant() ;

答案2

得分: 1

LocalTime没有TimeZone。您需要使用ZonedDateTime代替。

英文:

LocalTime does not have TimeZone. You have to use ZonedDateTime instead.

huangapple
  • 本文由 发表于 2020年4月3日 22:22:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/61013993.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定