Java 编译器问题:找不到符号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22340643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:02:47  来源:igfitidea点击:

Java Compiler Issue: cannot find symbol

javadatesymbols

提问by stretchr

I am trying to format a String into a date but getting java compiler error:

我正在尝试将字符串格式化为日期,但出现 java 编译器错误:

The fragment of code is:

代码片段是:

String value = String.valueOf(entry.getValue());
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
//String dateInString = value;
SimpleDateFormat parse = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.ENGLISH);
if (isFirst){
   Date date = parse.parse(value);
   //Then I'll just put the date variable into a cell in the html table. 

The error I am getting:

我得到的错误:

cannot find symbol

找不到标志

 [javac] symbol  : constructor SimpleDateFormat(java.lang.String,java.util.Locale)
     [javac] location: class com.lb.base.util.extra.SimpleDateFormat
     [javac]                             SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
     [javac]                                                          ^

I get this for both times I am declaring new SimpleDateFormat. I have checked that import is not the issue. Quite confused to what it is then..why am I getting this 'cannot find symbol' error?

我在两次声明新的 SimpleDateFormat 时都得到了这个。我已经检查过导入不是问题。对它是什么很困惑..为什么我会收到这个“找不到符号”的错误?

回答by stretchr

Your import seems to be off. You want to use java's SimpleDataFormat and not com.lb.base.util.extra whatever that is.

您的导入似乎已关闭。您想使用 java 的 SimpleDataFormat 而不是 com.lb.base.util.extra 。

回答by saladinxu

Apparently you're trying to import com.lb.base.util.extra.SimpleDateFormat

显然你正在尝试导入 com.lb.base.util.extra.SimpleDateFormat

Did you mean to import the java.text one? If yes try changing to below:

您是要导入 java.text 吗?如果是,请尝试更改为以下内容:

import java.text.SimpleDateFormat;

import java.text.SimpleDateFormat;

Or change the line you call the constructor:

或者更改您调用构造函数的行:

java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);

java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);

回答by aliteralmind

Something in your code, or the code your using, is very oddly expecting a non-standard version of a very standard class. You should search the source-code for this import statement:

您的代码或您使用的代码中的某些内容非常奇怪地期望非常标准类的非标准版本。您应该搜索此导入语句的源代码:

import  com.lb.base.util.extra.SimpleDateFormat;

or for a class/java file namedSimpleDateFormat.java

或者对于名为SimpleDateFormat.java的类/java 文件

This is pretty difficult to diagnose without having direct access to your computer, but it seems like you are trying to use two classes with exactly the same name.

如果不直接访问您的计算机,这很难诊断,但您似乎正在尝试使用两个名称完全相同的类。

One other thought: Instead of this

另一种想法:而不是这个

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);

try explicit package names to bypass import statements:

尝试显式包名称以绕过导入语句:

xbn.text.SimpleDateFormat formatter = new xbn.text.SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);

I am assuming here that you wantto use the standard Java versions.

我在这里假设您使用标准的 Java 版本。

Here is a link with some information that may be instructive:

这是一个包含一些可能有指导意义的信息的链接:

https://www.google.com/search?q=is+already+defined+in+a+single-type+import+%5Bjavac%5D+import

https://www.google.com/search?q=is+already+defined+in+a+single-type+import+%5Bjavac%5D+import

回答by stretchr

Thanks folks, I fixed it by commenting out the first import com.lb.base.util.extra.SimpleDateFormat

谢谢大家,我通过注释掉第一个导入来修复它 com.lb.base.util.extra.SimpleDateFormat

and adding

并添加

import java.text.SimpleDateFormat;

import java.text.SimpleDateFormat;

below it. Guess we can't let the two imports happen at the same time.

在它下面。猜猜我们不能让两个导入同时发生。