-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved the date format and made the selection of files by date func…
…tional.
- Loading branch information
Mihai-Cristian Condrea
committed
Sep 27, 2024
1 parent
d470803
commit d3abe56
Showing
4 changed files
with
67 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.d4rk.cleaner.utils | ||
|
||
import java.text.SimpleDateFormat | ||
import java.util.Calendar | ||
import java.util.Date | ||
import java.util.Locale | ||
|
||
object TimeHelper { | ||
|
||
fun formatDate(date: Date): String { | ||
val today = Calendar.getInstance() | ||
val yesterday = Calendar.getInstance() | ||
yesterday.add(Calendar.DAY_OF_YEAR, -1) | ||
|
||
val calendar = Calendar.getInstance() | ||
calendar.time = date | ||
|
||
return when { | ||
calendar.get(Calendar.YEAR) == today.get(Calendar.YEAR) && | ||
calendar.get(Calendar.DAY_OF_YEAR) == today.get(Calendar.DAY_OF_YEAR) -> { | ||
"Today" | ||
} | ||
calendar.get(Calendar.YEAR) == yesterday.get(Calendar.YEAR) && | ||
calendar.get(Calendar.DAY_OF_YEAR) == yesterday.get(Calendar.DAY_OF_YEAR) -> { | ||
"Yesterday" | ||
} | ||
calendar.get(Calendar.YEAR) == today.get(Calendar.YEAR) -> { | ||
SimpleDateFormat("EEE, MMM d", Locale.getDefault()).format(date) | ||
} | ||
calendar.get(Calendar.YEAR) == today.get(Calendar.YEAR) - 1 -> { | ||
"a year ago" | ||
} | ||
else -> { | ||
val yearsAgo = today.get(Calendar.YEAR) - calendar.get(Calendar.YEAR) | ||
"$yearsAgo years ago" | ||
} | ||
} | ||
} | ||
} |