java 如何在 Maven 中使用 Joda-Time?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34222227/
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-11-02 22:37:24  来源:igfitidea点击:

How to use Joda-Time with Maven?

javamavenjodatime

提问by Chènevis

I it seems to be a very stupid question, because Joda-Timeis easy to use that there is no explanation about how to use it in its user guide (and I don't find useful answers on the web).

我这似乎是一个非常愚蠢的问题,因为Joda-Time易于使用,在其用户指南中没有解释如何使用它(而且我在网上找不到有用的答案)。

I try to use the method of Joda-Time which can calculate the duration between two dates. I have to compare the current date with an other date.

我尝试使用 Joda-Time 的方法,它可以计算两个日期之间的持续时间。我必须将当前日期与其他日期进行比较。

I cannot use LocalDateTimebecause the class does not exist (so I created it), but I saw that "duration" need to implement ReadableDurationin the class but this interface does not exist too...

我无法使用,LocalDateTime因为该类不存在(所以我创建了它),但我看到“duration”需要ReadableDuration在类中实现,但该接口也不存在......

I tried to write :

我试着写:

DateTime dt = new DateTime(juDate);

and

public class LocalDateTime implements ReadableDuration{

    public LocalDateTime() {
    }
}

I have install Joda-Time by dependency on Maven, I have the 2.9.1 version.

我已经通过依赖Maven安装了 Joda-Time ,我有 2.9.1 版本。

<dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
        <version>2.9.1</version>
</dependency>

Can you explain to a newbie how to do, please ?

你能向新手解释一下怎么做吗?

回答by sashok_bg

java.time

时间

The time api has been reworked in JDK 8 and resembles a lot Joda-Time (I think Joda-Time is implementation of some JSR).

时间 api 已经在 J​​DK 8 中重新设计,并且很像 Joda-Time(我认为 Joda-Time 是一些 JSR 的实现)。

Citation http://www.joda.org/joda-time/

引文http://www.joda.org/joda-time/

Joda-Time is the de facto standard date and time library for Java. From Java SE 8 onwards, users are asked to migrate to java.time (JSR-310).

Joda-Time 是 Java 的事实上的标准日期和时间库。从 Java SE 8 开始,要求用户迁移到 java.time (JSR-310)。

So just create a JDK 8 project and your LocalDate and LocalTime classes should be available in package java.time

所以只需创建一个 JDK 8 项目,你的 LocalDate 和 LocalTime 类应该在包 java.time 中可用

After that just follow official oracle tutorial:

之后只需按照官方oracle教程进行操作:

http://www.oracle.com/technetwork/articles/java/jf14-date-time-2125367.html

http://www.oracle.com/technetwork/articles/java/jf14-date-time-2125367.html

GLHF

GLHF

回答by Hyman Sierkstra

Have you done the following in your pom.xml file? :

您是否在 pom.xml 文件中执行了以下操作?:

<dependency>
  <groupId>joda-time</groupId>
  <artifactId>joda-time</artifactId>
  <version>2.9.1</version>
</dependency>

Then, after you've added that entry, you have to do mvn clean install from your IDE or command-line. Then it should be available as an import in your project. If you are using another build system, I would like to refer to the following page: http://www.joda.org/joda-time/dependency-info.html.

然后,在添加该条目后,您必须从 IDE 或命令行执行 mvn clean install。然后它应该可以作为您项目中的导入使用。如果您使用其他构建系统,我想参考以下页面:http: //www.joda.org/joda-time/dependency-info.html

Don't create the classes yourself, it is a sign that the project is not imported through your build system.

不要自己创建类,这表明该项目不是通过您的构建系统导入的。

You should be able to do the following in your code above your class declaration:

您应该能够在类声明上方的代码中执行以下操作:

import org.joda.time.LocalDateTime;

Then you are able to use it.

然后你就可以使用它了。

Edit:As an addition to the above information, I can see that you accepted the answer that suggested that you should switch to Java 8. While in this situation, that might work, but what if you need other packages (dependencies) which are not available through upgrading to a higher Java version?

编辑:作为对上述信息的补充,我可以看到您接受了建议您应该切换到 Java 8 的答案。虽然在这种情况下,这可能有效,但是如果您需要其他不支持的包(依赖项)怎么办可以通过升级到更高的 Java 版本获得吗?

I suggest you should read upon build systems like Maven and Gradle. Those are mostly used in Java projects. Just playing around with it could give insights.

我建议你应该阅读像 Maven 和 Gradle 这样的构建系统。这些主要用于 Java 项目。只是玩弄它可以提供见解。