Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android date-time manipulation library

Coming from C#, I got used to the slick interface DateTime and TimeSpan offer. With these, I can access date-time parts (Year, Month, Seconds, etc.), the underlying Ticks, subtract dates, add time deltas and so on.

As a Java/Android newbie, I understand that the equivalent Java Date class used to have these accessors, but they were removed for back-compatibility reasons. The suggested alternative is to use a Calendar object, but I find its interface cumbersome compared with .Net's.

Is there an equivalent Java library to ease the manipulation of Date objects in Android?

like image 898
bavaza Avatar asked Mar 20 '14 07:03

bavaza


Video Answer


1 Answers

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

like image 154
Basil Bourque Avatar answered Sep 21 '22 08:09

Basil Bourque