我在 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;
}