I have 2 database columns in MySQL database showing start and end times of the appointments. it is stored as '2019-01-01 00:00:00'. How to I take that time (assuming it is in UTC) and convert it to local system time (for example PC set to EST) and then display it inside start and end columns of the TableView?
I setup tableview columns like this:
appTableViewStartColumn.setCellValueFactory(new PropertyValueFactory<Appointment, Calendar>("start"));
appTableViewEndColumn.setCellValueFactory(new PropertyValueFactory<Appointment, Calendar>("end"));
And this is my code for method called 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);
}
This is the method that I used to convert string to Calendar object:
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;
}