Android date-time manipulation library

I suggest using this library instead of Joda:

https://github.com/JakeWharton/ThreeTenABP

The reasons are written there:

Why not use Joda-Time? Joda-Time has a very large API which brings with it a very large binary size and large method count. The creator of both JSR-310 and Joda-Time has also said that while Joda-Time isn't broken, it does have design flaws.

If you are using Joda-Time already, there's little reason to switch unless its size or method count is relevant to you. For new projects, however, this library offers the standard APIs in Java 8 as a much smaller package in not only binary size and method count, but also in API size.


Update:

As @basil pointed out in the comment below

The Joda-Time project is now in maintenance mode. Its creator, Stephen Colebourne, went on to lead JSR 310 defining the java.time classes built into Java 8+. See Tutorial by Oracle. – Basil Bourque 1 min ago

you can use joda-time it is an external library, Date & Calendar are basic API provided, with java 8 it has added much better api for date time but for android for now you can use joda tiem


java.time

The other Answers are outdated.

The Joda-Time framework is a highly successful and brilliant piece of work. However, its creators led by Stephen Colbourne did a complete rewrite and redesign to produce the java.time framework built into Java 8 and later.

Much of the java.time functionality has been back-ported to Java 6 & 7 in the ThreeTen-Backport project. That work is further adapted to Android in the ThreeTenABP project.

Joda-Time continues to be maintained. But the team has advised that we migrate to java.time as soon as is convenient. Future innovation work will go into java.time and its extension in the ThreeTen-Extra project.

I suggest dabbling with java.time in your fresh code. No need to delete Joda-Time. Both frameworks can coexist in your projects. Just be careful with your import statements as a few of the classes share a name between both frameworks. Later, once you are comfortable with java.time, you can think about revisiting old code to phase out use of Joda-Time.

ZonedDateTime

See the ZonedDateTime class. It has the methods you ask for, getYear, getHour, and such.

The Instant class represents a moment in UTC resolving to nanoseconds (finer than the milliseconds and microseconds used in other libraries/systems). This is the basic-building class of java.time.

I can access date-time parts (Year, Month, Seconds, etc.), the underlying Ticks, subtract dates, add time deltas and so on.

ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
ZonedDateTime zdt = ZonedDateTime.now( z ) ;
int year = zdt.getYear() ;
int hour = zdt.getHour() ;
int second = zdt.getSecond() ;
long millisecondsSinceEpochOfFirstMomentOf1970InUtc = zdt.toInstant().toEpochMilli() ;
ZonedDateTime earlier = zdt.minusDays( 3 ) ;  // Three days prior.
Duration duration = Duration.of( earlier , zdt ) ;  // Scale of 24-hour days (not attached to the calendar), hours, minutes, seconds, and fraction of second.
Period period = Period.of( earlier.toLocalDate() , zdt.toLocalDate() ) ;  // Scale of year-months-days. 

Spans of time

For spans of time not attached to the timeline, see the Period and Duration classes. The various date-time classes have math methods for adding or subtracting such objects.


Table of all date-time types in Java, both modern and legacy


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes. Hibernate 5 & JPA 2.2 support java.time.

Where to obtain the java.time classes?

  • Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
    • Java 9 brought some minor features and fixes.
  • Java SE 6 and Java SE 7
    • Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • Later versions of Android (26+) bundle implementations of the java.time classes.
    • For earlier Android (<26), a process known as API desugaring brings a subset of the java.time functionality not originally built into Android.
      • If the desugaring does not offer what you need, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above) to Android. See How to use ThreeTenABP….

Table of which java.time library to use with which version of Java or Android