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?
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.
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.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With