如何将MySQL数据库约会中的时间读取为UTC,转换为本地系统时间并在JavaFX的TableView列中显示?

如何将MySQL数据库约会中的时间读取为UTC,转换为本地系统时间并在JavaFX的TableView列中显示?,java,mysql,javafx,Java,Mysql,Javafx,我在MySQL数据库中有两个数据库列,显示约会的开始和结束时间。 它存储为“2019-01-01 00:00:00”。如何计算该时间(假设是UTC)并将其转换为本地系统时间(例如PC设置为EST),然后将其显示在TableView的开始和结束列中 我设置tableview列的方式如下: appTableViewStartColumn.setCellValueFactory(new PropertyValueFactory<Appointment, Calendar>(

我在MySQL数据库中有两个数据库列,显示约会的开始和结束时间。 它存储为“2019-01-01 00:00:00”。如何计算该时间(假设是UTC)并将其转换为本地系统时间(例如PC设置为EST),然后将其显示在TableView的开始和结束列中

我设置tableview列的方式如下:

        appTableViewStartColumn.setCellValueFactory(new PropertyValueFactory<Appointment, Calendar>("start"));
        appTableViewEndColumn.setCellValueFactory(new PropertyValueFactory<Appointment, Calendar>("end"));

首先,问题中缺少部分代码(
stringToCalendar()

但是,假设您正试图以UTC/GMT格式解析字符串日期,然后利用另一时区中的日历功能,则以下操作应该有效:

// Parse date - use your format
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Date date = sdf.parse(strDate);
//instantiates a calendar using the current time in the specified timezone
Calendar cal= Calendar.getInstance(TimeZone.getTimeZone("GMT"));
cal.setTime(date);
//change the timezone
cal.setTimeZone(TimeZone.getDefault());
// or cal.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta"));
//get the current hour of the day in the new timezone
cal.get(Calendar.HOUR_OF_DAY);
//...

部分代码来自:

我假设您在数据库中使用了
DATETIME
列类型

我建议你帮自己一个忙,使用
java.time
API,而不是使用
Calendar

您可以通过
ResultSet
直接检索时间,并对
LocalDateTime
ZonedDateTime
应用适当的转换。前者的优点是在javafxapi中有一个
StringConverter
实现,但对于配置显示,两者都可以工作

数据检索

final ZoneId est = ZoneId.of("America/New_York"); // ZoneId.systemDefault();
while (result.next()){
    ...
    Timestamp time = result.getTimestamp("start"); // utc is offset 0; no offset required
    ZonedDateTime zTime = time.toInstant().atZone(est);
    LocalDateTime start = zTime.toLocalDateTime();
    ...
}
列设置

TableColumn<Appointment, LocalDateTime> appTableViewStartColumn = ...

...

final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
appTableViewStartColumn.setCellFactory(col -> new TableCell<Appointment, LocalDateTime>() {
    @Override
    protected void updateItem(LocalDateTime item, boolean empty) {
        super.updateItem(item, empty);
        setText(item == null ? "" : formatter.format(item));
    }
});
TableColumn appTableViewStartColumn=。。。
...
最终DateTimeFormatter formatter=模式的DateTimeFormatter.of(“yyyy-MM-dd HH:MM:ss”);
appTableViewStartColumn.setCellFactory(col->new TableCell()){
@凌驾
受保护的void updateItem(LocalDateTime项,布尔值为空){
super.updateItem(项,空);
setText(item==null?“:formatter.format(item));
}
});

我在StackOverflow上搜索过。有一些相关的问题可能非常相似,但它们不适合我的情况。请注意,使用
hh
只能得到表1-12中的值。这可能不是你想要的。改用
HH
。最好不要在问题中添加冒犯性的评论,这样做可能会导致帐户暂停。我道歉!谢谢大家的帮助!真的很感激!但是如何让它读取本地机器上的系统时间来计算基于此的时区偏移呢?通常,您使用getDefault获得一个时区,它根据程序运行的时区创建一个时区。From:你的代码帮助我理解了这个问题!非常感谢。你能提供一些关于如何改进答案的提示吗?它非常有效!谢谢你的帮助!为了将其反向写入数据库,我只需要按相反的顺序进行?
final ZoneId est = ZoneId.of("America/New_York"); // ZoneId.systemDefault();
while (result.next()){
    ...
    Timestamp time = result.getTimestamp("start"); // utc is offset 0; no offset required
    ZonedDateTime zTime = time.toInstant().atZone(est);
    LocalDateTime start = zTime.toLocalDateTime();
    ...
}
TableColumn<Appointment, LocalDateTime> appTableViewStartColumn = ...

...

final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
appTableViewStartColumn.setCellFactory(col -> new TableCell<Appointment, LocalDateTime>() {
    @Override
    protected void updateItem(LocalDateTime item, boolean empty) {
        super.updateItem(item, empty);
        setText(item == null ? "" : formatter.format(item));
    }
});