在JavaFx中使用DatePicker时遇到问题



我正在尝试转换来自日期选择器的本地日期,但遇到了问题。下面是与日期选取器相关的代码。我正在使用FXML。这应该是自包含的示例。日期的输出是这样的

日期字符串为 :17Feb2015转换为日期的日期字符串是:星期日 12 月 28 日 00:00:00 IST 2014转换为日期的本地日期是:星期二 2 月 17 日 00:00:00 IST 2015

如您所见,日期在转换中发生了变化,从本地

日期到日期到字符串到日期

控制器

public class ReportViewerController implements Initializable {
@FXML
private DatePicker datepickerfx1;
@FXML
private DatePicker datepickerfx2;
/**
 * Initializes the controller class.
 */
@Override
public void initialize(URL url, ResourceBundle rb) {
//    LocalDate date = datepickerfx1.getValue();

       datepickerfx1.setOnAction(event -> {
        LocalDate date = datepickerfx1.getValue();
        Instant instant = date.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant();
        Date res = Date.from(instant);
        DateFormat dateFormat = new SimpleDateFormat("ddMMMYYYY");
        String       pexpdateStr= dateFormat.format(res);
        Date newdate=null;
           try {
               newdate = dateFormat.parse(pexpdateStr);
           } catch (ParseException ex) {
               Logger.getLogger(ReportViewerController.class.getName()).log(Level.SEVERE, null, ex);
           }
        System.out.println("Date String is :"+pexpdateStr);
         System.out.println("DateString converted to date is :" +newdate);
        System.out.println("Local date converted to date is :"+res);
           });  
}    
}

FXML文件

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="testbed.ReportViewerController">
  <center>
  <GridPane BorderPane.alignment="CENTER">
    <columnConstraints>
      <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
      <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
    </columnConstraints>
    <rowConstraints>
      <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
      <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
      <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
    </rowConstraints>
     <children>
        <HBox prefHeight="100.0" prefWidth="200.0" GridPane.rowIndex="1">
           <children>
              <DatePicker fx:id="datepickerfx1" />
              <DatePicker fx:id="datepickerfx2" />
           </children>
        </HBox>
       </children>
      </GridPane>
    </center>
  </BorderPane>

主文件

public class TestBed extends Application {
@Override
public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("ReportViewer.fxml"));
   Rectangle2D screenBounds = Screen.getPrimary().getVisualBounds();
    Scene scene = new Scene(root, screenBounds.getWidth()/3, screenBounds.getHeight()/3);
    scene.setFill(Color.LIGHTGRAY);
    stage.setScene(scene);
    stage.show();
}
/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}

}

感谢您的代码 jay :-)这个问题很简单,但具有欺骗性,与这行有关:

DateFormat dateFormat = new SimpleDateFormat("ddMMMYYYY");

如果您仔细阅读 SimpleDateFormat 的文档,您会注意到大写的 Y 和一点 y 之间存在差异。

大 Y 也是一周属于哪一年,并且只与一年的第一周和最后几周的日期真正相关。

现在,文档显示"如果指定了周年'Y'并且日历不支持任何工作年,则改用日历年('y')。 所以这就是为什么你会得到pexpdateStr的预期结果。

但是,在

解析回去时,此规则似乎不适用,因此您会得到newdate的"不正确"结果。

所以你的意思是"ddMMMyyyy"而不是"ddMMMYYYY",一个简单的错误,它将消除不一致。

最新更新