Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用DateTimeFormatter正确调整时间_Java_Java 8 - Fatal编程技术网

Java 使用DateTimeFormatter正确调整时间

Java 使用DateTimeFormatter正确调整时间,java,java-8,Java,Java 8,我正在创建一个约会应用程序,它可以每月/每周查看。标准视图能够以HH:mm显示正确的时间,仅在9:00到17:00之间。但是,每月/每周我无法转换并在24小时内显示 每月我有: public LocalTime getTime() { return time; } @FXML private void viewByMonthHandler(ActionEvent event) { DataProvider.getAppointmentsByMonth

我正在创建一个约会应用程序,它可以每月/每周查看。标准视图能够以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()) {
            // 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);
    
}
    @FXML
private void viewByWeekHandler(ActionEvent event) {
    
    DataProvider.getAppointmentsByMonth().clear();
    DataProvider.getAppointmentsByWeek().clear();
    
    if(viewByWeekRadioButton.isSelected()) {
        
        DataProvider.setWeeklyView(0);
    }
    
    sortAppointment();
    viewByComboBox.setItems(viewByWeek);

}
       
我试过:

String month = new DateTimeFormatter.ofPattern("MMMM HH:mm").format(cal.getTime());
以及:

错误:

Caused by: java.lang.IllegalArgumentException: Cannot format given Object as a Date
就本周而言,我有:

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);
    
}
    @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());
    }
    
}
publicstaticvoidsetweeklyview(intweekforreference){
试一试{
ArrayList SelectedAppointsByWeek=新建ArrayList();
语句Statement=DBConnection.getConnection().createStatement();
ResultSet WeeklyAppoints=statement.executeQuery(“从约会中选择约会ID,其中年(开始)=年(日期添加(curdate(),间隔“+weekForReference+”周))和周(开始)=周(日期添加(curdate(),间隔“+weekForReference+”周));
while(WeeklyAppoints.next()){
SelectedAppointsByWeek.add(WeeklyAppoints.getInt(1));
}
对于(内部任命ID:SelectedAppointsByWeek){
结果设置selectAppointment=statement.executeQuery(“选择customer.customerName,customer.customerId,联系人,标题,类型,位置,描述,开始,结束从约会加入customer.customerId=appointment.customerId其中appointmentId=“+appointmentId”);
选择appointment.next();
预约=新预约();
String customerName=selectAppointment.getString(1);
int customerId=selectAppointment.getInt(2);
String contact=selectAppointment.getString(3);
String title=selectAppointment.getString(4);
字符串类型=selectAppointment.getString(5);
字符串位置=selectAppointment.getString(6);
String description=selectAppointment.getString(7);
String start=selectAppointment.getString(8);
String end=selectAppointment.getString(9);
预约。设置客户名称(客户名称);
预约。设置联系人(联系人);
任命.职称(职称);
预约.设置类型(类型);
预约。设置地点(地点);
任命.说明(说明);
预约。设置开始(开始);
预约。结束(结束);
任命周。添加(任命);
}
}
catch(SQLException-ex){
System.out.println(“错误”+ex.getMessage());
}
}

setMonthlyView几乎完全相同,只是将每周视图替换为每月视图。我如何格式化这些文件,使它们不再是HH:mm:ss.S,并且在9:00-17:00之间?我已经接受了之前给我的关于小部分的建议,但仍然无法理解。非常感谢您抽出时间阅读并提供帮助。

执行其中一项操作,随心所欲

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);

印刷品

July 06:11 PM
July 18:11

  • 使用
    HH
    24小时
  • 使用
    hh
    12小时(
    a
    提供AM或PM)
在指定格式化程序属性(如
MMMM
时,请记住使用
LocalDateTime
而不是
LocalTime
)。您正在
getTime()
方法中返回一个
LocalTime
对象。我会避免使用
日历
日期
,因为它们已经过时了。使用包中的类