检测Calendar.set()上的无效日期

时间:2015-07-30 08:50:31

标签: java date

我有这个方法,它返回一个由其中一个"字段"更改的Date()。 (每年,每年)。

public static Date getDateChanged(Date date, int field, int value) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.set(field, value);
    return cal.getTime();
}

但是,当我们尝试设置的字段与相关日期不兼容时,cal.set()无法给出异常。所以,在这种情况下:

Date date = new Date();
date = getDateChanged(date, Calendar.DAY_OF_MONTH, 29); 
date = getDateChanged(date, Calendar.MONTH, 1); // feb

日期最后是2015年3月1日,因为Calendar.set()检测到2月不能设置为第29天,因此自动将日期设置为下一个可能的日期(我认为这是常规的工作方式)。

然而,就我而言,这并不是那么糟糕,

我想检测到我尝试构建的日期是不可能的,然后开始递减当天,所以在这种情况下,我想要实现的是28/02/15, 我该怎么做?

示例:

  

29/02/15 ===> 28/02/15

4 个答案:

答案 0 :(得分:2)

您可以使用cal.getActualMaximum(Calendar.DAY_OF_MONTH)查找日历实例的给定月份的最长天数。

这样,您可以自己处理上面提到的案例。

另见答案:Number of days in particular month of particular year?

答案 1 :(得分:2)

您可以使用:

public void setLenient(boolean lenient)
  

指定日期/时间解释是否宽松。   通过宽松的解释,例如" 1996年2月942和#34;将   被视为相当于2月1日后的第941天,   通过严格(非宽大)的解释,这样的日期将导致抛出异常。默认值为lenient。

Parameters:
    lenient - true if the lenient mode is to be turned on; false if it is to be turned off.
See Also:
    isLenient(), DateFormat.setLenient(boolean)

这样,当你传递错误时会出现异常:)

答案 2 :(得分:1)

您可以使用getActualMaximum检查最大值:

 <Tab closable="false" text="Bilder">
     <content>
        <GridPane>
          <columnConstraints>
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
              <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
              <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
              <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 minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          </rowConstraints>
           <children>
              <Button fx:id="addPic" mnemonicParsing="false" onAction="#choseFile" text="Bild hinzuügen" GridPane.columnIndex="4" GridPane.halignment="RIGHT" GridPane.rowIndex="4">
                 <GridPane.margin>
                    <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
                 </GridPane.margin>
              </Button>
              <ImageView fx:id="image1" fitHeight="110.0" fitWidth="141.0" pickOnBounds="true" preserveRatio="true" GridPane.hgrow="ALWAYS" GridPane.vgrow="ALWAYS">
                 <GridPane.margin>
                    <Insets />
                 </GridPane.margin></ImageView>
              <ImageView fx:id="image2" fitHeight="110.0" fitWidth="141.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="1" GridPane.hgrow="ALWAYS" GridPane.vgrow="ALWAYS">
                 <GridPane.margin>
                    <Insets />
                 </GridPane.margin></ImageView>
              <ImageView fx:id="image3" fitHeight="110.0" fitWidth="141.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="2" GridPane.hgrow="ALWAYS" GridPane.vgrow="ALWAYS">
                 <GridPane.margin>
                    <Insets />
                 </GridPane.margin></ImageView>
              <ImageView fx:id="image4" fitHeight="110.0" fitWidth="141.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="3" GridPane.hgrow="ALWAYS" GridPane.vgrow="ALWAYS">
                 <GridPane.margin>
                    <Insets />
                 </GridPane.margin></ImageView>
              <ImageView fx:id="image5" fitHeight="110.0" fitWidth="141.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="4" GridPane.hgrow="ALWAYS" GridPane.vgrow="ALWAYS">
                 <GridPane.margin>
                    <Insets />
                 </GridPane.margin></ImageView>
              <ImageView fx:id="image6" fitHeight="110.0" fitWidth="141.0" pickOnBounds="true" preserveRatio="true" GridPane.hgrow="ALWAYS" GridPane.rowIndex="1" GridPane.vgrow="ALWAYS">
                 <GridPane.margin>
                    <Insets />
                 </GridPane.margin></ImageView>
              <ImageView fx:id="image7" fitHeight="110.0" fitWidth="141.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="1" GridPane.hgrow="ALWAYS" GridPane.rowIndex="1" GridPane.vgrow="ALWAYS">
                 <GridPane.margin>
                    <Insets />
                 </GridPane.margin>
              </ImageView>
              <ImageView fx:id="image8" fitHeight="110.0" fitWidth="141.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="2" GridPane.hgrow="ALWAYS" GridPane.rowIndex="1" GridPane.vgrow="ALWAYS">
                 <GridPane.margin>
                    <Insets />
                 </GridPane.margin></ImageView>
              <ImageView fx:id="image9" fitHeight="110.0" fitWidth="141.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="3" GridPane.hgrow="ALWAYS" GridPane.rowIndex="1" GridPane.vgrow="ALWAYS">
                 <GridPane.margin>
                    <Insets />
                 </GridPane.margin></ImageView>
              <ImageView fx:id="image10" fitHeight="110.0" fitWidth="141.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="4" GridPane.hgrow="ALWAYS" GridPane.rowIndex="1" GridPane.vgrow="ALWAYS">
                 <GridPane.margin>
                    <Insets />
                 </GridPane.margin></ImageView>
              <ImageView fx:id="image11" fitHeight="110.0" fitWidth="141.0" pickOnBounds="true" preserveRatio="true" GridPane.hgrow="ALWAYS" GridPane.rowIndex="2" GridPane.vgrow="ALWAYS">
                 <GridPane.margin>
                    <Insets />
                 </GridPane.margin></ImageView>
              <ImageView fx:id="image12" fitHeight="110.0" fitWidth="141.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="1" GridPane.hgrow="ALWAYS" GridPane.rowIndex="2" GridPane.vgrow="ALWAYS">
                 <GridPane.margin>
                    <Insets />
                 </GridPane.margin></ImageView>
              <ImageView fx:id="image13" fitHeight="110.0" fitWidth="141.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="2" GridPane.hgrow="ALWAYS" GridPane.rowIndex="2" GridPane.vgrow="ALWAYS">
                 <GridPane.margin>
                    <Insets />
                 </GridPane.margin></ImageView>
              <ImageView fx:id="image14" fitHeight="110.0" fitWidth="141.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="3" GridPane.hgrow="ALWAYS" GridPane.rowIndex="2" GridPane.vgrow="ALWAYS">
                 <GridPane.margin>
                    <Insets />
                 </GridPane.margin></ImageView>
              <ImageView fx:id="image15" fitHeight="110.0" fitWidth="141.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="4" GridPane.hgrow="ALWAYS" GridPane.rowIndex="2" GridPane.vgrow="ALWAYS">
                 <GridPane.margin>
                    <Insets />
                 </GridPane.margin></ImageView>
              <ImageView fx:id="image16" fitHeight="110.0" fitWidth="141.0" pickOnBounds="true" preserveRatio="true" GridPane.hgrow="ALWAYS" GridPane.rowIndex="3" GridPane.vgrow="ALWAYS">
                 <GridPane.margin>
                    <Insets />
                 </GridPane.margin></ImageView>
              <ImageView fx:id="image17" fitHeight="110.0" fitWidth="141.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="1" GridPane.hgrow="ALWAYS" GridPane.rowIndex="3" GridPane.vgrow="ALWAYS">
                 <GridPane.margin>
                    <Insets />
                 </GridPane.margin></ImageView>
              <ImageView fx:id="image18" fitHeight="110.0" fitWidth="141.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="2" GridPane.hgrow="ALWAYS" GridPane.rowIndex="3" GridPane.vgrow="ALWAYS">
                 <GridPane.margin>
                    <Insets />
                 </GridPane.margin></ImageView>
              <ImageView fx:id="image19" fitHeight="110.0" fitWidth="141.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="3" GridPane.hgrow="ALWAYS" GridPane.rowIndex="3" GridPane.vgrow="ALWAYS">
                 <GridPane.margin>
                    <Insets />
                 </GridPane.margin></ImageView>
              <ImageView fx:id="image20" fitHeight="110.0" fitWidth="141.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="4" GridPane.hgrow="ALWAYS" GridPane.rowIndex="3" GridPane.vgrow="ALWAYS">
                 <GridPane.margin>
                    <Insets />
                 </GridPane.margin></ImageView>
           </children>
        </GridPane>
     </content>
</Tab>

答案 3 :(得分:0)

可能这个解决方案会帮助你

public static Date getDateChanged(Date date, int field, int value) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.set(field, value);
    if (field == Calendar.MONTH && cal.get(Calendar.MONTH) != value) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int month = (cal.get(Calendar.MONTH) - 1) < 0 ? 0 : (cal.get(Calendar.MONTH) - 1);
        calendar.set(Calendar.DATE, calendar.getActualMinimum(Calendar.DATE));
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
        return calendar.getTime();
    }
    return cal.getTime();
}