-2

我在 MySQL 数据库中有 2 个数据库列,显示约会的开始和结束时间。它存储为“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"));

这是我的名为 getAllAppointments 的方法的代码:

    public static ObservableList<Appointment> getAllAppointments() throws SQLException, Exception {
        DatabaseConnection.makeConnection();
        String sqlStatement = "SELECT appointmentId, customerId, type, start, end, customerName FROM appointment LEFT JOIN customer " +
                "USING (customerId)";
        Query.makeQuery(sqlStatement);
        ResultSet result = Query.getResult();

        while (result.next()) {
            int appointmentId = result.getInt("appointmentId");
            int customerIdIn = result.getInt("customerId");
            String type = result.getString("type");
            String customerName = result.getString("customerName");

            // Following gets date as string, then converts it to Calendar
            String startString = result.getString("start");
            String endString = result.getString("end");
            Calendar start = stringToCalendar(startString);
            Calendar end = stringToCalendar(endString);

            Appointment appointmentResult = new Appointment (appointmentId, customerIdIn, type, start, end, customerName);
            allAppointments.add(appointmentResult);
        }

这是我用来将字符串转换为 Calendar 对象的方法:

    public static Calendar stringToCalendar (String stringDate) throws ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date date = sdf.parse(stringDate);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    }

4

2 回答 2

1

我假设您使用DATETIME数据库中的列类型。

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

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

数据检索

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));
    }
});
于 2020-02-07T16:54:48.170 回答
0

首先,问题(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);
//...

部分代码来自:How to change TIMEZONE for a java.util.Calendar/Date

于 2020-02-07T15:28:30.633 回答