8

I want to use long value I got from android API that return me dates as longs, represented as milliseconds since Epoch.

I need to use methods like isBefore() plusDays() isAfter()

Cursor managedCursor = getContentResolver().query(CallLog.Calls.CONTENT_URI,
                    new String[]{"number", "type", "date", "duration", "name"},
                    null, null, strOrder);

In java I can use LocalDate and LocalTime classes to handle this, unfortunately these classes don't exist on android so what classes available on android similar to localdate and localtime?

user3341564
  • 182
  • 2
  • 17
  • 2
    you can check this question http://stackoverflow.com/questions/5369682/get-current-time-and-date-on-android – Sandeep Londhe May 14 '16 at 10:24
  • I need class that allows me to make operation on date and time like plusDays(), isAfter(), isBefore() ... etc not to get current time!!!! @AndroidHacker – user3341564 May 14 '16 at 10:46
  • See this duplicate for info about an Adroid adaptation of a back-port of much of the java.time framework built into Java 8 that supplants the troublesome old date-time classes. http://stackoverflow.com/q/22525857 – Basil Bourque May 14 '16 at 15:08
  • I suggest using this library: https://github.com/JakeWharton/ThreeTenABP . To create LocalDate from epoch ms, you can use : `Instant.ofEpochMilli(epochMilli).atZone(ZoneId.ofOffset("UTC", ZoneOffset.ofHours(0))).toLocalDate()` . You can change the end of this call to convert to whatever you wish. – android developer Jan 23 '18 at 14:32

1 Answers1

2

https://github.com/dlew/joda-time-android

This is the library that most use for time in Android. A bit heavy on size, but if that's not an issue then it's way better than using utils Android gives you

paul_hundal
  • 371
  • 2
  • 9
  • 1
    The Joda-Time team has advised migrating to the java.time framework. See the ThreeTenABP project. – Basil Bourque May 14 '16 at 15:13
  • 1
    @BasilBourque yes I saw that, however this only works well with java 8 and right now Android doesn't seem to be very backwards compatible. I suggested JodaTime as for anything Java 7 and prior, it is much better – paul_hundal May 17 '16 at 01:45
  • 1
    For using *java.time* functionality in Android, see [*How to use ThreeTenABP in Android Project*](https://stackoverflow.com/q/38922754/642706). Notice the new API desugaring feature, as well as *ThreeTenABP*. – Basil Bourque Jul 09 '20 at 21:53