1

I'm building a simple organizer app and trying to pick a class to keep my date and time in object. Which classes should I use? I've seen that lots of methods in Date class are deprecated. Maybe Calendar then? Recommend something. Thanks.

Twinkle_Monkey
  • 185
  • 1
  • 4
  • 14
  • 1
    How about the classes in `java.time.*` package? – Kayaman Jul 19 '17 at 19:21
  • The new ones? But how about backward support? – Twinkle_Monkey Jul 19 '17 at 19:23
  • 3
    Why do you need backwards support? – Rabbit Guy Jul 19 '17 at 19:23
  • 1
    If you're using **Java <= 7**, you can use the [ThreeTen Backport](http://www.threeten.org/threetenbp/). And if you still need to work with `Date` or `Calendar`, check this: https://docs.oracle.com/javase/tutorial/datetime/iso/legacy.html –  Jul 19 '17 at 19:23
  • Do you really think you need backwards support? You're building a simple organizer app, for which you'll most likely be the only user. There are thousands of organizers on the market already. – Kayaman Jul 19 '17 at 19:24
  • I guess, I just don't fully understand then. If I use JDK8 in my project, all devices will support it? – Twinkle_Monkey Jul 19 '17 at 19:25
  • 1
    In Android, you can also use the [ThreeTen Backport](http://www.threeten.org/threetenbp/) with the [ThreeTenABP project](https://github.com/JakeWharton/ThreeTenABP) (more on how to use it [here](https://stackoverflow.com/a/38922755/7605325)) –  Jul 19 '17 at 19:26
  • 2
    Experiences with trouble, unexpected behaviour, very hard debugging and unclear code with the outdated classes, including `Calendar`, are plentyful. There is a reason why so many here are talking about the modern API (known as JSR-310 or `java.time`). – Ole V.V. Jul 19 '17 at 19:52
  • They are not that new, @Twinkle_Monkey. Out since March 2014. – Ole V.V. Jul 19 '17 at 19:53

1 Answers1

3

It will depend on what kind of date / time you want.

  • System.currentTimeMillis() - this will give you a simple numeric value expressed as the number of miliseconds after the UNIX epoch as a long.
  • If you want year, month day format you could use one of these:

    new Date() - this will give you a Date object initialized with current date / time. This is deprecated as the API methods are mostly flawed.

    new org.joda.time.DateTime() - this will give you a joda-time object initialized with the current date / time. Joda-time includes a great API for doing anything involving time point an duration calculations. With Java 8, however, this is no longer true

    Calendar.getInstance() - this gives you a Calendar object initialized witht he current date / time, using the default locale and TimeZone.

  • If you just need a simple way to output a time stamp in format YYYY.MM.DD-HH.MM.SS - a very frequent case - then use this:

    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());
    
  • Instant - if you want to do any work with business logic and data storage/exchange it should be done in UTC, as a best practice. To get the current moment in nanoseconds use the Instant class:

    Instant instant = Instant.now();
    

You can also adjust that Instant into other time zones. Apply a ZoneId object to get a ZonedDateTime.

ZoneId zid = ZoneId.of("Europe/Paris"); //create zone id
ZonedDateTime zdt = instant.atZone(zid); //pass zone id get the adjusted zoned date time.
  • LocalDate - this class represents a date-only value without time-of-day and without time zone.

    ZoneId zid = ZoneId.of("Europe/Paris");
    LocalDate today = LocalDate.now(zid); //Always pass in a time zone
    
Logan M
  • 348
  • 2
  • 12
  • 5
    Please don’t teach the young ones the outdated classes `Date` and `Calendar`, at least not as the first option and not without a warning or reservation. `Instant`, `LocalDate`, `ZonedDateTime` and friends are so much better. – Ole V.V. Jul 19 '17 at 20:26