java - 找不到方法格式的符号(DateTimeFormatter)?

标签 java compiler-errors cannot-find-symbol

我遇到了一个问题,我绝对无法解决这个问题。我对 Java(或任何语言,就此而言)没有经验,如果这是一个愚蠢的问题,请原谅。

据我所知,java.time.format.DateTimeFormatter 中的 format() 方法并没有像我想象的那样工作。我一直收到以下错误消息 (CLI):

Watch.java:609: error: cannot find symbol
                String dateTimeDisplay = dateTime.format(dateTimeFormat);
                                                 ^
  symbol:   method format(DateTimeFormatter)
  location: variable dateTime of type Object
1 error

下面是我认为相关的代码:

import java.time.ZonedDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

public class Watch {
    
    protected int hour, hourTens, hourOnes, minute, minuteTens, minuteOnes, amPM;
    
    public void watchMethod(String userInput) { 
        
        Object dateTime; // Create reference to object called dateTime
    
        switch (userInput) {
            case "1":
            ZonedDateTime dateTimeHere = ZonedDateTime.now();
            hour = dateTimeHere.getHour();
            minute = dateTimeHere.getMinute();
            // amPM is used exclusively to display AM/PM or a heart (see lines 580 to 567).
            amPM = dateTimeHere.getHour();
            dateTime = dateTimeHere;
            break;
            
            case "2":
            ZonedDateTime dateTimeThere = ZonedDateTime.now(ZoneId.of("Europe/Paris"));
            hour = dateTimeThere.getHour();
            minute = dateTimeThere.getMinute();
            // amPM is used exclusively to display AM/PM or a heart (see lines 560 to 567).
            amPM = dateTimeThere.getHour();
            dateTime = dateTimeThere;
            break; 
            
            case "3":
            hour = -1;
            minute = -1;
            break;
        }

        // There is some code that prints things to console depending on the hour, minute, and amPM variables.
        // I don't think this is relevant, but that's what's here.

        // The following code prints out the actual date and time from a ZonedDateTime object.
        DateTimeFormatter dateTimeFormat = DateTimeFormatter.ofPattern("hh:mm a 'on' EEEE, MMMM dd, yyyy");
        String dateTimeDisplay = dateTime.format(dateTimeFormat); // ERROR
        System.out.print("\nIt is currently " + dateTimeDisplay + "\n");
    }
}

这是一个类。

我已经检查过我的导入是正确的,并且没有任何拼写错误!这是与我的问题相关的其他问题让我相信是问题所在,但事实似乎并非如此。

我也不是很熟悉使用 Object 作为引用类型,所以如果这可能是导致问题的原因,我并不感到惊讶。这是我能想到的将 dateTimeHere/dateTimeThere 从 switch block 中断开的所有方法。

无论如何,如果我收到任何帮助,我将不胜感激。

快速编辑:我还认为如果没有合适的 dateTime 对象(即情况“3”),这也会导致错误。我简单地添加了一个 if/else 语句来解决这个问题,在 case "3"中有一些代码指示 dateTime 为 null,但这绝对没有任何作用。不过,如果我应该重新添加它,请告诉我。

编辑:感谢@MarsAtomic 的评论,我发现这篇文章本可以使用另一个通读。我的问题是,在我的代码末尾,我想打印出 switch 语句检索到的 ZonedDateTime 数据,无论是来自用户的计算机位置还是“巴黎/欧洲”。最终,“Paris/Europe”字符串(理想情况下)将有一些来自子类的用户输入(与 userInput 的情况一样,但该特定字符串仅用于选择已经显示的 3 种情况之一)。

最佳答案

您可能打算将 dateTime 声明为 ZonedDateTime,因为 Object 的定义不包括 format 方法。

修改后的代码:

ZonedDateTime dateTime = null; // Create reference to object called dateTime
//...
DateTimeFormatter dateTimeFormat = DateTimeFormatter.ofPattern("hh:mm a 'on' EEEE, MMMM dd, yyyy");
if (dateTime != null) {
    String dateTimeDisplay = dateTime.format(dateTimeFormat);
    System.out.print("\nIt is currently " + dateTimeDisplay + "\n");
}

关于java - 找不到方法格式的符号(DateTimeFormatter)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62987363/

相关文章:

c++ - 当我的代码在函数范围之外时,为什么会出现编译器错误 "does not name a type"?

c++ - 有没有人考虑过更多 "strict"风格的 C++,其中默认/需要初始化变量?

java - 尽管已声明变量,但无法找到符号 - 变量

Java对象序列化,无法关闭ObjectOutputStream?

java - 检查子字符串是否存在于java中的字符串数组列表中

java - 我如何将两个子字符串绑定(bind)到一个?

java - 构造函数未定义?枚举错误

java - isPalindrome 错误(找不到符号)

java - 我重写的方法无法正常工作,代码进入方法,但不执行

java - Spring MVC 3.1 - 请求头字符编码问题[UTF-8]