英文:
Adjusting time correctly using DateTimeFormatter
问题
我正在创建一个预约应用程序,该应用程序具有查看月/周的功能。标准视图能够以HH:mm格式正确显示时间,只在9:00 - 17:00之间。但是,月/周视图无法进行转换,而是显示完整的24小时制时间。
对于月视图,我有以下代码:
public LocalTime getTime() {
return time;
}
@FXML
private void viewByMonthHandler(ActionEvent event) {
DataProvider.getAppointmentsByMonth().clear();
DataProvider.getAppointmentsByWeek().clear();
if(viewByMonthRadioButton.isSelected()) {
Calendar cal = Calendar.getInstance();
String month = new SimpleDateFormat("MMMM").format(cal.getTime());
DataProvider.setMonthlyView(month);
}
sortAppointment();
viewByComboBox.setItems(viewByMonth);
}
我尝试过以下方式:
String month = new DateTimeFormatter.ofPattern("MMMM HH:mm").format(cal.getTime());
以及:
String month = new SimpleDateFormat("MMMM").format(timeFormat);
错误信息:
Caused by: java.lang.IllegalArgumentException: Cannot format given Object as a Date
对于周视图,我有以下代码:
@FXML
private void viewByWeekHandler(ActionEvent event) {
DataProvider.getAppointmentsByMonth().clear();
DataProvider.getAppointmentsByWeek().clear();
if(viewByWeekRadioButton.isSelected()) {
DataProvider.setWeeklyView(0);
}
sortAppointment();
viewByComboBox.setItems(viewByWeek);
}
setWeeklyView
方法如下:
public static void setWeeklyView(int weekForReference) {
try {
ArrayList<Integer> selectedAppointmentsByWeek = new ArrayList<>();
Statement statement = DBConnection.getConnection().createStatement();
ResultSet weeklyAppointments = statement.executeQuery("SELECT appointmentId from appointment where year(start) = YEAR(date_add(curdate(), interval " + weekForReference + " WEEK)) and weekofyear(start) = weekofyear(date_add(curdate(),interval " + weekForReference + " WEEK));");
while(weeklyAppointments.next()) {
selectedAppointmentsByWeek.add(weeklyAppointments.getInt(1));
}
for(int appointmentId : selectedAppointmentsByWeek) {
ResultSet selectAppointment = statement.executeQuery("SELECT customer.customerName, customer.customerId, contact, title, type, location, description, start, end FROM appointment JOIN customer ON customer.customerId = appointment.customerId WHERE appointmentId =" + appointmentId);
selectAppointment.next();
Appointment appointment = new Appointment();
String customerName = selectAppointment.getString(1);
int customerId = selectAppointment.getInt(2);
String contact = selectAppointment.getString(3);
String title = selectAppointment.getString(4);
String type = selectAppointment.getString(5);
String location = selectAppointment.getString(6);
String description = selectAppointment.getString(7);
String start = selectAppointment.getString(8);
String end = selectAppointment.getString(9);
appointment.setCustomerName(customerName);
appointment.setContact(contact);
appointment.setTitle(title);
appointment.setType(type);
appointment.setLocation(location);
appointment.setDescription(description);
appointment.setStart(start);
appointment.setEnd(end);
appointmentsByWeek.add(appointment);
}
}
catch(SQLException ex) {
System.out.println("Error " + ex.getMessage());
}
}
setMonthlyView
与此几乎相同,只是将"weekly"替换为"monthly"。您可以如何格式化它们,以便不再是HH:mm:ss.S并在9:00-17:00之间?我已经采纳了之前给我的建议,但仍然无法解决问题。非常感谢您抽出时间阅读并提供帮助。
英文:
I am creating an appointment app that has the capability to view monthly/weekly. The standard view, is able to show correct times in HH:mm, only between 9:00 - 17:00 . However, the monthly/weekly I can't convert and shows in full 24 hour time.
For the monthly I have:
public LocalTime getTime() {
return time;
}
@FXML
private void viewByMonthHandler(ActionEvent event) {
DataProvider.getAppointmentsByMonth().clear();
DataProvider.getAppointmentsByWeek().clear();
if(viewByMonthRadioButton.isSelected()) {
// I was using these two to try and convert
//DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
//DateTimeFormatter timeFormat = DateTimeFormatter.ofPattern("MMMM HH:mm");
Calendar cal = Calendar.getInstance();
String month = new SimpleDateFormat("MMMM").format(cal.getTime());
DataProvider.setMonthlyView(month);
}
sortAppointment();
viewByComboBox.setItems(viewByMonth);
}
I have tried :
String month = new DateTimeFormatter.ofPattern("MMMM HH:mm").format(cal.getTime());
and:
String month = new SimpleDateFormat("MMMM").format(timeFormat);
error:
Caused by: java.lang.IllegalArgumentException: Cannot format given Object as a Date
for the weekly I have:
@FXML
private void viewByWeekHandler(ActionEvent event) {
DataProvider.getAppointmentsByMonth().clear();
DataProvider.getAppointmentsByWeek().clear();
if(viewByWeekRadioButton.isSelected()) {
DataProvider.setWeeklyView(0);
}
sortAppointment();
viewByComboBox.setItems(viewByWeek);
}
setWeeklyView:
public static void setWeeklyView (int weekForReference) {
try {
ArrayList<Integer> selectedAppointmentsByWeek = new ArrayList<>();
Statement statement = DBConnection.getConnection().createStatement();
ResultSet weeklyAppointments = statement.executeQuery("SELECT appointmentId from appointment where year(start) = YEAR(date_add(curdate(), interval " + weekForReference + " WEEK)) and weekofyear(start) = weekofyear(date_add(curdate(),interval " + weekForReference + " WEEK));");
while(weeklyAppointments.next()) {
selectedAppointmentsByWeek.add(weeklyAppointments.getInt(1));
}
for(int appointmentId : selectedAppointmentsByWeek) {
ResultSet selectAppointment = statement.executeQuery("SELECT customer.customerName, customer.customerId, contact, title, type, location, description, start, end FROM appointment JOIN customer ON customer.customerId = appointment.customerId WHERE appointmentId =" + appointmentId);
selectAppointment.next();
Appointment appointment = new Appointment();
String customerName = selectAppointment.getString(1);
int customerId = selectAppointment.getInt(2);
String contact = selectAppointment.getString(3);
String title = selectAppointment.getString(4);
String type = selectAppointment.getString(5);
String location = selectAppointment.getString(6);
String description = selectAppointment.getString(7);
String start = selectAppointment.getString(8);
String end = selectAppointment.getString(9);
appointment.setCustomerName(customerName);
appointment.setContact(contact);
appointment.setTitle(title);
appointment.setType(type);
appointment.setLocation(location);
appointment.setDescription(description);
appointment.setStart(start);
appointment.setEnd(end);
appointmentsByWeek.add(appointment);
}
}
catch(SQLException ex) {
System.out.println("Error " + ex.getMessage());
}
}
The setMonthlyView is almost identical, just replacing weekly with monthly. How could I go about formatting these so they are no longer HH:mm:ss.S and in between 9:00-17:00? I have taken the advice previously given to me for the little parts, and still can't figure it out. Thank you so much for taking the time to read and help.
答案1
得分: 1
12小时制
String localDateTime12Hour = LocalDateTime.now().format(DateTimeFormatter.ofPattern("MMMM hh:mm a"));
System.out.println(localDateTime12Hour);
24小时制
String localDateTime24Hour = LocalDateTime.now().format(DateTimeFormatter.ofPattern("MMMM HH:mm"));
System.out.println(localDateTime24Hour);
输出
七月 06:11 PM
七月 18:11
- 用
HH
表示24小时制 - 用
hh
表示12小时制(a
表示AM或PM)
记住在指定格式化器属性(如 MMMM
)时使用 LocalDateTime
,而不是 LocalTime
。在 getTime()
方法中,您返回了一个 LocalTime
对象。我建议避免使用 Calendar
和 Date
,因为它们已经过时。请使用 java.time 包中的类。
英文:
Do one of these do what you want.
12 hour
String localDateTime12Hour =
LocalDateTime.now().format(DateTimeFormatter.ofPattern("MMMM hh:mm a"));
System.out.println(localDateTime12Hour);
24 hour
String localDateTime24Hour =
LocalDateTime.now().format(DateTimeFormatter.ofPattern("MMMM HH:mm"));
System.out.println(localDateTime24Hour);
Prints
July 06:11 PM
July 18:11
- Use
HH
for 24 hour - Use
hh
for 12 hour (thea
provides AM or PM)
Remember to use LocalDateTime
and not LocalTime
when specifying formatter attributes like MMMM
. You are returning a LocalTime
object in your getTime()
method. And I would avoid using Calendar
and Date
as they are outmoded. Use classes from the java.time package.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论