2

I'm trying to use DateTime for the first time.

I'm working with Java on IntelliJ.

I've tried writing:

DateTime dt = new DateTime();

but I get a "Cannot resolve symbol DateTime" error.

What should I do?

Dina Kleper
  • 1,949
  • 4
  • 17
  • 23

1 Answers1

5

You should import this class, add the following line in the beginning:

import org.joda.time.DateTime;

Note also that JodaTime library should be somehow available to your code, either manually connected as a jar or linked through Maven or other dependency management tool.

Scadge
  • 9,380
  • 3
  • 30
  • 39
  • Thank you! would you also mind telling me how to connet the JodaTime library? – Dina Kleper Nov 17 '15 at 07:46
  • 1
    First download the archive from https://github.com/JodaOrg/joda-time/releases and you'll get the joda-time-2.9.jar inside. After that, depending on your IDE, add this jar to your project. Example for Eclipse: http://stackoverflow.com/questions/3280353/how-to-import-a-jar-in-eclipse – Scadge Nov 17 '15 at 07:54
  • Note: [Joda-time's web-site](https://www.joda.org/joda-time/) says: **"Note that from Java SE 8 onwards, users are asked to migrate to java.time (JSR-310) - a core part of the JDK which replaces this project.)"** Which brings us back to the question of where is `DateTime` in `java.time`? – Ian Boyd Jul 15 '22 at 19:41
  • @IanBoyd, it's a bit more complicated than just changing the package. `java.time` does not contain `DateTime` class exactly, and the usage may (and probably will) differ. See some guide on migrating from Joda Time to Java 8's `java.time`, for example this one: https://blog.joda.org/2014/11/converting-from-joda-time-to-javatime.html – Scadge Jul 25 '22 at 13:24