Skip to content

Instantly share code, notes, and snippets.

@AkshayChordiya
Last active May 26, 2023 13:39
Show Gist options
  • Select an option

  • Save AkshayChordiya/b1c9788d24e62d92e3e20778288e64a8 to your computer and use it in GitHub Desktop.

Select an option

Save AkshayChordiya/b1c9788d24e62d92e3e20778288e64a8 to your computer and use it in GitHub Desktop.

Revisions

  1. AkshayChordiya revised this gist Jan 24, 2018. 2 changed files with 38 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions DateHeaderHelper.kt
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    import org.joda.time.LocalDate

    /**
    * @return true if the supplied date is in the future else false
    */
    @@ -30,12 +32,12 @@ fun isYesterday(millis: Long): Boolean {
    * @return true if the supplied when is this week else false
    */
    fun isThisWeek(millis: Long): Boolean {
    return LocalDate.now().minusWeeks(1).compareTo(LocalDate(millis)) == 0
    return LocalDate.now().minusWeeks(1) <= LocalDate(millis)
    }

    /**
    * @return true if the supplied when is this month else false
    */
    fun isThisMonth(millis: Long): Boolean {
    return LocalDate.now().minusMonths(1).compareTo(LocalDate(millis)) == 0
    return LocalDate.now().minusMonths(1) <= LocalDate(millis)
    }
    34 changes: 34 additions & 0 deletions TimeHelperTest.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    import org.junit.Assert.assertEquals
    import org.junit.Test
    import org.junit.runner.RunWith
    import org.junit.runners.JUnit4

    @RunWith(JUnit4::class)
    class TimeHelperTest {


    @Test
    fun isTodayTest() {
    // 21/01/2018 : 14:22
    assertEquals(true, isToday(1516524740355))
    }

    @Test
    fun isYesterdayTest() {
    // 20/01/2018 : 20:00
    assertEquals(true, isYesterday(1516458600000))
    }

    @Test
    fun isThisWeekTest() {
    // 18/01/2018 : 20:00
    assertEquals(true, isThisWeek(1516285800000))
    }

    @Test
    fun isThisMonthTest() {
    // 11/01/2018 : 20:00
    assertEquals(true, isThisMonth(1515681000000))
    }

    }
  2. AkshayChordiya revised this gist Jan 10, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -8,5 +8,5 @@ or older to show sections in [my app](play.google.com/store/apps/details?id=com.

    PS: I used [SectionedRecyclerView](https://github.com/afollestad/sectioned-recyclerview) library to show sections in my app.

    Dependecy:
    Dependency:
    - [Joda-time](https://github.com/dlew/joda-time-android)
  3. AkshayChordiya revised this gist Jan 10, 2018. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -6,4 +6,7 @@ There are several apps which show a list with sections like "Today", "Yesterday"
    I wrote few top-level functions which are helpful to check if the date is upcoming, tomorrow, today, yesterday, this month, this week
    or older to show sections in [my app](play.google.com/store/apps/details?id=com.bitfurther.notigo.student)

    PS: I used [SectionedRecyclerView](https://github.com/afollestad/sectioned-recyclerview) library to show sections in my app.
    PS: I used [SectionedRecyclerView](https://github.com/afollestad/sectioned-recyclerview) library to show sections in my app.

    Dependecy:
    - [Joda-time](https://github.com/dlew/joda-time-android)
  4. AkshayChordiya revised this gist Jan 10, 2018. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,9 @@
    # Date header top-level Kotlin functions

    There are several apps which show a list with sections like "Today", "Yesterday" and so on like
    (Inbox Android app)[https://lh3.googleusercontent.com/Oz13JGyH0H2XBd_sflttWnAK_LRafyHW_P59Wfc4kqasiAMLhHLyZgKBTz2MpwZbJg=h900-rw]
    [Inbox Android app](https://lh3.googleusercontent.com/Oz13JGyH0H2XBd_sflttWnAK_LRafyHW_P59Wfc4kqasiAMLhHLyZgKBTz2MpwZbJg=h900-rw)

    I wrote few top-level functions which are helpful to check if the date is upcoming, tomorrow, today, yesterday, this month, this week
    or older to show sections in (my app)[play.google.com/store/apps/details?id=com.bitfurther.notigo.student]
    or older to show sections in [my app](play.google.com/store/apps/details?id=com.bitfurther.notigo.student)

    PS: I used (SectionedRecyclerView)[https://github.com/afollestad/sectioned-recyclerview] library to show sections in my app.
    PS: I used [SectionedRecyclerView](https://github.com/afollestad/sectioned-recyclerview) library to show sections in my app.
  5. AkshayChordiya created this gist Jan 10, 2018.
    41 changes: 41 additions & 0 deletions DateHeaderHelper.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    /**
    * @return true if the supplied date is in the future else false
    */
    fun isUpcoming(millis: Long): Boolean {
    return !isTomorrow(millis) && LocalDate(millis).isAfter(LocalDate.now())
    }

    /**
    * @return true if the supplied date is tomorrow else false
    */
    fun isTomorrow(millis: Long): Boolean {
    return LocalDate.now().plusDays(1).compareTo(LocalDate(millis)) == 0
    }

    /**
    * @return true if the supplied date is today else false
    */
    fun isToday(millis: Long): Boolean {
    return LocalDate.now().compareTo(LocalDate(millis)) == 0
    }

    /**
    * @return true if the supplied when is yesterday else false
    */
    fun isYesterday(millis: Long): Boolean {
    return LocalDate.now().minusDays(1).compareTo(LocalDate(millis)) == 0
    }

    /**
    * @return true if the supplied when is this week else false
    */
    fun isThisWeek(millis: Long): Boolean {
    return LocalDate.now().minusWeeks(1).compareTo(LocalDate(millis)) == 0
    }

    /**
    * @return true if the supplied when is this month else false
    */
    fun isThisMonth(millis: Long): Boolean {
    return LocalDate.now().minusMonths(1).compareTo(LocalDate(millis)) == 0
    }
    9 changes: 9 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    # Date header top-level Kotlin functions

    There are several apps which show a list with sections like "Today", "Yesterday" and so on like
    (Inbox Android app)[https://lh3.googleusercontent.com/Oz13JGyH0H2XBd_sflttWnAK_LRafyHW_P59Wfc4kqasiAMLhHLyZgKBTz2MpwZbJg=h900-rw]

    I wrote few top-level functions which are helpful to check if the date is upcoming, tomorrow, today, yesterday, this month, this week
    or older to show sections in (my app)[play.google.com/store/apps/details?id=com.bitfurther.notigo.student]

    PS: I used (SectionedRecyclerView)[https://github.com/afollestad/sectioned-recyclerview] library to show sections in my app.
    12 changes: 12 additions & 0 deletions Usage.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    // Example usage
    when {
    isUpcoming(time) -> {}
    isTomorrow(time) -> {}
    isToday(time) -> {}
    isYesterday(time) -> {}
    isThisWeek(time) -> {}
    isThisMonth(time) -> {}
    else -> {
    // Else branch means the date is older
    }
    }