Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
336 views
in Technique[技术] by (71.8m points)

java - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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?

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...