Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 508 Vote(s) - 3.66 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get date 7 days ago to today in Kotlin?

#1
I want to get date from 7 days ago to today in Kotlin. Any suggestions? This is what I have so far

val date = Calendar.getInstance()

val yesterday = Calendar.getInstance()

yesterday.add(Calendar.DATE,-1)

var todayOrYesterday:String?

var todayDate = date.time

while (todayDate > yesterday.time){

val formatter = SimpleDateFormat("EEEE, d MMMM yyyy")

val format = formatter.format(todayDate)

println(format)

todayOrYesterday = if (DateUtils.isToday(date.timeInMillis)) {
"Today"
}else "Yesterday"

date.add(Calendar.DATE,-7)
}

Reply

#2
Simply use,

val date = Calendar.getInstance() // 19-01-2018
date.add(Calendar.DATE, -7) // 12-01-2018
Reply

#3
Use this function, pass the days ago you want:

fun getDaysAgo(daysAgo: Int): Date {
val calendar = Calendar.getInstance()
calendar.add(Calendar.DAY_OF_YEAR, -daysAgo)

return calendar.time
}
Reply

#4
An alternative is to use [Joda Time][1] ([Joda Time for Android][2]). This library has a really nice API.

DateTime.now().minusDays(7)

And you can call `.toDate()` if you need a Java-Date object.

Link: [Why Joda Time?][3]


[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through