【问题标题】:Java Gregorian calendar formattingJava 公历格式
【发布时间】:2018-08-31 12:57:20
【问题描述】:

我有一个露营程序,它有一个对话框,用户可以在其中输入他们要签入的日期以及他们计划签出的日期到一个文本字段中。一旦他们在对话框上按下确定,他们输入的日期就应该可以在 JTable 中查看。目前,在日期列中,当用户按下确定时,我得到一个 java.utl.GregorianCalendar[time....etc]。姓名、停留天数和站点编号等其他信息显示正常。显然它没有正确格式化日期,但我不确定如何修复它。这是 DialogCheckInTent 类中公历的代码:

 public GregorianCalendar getDateIn(){
    DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
    GregorianCalendar g = new GregorianCalendar();
     try{Date date = df.parse(dateIn.getText()); 
     g.setTime(date);
     }
     catch (ParseException ex) {
            ex.printStackTrace();
        }
    return g;
}

public GregorianCalendar getCheckOutDate(){
    DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
    GregorianCalendar g = new GregorianCalendar();
     try{Date date = df.parse(checkOutTxt.getText()); 
     g.setTime(date);
     }
     catch (ParseException ex) {
            ex.printStackTrace();
        }
    return g;
}

当用户按下确定时,上面的代码在 GUI 中的这个方法中使用。

 if (pressed == checkInTentItem) {
        newTent.clear();
        newTent.setVisible(true);
        if (model.getRowCount() < 5) {
            if (newTent.isOk()) {
                String nameReserving = newTent.getNameReserve();
                GregorianCalendar checkIn = newTent.getDateIn();
                int daysStaying = newTent.getDaysStaying();
                GregorianCalendar checkOutOn = newTent.getCheckOutDate();
                int siteNumber = newTent.getSiteNumber();
                int tenters = newTent.getTenters();
                Tent tent = new Tent(nameReserving, checkIn, daysStaying,
                        checkOutOn, siteNumber, tenters);
                model.add(tent);
            }

        }
        else {
            JOptionPane.showMessageDialog(frame, "All sites are full.",
                    " ", JOptionPane.INFORMATION_MESSAGE);
        }
    }

一切编译都没有问题,但日期工作不正常。我该如何解决这个问题?

【问题讨论】:

    标签: java date datetime gregorian-calendar


    【解决方案1】:
    DateFormat df = new SimpleDateFormat("mm/dd/yyyy");
    

    格式不正确

    月份用M表示,所以把它改成MM/dd/yyyy

    也不确定pressed 和checkInTentItem 的类型是什么

    if (pressed == checkInTentItem) {
    

    【讨论】:

    • public void actionPerformed(ActionEvent evt) { 按下的对象 = evt.getSource();
    • 我将格式更改为 MM,但仍然出现相同的错误。
    • 不应通过== 检查对象是否相等,使用equals() 并正确实现equals() 方法
    • @TABmayfield 如果您认为它正确并解决了您的问题,请接受此答案。如果您看到一个,请点击大的空绿色复选标记。
    【解决方案2】:

    java.time

    Answer by Joshi 是正确的,应该被接受。

    现代解决方案使用LocalDate 类。该类是 java.time 类的一部分,现在取代了麻烦的旧日期时间类,例如 GregorianCalendar、Date 和 SimpleDateFormat。

    将您的输入解析为LocalDate。

    DateTimeFormatter f = DateTimeFormatter.ofPattern( "MM/dd/uuuu" , Locale.US ) ;
    LocalDate ld = LocalDate.parse( "11/29/2017" ) ;
    

    关于java.time

    java.time 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧 legacy 日期时间类,例如 java.util.Date、Calendar 和 SimpleDateFormat。

    Joda-Time 项目现在位于maintenance mode,建议迁移到java.time 类。

    要了解更多信息,请参阅Oracle Tutorial。并在 Stack Overflow 上搜索许多示例和解释。规格为JSR 310。

    您可以直接与您的数据库交换 java.time 对象。使用符合JDBC 4.2 或更高版本的JDBC driver。不需要字符串,不需要java.sql.* 类。

    从哪里获得 java.time 类?

    ThreeTen-Extra 项目通过附加类扩展了 java.time。该项目是未来可能添加到 java.time 的试验场。您可以在这里找到一些有用的类,例如Interval、YearWeek、YearQuarter 和more。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-13
      • 1970-01-01
      • 2020-01-28
      • 1970-01-01
      相关资源
      最近更新 更多