Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
AloqaIM-Android
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
AloqaIM-Android
Commits
9f1ce5b9
Commit
9f1ce5b9
authored
Nov 22, 2017
by
Filipe de Lima Brito
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add DateTimeHelper.kt
parent
8495618f
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
51 additions
and
0 deletions
+51
-0
DateTimeHelper.kt
app/src/main/java/chat/rocket/android/app/DateTimeHelper.kt
+51
-0
No files found.
app/src/main/java/chat/rocket/android/app/DateTimeHelper.kt
0 → 100644
View file @
9f1ce5b9
import
android.content.Context
import
chat.rocket.android.R
import
org.threeten.bp.DayOfWeek
import
org.threeten.bp.LocalDate
import
org.threeten.bp.LocalDateTime
import
org.threeten.bp.Period
import
org.threeten.bp.format.DateTimeFormatter
import
org.threeten.bp.format.FormatStyle
import
org.threeten.bp.temporal.ChronoUnit
object
DateTimeHelper
{
private
val
today
=
LocalDate
.
now
()
private
val
yesterday
=
today
.
minusDays
(
1
)
private
val
lastWeek
=
today
.
minus
(
1
,
ChronoUnit
.
WEEKS
)
/**
* Returns a date or the correlated expression from a LocalDateTime.
* REMARK: this will return "today", "yesterday", the day of the week or the localDate if the LocalDateTime is from a week ago.
*
* @param localDateTime The LocalDateTime.
* @param context The context.
* @return The date or the correlated expression from a LocalDateTime.
*/
fun
getDate
(
localDateTime
:
LocalDateTime
,
context
:
Context
):
String
{
val
localDate
=
localDateTime
.
toLocalDate
()
return
when
(
localDate
)
{
today
->
localDateTime
.
toLocalTime
().
toString
()
yesterday
->
context
.
getString
(
R
.
string
.
msg_yesterday
)
else
->
{
if
(
Period
.
between
(
lastWeek
,
localDate
).
days
<=
0
)
{
formatDate
(
localDate
)
}
else
{
when
(
localDateTime
.
dayOfWeek
)
{
DayOfWeek
.
MONDAY
->
context
.
getString
(
R
.
string
.
msg_monday
)
DayOfWeek
.
TUESDAY
->
context
.
getString
(
R
.
string
.
msg_tuesday
)
DayOfWeek
.
WEDNESDAY
->
context
.
getString
(
R
.
string
.
msg_wednesday
)
DayOfWeek
.
THURSDAY
->
context
.
getString
(
R
.
string
.
msg_thursday
)
DayOfWeek
.
FRIDAY
->
context
.
getString
(
R
.
string
.
msg_friday
)
DayOfWeek
.
SATURDAY
->
context
.
getString
(
R
.
string
.
msg_saturday
)
else
->
context
.
getString
(
R
.
string
.
msg_sunday
)
}
}
}
}
}
private
fun
formatDate
(
localDate
:
LocalDate
):
String
{
val
formatter
=
DateTimeFormatter
.
ofLocalizedDate
(
FormatStyle
.
SHORT
)
return
localDate
.
format
(
formatter
).
toString
()
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment