Time Difference calculation Android and iOS - android

I have a web site running on IIS that will take two time variables, calculate those variables and produce a time differnce in -Minutes (eg StartTime = "18:00", EndTime = "18:30", Diff = "-30")
I have this working perfectly in VBScript using the DateDiff() function. I have searched high and low, looking for a similar solution for both Android and iOS so I can build the time calculation into my mobile app and have it available off line(if there is not internet connectivity for example).
This is part of the code I use in VBScript:
//Grab the values from the form controls
occtime = occDate.Value //from form controls
testTime = testDate.Value //from form controls
//work out the time difference in minutes. Should always a negative number.
timeCalc = DateDiff("n", testTime, occtime)
Using either a time variable, HH:MM or using a full date dd/mm/yyyy HH:MM will give me the result I need, however I'm struggling getting this to work in either iOS(Swift) or Android Studio.
Thanks in Advance for any help.

For iOS please try this Swift code in the XCode playgorund:
import Foundation
let curerntDate = NSDate()
let futureDate = NSDate(timeInterval: 36*60*60, sinceDate: curerntDate)
let currentCalendar = NSCalendar.currentCalendar()
let differenceDateComponents = currentCalendar.components([.Era, .Year, .Month, .Day, .Hour, .Minute, .Second], fromDate: curerntDate, toDate: futureDate, options: NSCalendarOptions(rawValue: 0))
print(differenceDateComponents)

Swift 2.2 compatible extension:
extension NSDate {
func yearsFrom(date:NSDate) -> Int{
return NSCalendar.currentCalendar().components(.Year, fromDate: date, toDate: self, options: []).year
}
func monthsFrom(date:NSDate) -> Int{
return NSCalendar.currentCalendar().components(.Month, fromDate: date, toDate: self, options: []).month
}
func weeksFrom(date:NSDate) -> Int{
return NSCalendar.currentCalendar().components(.WeekOfYear, fromDate: date, toDate: self, options: []).weekOfYear
}
func daysFrom(date:NSDate) -> Int{
return NSCalendar.currentCalendar().components(.Day, fromDate: date, toDate: self, options: []).day
}
func hoursFrom(date:NSDate) -> Int{
return NSCalendar.currentCalendar().components(.Hour, fromDate: date, toDate: self, options: []).hour
}
func minutesFrom(date:NSDate) -> Int{
return NSCalendar.currentCalendar().components(.Minute, fromDate: date, toDate: self, options: []).minute
}
func secondsFrom(date:NSDate) -> Int{
return NSCalendar.currentCalendar().components(.Second, fromDate: date, toDate: self, options: []).second
}
func offsetFrom(date:NSDate) -> String {
if yearsFrom(date) > 0 { return "\(yearsFrom(date))y" }
if monthsFrom(date) > 0 { return "\(monthsFrom(date))M" }
if weeksFrom(date) > 0 { return "\(weeksFrom(date))w" }
if daysFrom(date) > 0 { return "\(daysFrom(date))d" }
if hoursFrom(date) > 0 { return "\(hoursFrom(date))h" }
if minutesFrom(date) > 0 { return "\(minutesFrom(date))m" }
if secondsFrom(date) > 0 { return "\(secondsFrom(date))s" }
return ""
}
}
For Android these methods working good:
public static int calcDaysDiff(Date day1, Date day2) {
Date d1 = new Date(day1.getTime());
Date d2 = new Date(day2.getTime());
Calendar date1 = Calendar.getInstance();
date1.setTime(d1);
Calendar dateCpy = (Calendar) date1.clone();
Calendar date2 = Calendar.getInstance();
date2.setTime(d2);
//checks if the start date is later then the end date - gives 0 if it is
if (date1.get(Calendar.YEAR) >= date2.get(Calendar.YEAR)) {
if (date1.get(Calendar.DAY_OF_YEAR) >= date2.get(Calendar.DAY_OF_YEAR)) {
return 0;
}
}
//checks if there is a daylight saving change between the two dates
boolean isDate1Summer = TimeZone.getDefault().inDaylightTime(d1);
boolean isDate2Summer = TimeZone.getDefault().inDaylightTime(d2);
int offset = 0;
//check if there as been a change in winter/summer time and adds/reduces an hour
if (isDate1Summer && !isDate2Summer) {
offset = 1;
}
if (!isDate1Summer && isDate2Summer) {
offset = -1;
}
return calcDaysDiffAux(dateCpy, date2) + checkFullDay(dateCpy, date2, offset);
}
// check if there is a 24 hour diff between the 2 dates including the daylight saving offset
public static int checkFullDay(Calendar day1, Calendar day2, int offset) {
if (day1.get(Calendar.HOUR_OF_DAY) <= day2.get(Calendar.HOUR_OF_DAY) + offset) {
return 0;
}
return -1;
}
// find the number of days between the 2 dates. check only the dates and not the hours
public static int calcDaysDiffAux(Calendar day1, Calendar day2) {
Calendar dayOne = (Calendar) day1.clone(),
dayTwo = (Calendar) day2.clone();
if (dayOne.get(Calendar.YEAR) == dayTwo.get(Calendar.YEAR)) {
return Math.abs(dayOne.get(Calendar.DAY_OF_YEAR) - dayTwo.get(Calendar.DAY_OF_YEAR));
} else {
if (dayTwo.get(Calendar.YEAR) > dayOne.get(Calendar.YEAR)) {
//swap them
Calendar temp = dayOne;
dayOne = dayTwo;
dayTwo = temp;
}
int extraDays = 0;
while (dayOne.get(Calendar.YEAR) > dayTwo.get(Calendar.YEAR)) {
dayOne.add(Calendar.YEAR, -1);
// getActualMaximum() important for leap years
extraDays += dayOne.getActualMaximum(Calendar.DAY_OF_YEAR);
}
return extraDays - dayTwo.get(Calendar.DAY_OF_YEAR) + dayOne.get(Calendar.DAY_OF_YEAR);
}
}

Related

How to fix Time picker dialog showing in spanish language

The bounty expires in 7 days. Answers to this question are eligible for a +50 reputation bounty.
MARSH is looking for an answer from a reputable source:
Please help me this is happening because of Localisation but i have debug code i am seeing its going in English language but its showing spanish language
This is popup for time picker.
when I click on keyboard icon it's showing like below screen.
I am using localization in our APP and below code I have written to open Time picker Popup
private fun openTimePickerDialog(datetime: String) {
val mHour: Int = datetime.substring(8, 10).toInt()
val mMinute: Int = datetime.substring(10, 12).toInt()
setDefaultLanguage()
Log.i("TAG", "openTimePickerDialog: $mHour $mMinute")
val timePickerDialog = TimePickerDialog(
activity,
{ _, hourOfDay, minute ->
var selectedMinute = minute.toString()
var selectedHour = hourOfDay.toString()
if (minute < 10) {
selectedMinute = "0$selectedMinute"
}
if (hourOfDay < 10) {
selectedHour = "0$selectedHour"
}
isClickedDate = ""
val date: String = datetime.substring(0, 8)
val time = selectedHour + selectedMinute + "00"
Log.i("TAG", "onTimeSet: $date$time")
roveR3SettingFragmentViewModel.setDateTime(date + time)
// txtTime.setText(hourOfDay + ":" + minute);
}, mHour, mMinute, false
)
timePickerDialog.setButton(
DatePickerDialog.BUTTON_POSITIVE,
getString(R.string.ok),
timePickerDialog
)
timePickerDialog.setButton(
DatePickerDialog.BUTTON_NEGATIVE,
getString(R.string.txt_cancel),
timePickerDialog
)
timePickerDialog.show()
}
private fun setDefaultLanguage() {
if (appPreference.appLanguage == LANGAUGE_ES) {
val locale1 = Locale("en")
Locale.setDefault(locale1)
val config = requireContext().resources.configuration
config.setLocale(locale1)
requireContext().createConfigurationContext(config)
} else if (appPreference.appLanguage == LANGAUGE_ES) {
val locale1 = Locale("ja")
Locale.setDefault(locale1)
val config = requireContext().resources.configuration
config.setLocale(locale1)
requireContext().createConfigurationContext(config)
}
}
Can any one please help me how to fix the language issue which is coming when i click on keyboard icon of time picker dialogue. I want it in english language I have selected it for English only other all thing coming in english but time picker is coming in Spanish language. Thanks.
Check the setDefaultLanguage() method in your code carefully.
Conditions for both if & else if looks the same, so always it will run the functions in the else if section.
Try to use a different condition for else if
Check the example code here,
private fun setDefaultLanguage() {
if (appPreference.appLanguage == CONDITION_1) {
val locale1 = Locale("en")
Locale.setDefault(locale1)
val config = requireContext().resources.configuration
config.setLocale(locale1)
requireContext().createConfigurationContext(config)
} else if (appPreference.appLanguage == CONDITION_2) {
val locale1 = Locale("ja")
Locale.setDefault(locale1)
val config = requireContext().resources.configuration
config.setLocale(locale1)
requireContext().createConfigurationContext(config)
}
}
In your scenario, both condition 1 and 2 are same. So, always the popup using the language from else if

Getting days of current week using kotlinx-datetime KMM

How can I get the dates of the current week using kotlinx.datetime KMM library?
eg. I want to get a list of current week's date like:
("Monday, 07", "Tuesday, 08", "Wednesday, 09", ... )
This is a similar impementation using Calendar:
fun getDaysOfWeek(): Array<String?> {
val dateFormat = SimpleDateFormat("EEEEE\ndd", Locale.getDefault())
val calendar = Calendar.getInstance()
calendar.firstDayOfWeek = Calendar.MONDAY
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY)
val days = arrayOfNulls<String>(7)
for (i in 0..6) {
days[i] = dateFormat.format(calendar.time)
calendar.add(Calendar.DAY_OF_MONTH, 1)
}
return days
}
val today = Clock.System.now().toLocalDateTime(TimeZone.currentSystemDefault()).date
val days = mutableListOf<LocalDate>()
val firstWeekDay = today.daysShift(-DayOfWeek.values().indexOf(today.dayOfWeek))
for (i in 0 until DayOfWeek.values().count()) {
days.add(firstWeekDay.daysShift(i))
}
val dayStrings = days.map { "${it.dayOfWeek}, ${it.dayOfMonth}" }
println("$dayStrings")
fun LocalDate.daysShift(days: Int): LocalDate = when {
days < 0 -> {
minus(DateTimeUnit.DayBased(-days))
}
days > 0 -> {
plus(DateTimeUnit.DayBased(days))
}
else -> this
}

Format date string 2021-06-13T15:00:00.000Z to how many hours remaining

I am receiving date string 2021-06-13T15:00:00.000Z from rest api call. I have to parse this date string that match will start in 5 hours or today
You can try something like below.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault());
LocalDateTime eventDate = LocalDateTime.parse("2021-06-15T21:00:00.000Z", formatter);
LocalDateTime now = LocalDateTime.now();
Duration dur = Duration.between(now, eventDate);
if (dur.isNegative()) {
Log.d("DINKAR", "Time Already Passed");
} else if (dur.isZero()) {
Log.d("DINKAR", "Its happening now");
} else {
if (dur.getSeconds() > 0 && dur.getSeconds() < 60) {
Log.d("DINKAR", String.format("Match will start in %d seconds", dur.getSeconds()));
} else if (dur.toMinutes() > 0 && dur.toMinutes() < 60) {
Log.d("DINKAR", String.format("Match will start in %d minutes", dur.toMinutes()));
} else if (dur.toHours() > 0 && dur.toHours() < 24) {
Log.d("DINKAR", String.format("Match will start in %d hours", dur.toHours()));
} else if (dur.toDays() > 0) {
Log.d("DINKAR", String.format("Match will start in %d days", dur.toDays()));
}
}
For API below 26 Please also add the following in your build.gradle
android {
defaultConfig {
// Required when setting minSdkVersion to 20 or lower
multiDexEnabled = true
}
compileOptions {
// Flag to enable support for the new language APIs
coreLibraryDesugaringEnabled = true
// Sets Java compatibility to Java 8
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
}
dependencies {
coreLibraryDesugaring("com.android.tools:desugar_jdk_libs:1.1.5")
}
The following code snippet might help:
val timeInString = "2021-06-16T05:00:00.000Z"
val givenTime = ZonedDateTime.parse(timeInString).toLocalDateTime()
val currentTime = ZonedDateTime.now().toLocalDateTime()
val difference = Duration.between(currentTime, givenTime)
val matchTimeMessage = when {
difference.isNegative -> "The match is already over"
difference.toHours() < 24 -> "The match will start in ${difference.toHours()} hours"
else -> "The match will start in ${difference.toDays()} days"
}
println(matchTimeMessage)
You can simply calculate the difference of hours between the present time and given time. There are more than one ways to do this. Here are some
val givenTime = Instant.parse("2021-06-13T15:00:00.000Z")
val currentTime = Instant.now()
val difference1 = ChronoUnit.HOURS.between(currentTime, givenTime) // Use Chronounit
val difference2 = Duration.between(currentTime, givenTime).toHours() // Use Duration
private fun getAppropriateTimeDiffResolution(
start: Date?,
end: Date?
): String {
return if (start != null && end != null) {
val diffInMs: Long = end.time - start.time
val diffInMins: Long = TimeUnit.MILLISECONDS.toMinutes(diffInMs)
val diffInHrs: Long = TimeUnit.MILLISECONDS.toHours(diffInMs)
val diffInDays: Long = TimeUnit.MILLISECONDS.toDays(diffInMs)
val diffInMonth: Long = TimeUnit.MILLISECONDS.toDays(diffInMs) / 30
val diffInYear = diffInMonth / 12
val stringBuilder = StringBuilder()
if (diffInMins < 60) {
if (diffInMins > 1) stringBuilder.append(diffInMins)
.append(" Mins Ago")
.toString() else if (diffInMins == 0L) "Now" else stringBuilder.append(
diffInMins
).append(" Mins Ago").toString()
} else if (diffInHrs < 24) {
stringBuilder.append(diffInHrs)
.append(" Hours Ago")
.toString()
} else if (diffInDays < 30) {
stringBuilder.append(diffInDays)
.append(" Days Ago").toString()
} else if (diffInMonth < 12) {
stringBuilder.append(diffInMonth)
.append(" Months Ago")
.toString()
} else {
stringBuilder.append(diffInYear)
.append(" Years Ago").toString()
}
} else {
"--"
}
}
private fun getFormattedTime(#NonNull time: String): String {
Log.e("BindingAdapter", time)
val input = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX", Locale.getDefault())
var d: Date?
try {
d = input.parse(time)
return getAppropriateTimeDiffResolution(d, Date())
} catch (e: ParseException) {
try {
val fallback =
SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.getDefault())
d = fallback.parse(time)
return getAppropriateTimeDiffResolution(d, Date())
} catch (e2: ParseException) {
return "--"
}
}
}

How do I simplify all of these query parameter possible combinations in Kotlin? Builder Pattern?

I'm building an Android App with Kotlin that uses various NYTimes APIs to fetch different news data..
Here I have a search screen where I want the user to be able to enter a search query (i.e. "Japan") and check off any checkbox and they will even be able to add a begin or end date to refine their search :
Typing something in the search query and checking off at least one box are the only requirements, everything else will be mandatory. And once they hit "Search", it will pass the data they entered to a second activity which will use the data to put together an api call, make the api call, and populate a recyclerview , like so:
Now here is my issue...
I have the SEARCH button only sending data if the search query has been entered and if the travel checkbox has been checked, and as you can imagine there are a TON of combinations.. I don't know how I can pass over all of those combinations and make each API Call accordingly without having an extremely long If/Else Block that'll take forever...
Using Kotlin, there has to be a more efficient way right ?
Here is my Search Activity:
class SearchActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_search)
search_query_edittext.getBackground().clearColorFilter();
actionBar?.setDisplayHomeAsUpEnabled(true)
Enter_Begin_Date.setPaintFlags(Enter_Begin_Date.getPaintFlags())
Enter_End_Date.setPaintFlags(Enter_End_Date.getPaintFlags())
// Calendar
val c = Calendar.getInstance()
val year = c.get(Calendar.YEAR)
val month = c.get(Calendar.MONTH)
val day = c.get(Calendar.DAY_OF_MONTH)
// TextView Clicked to show Date Picker Dialog
Enter_Begin_Date.setOnClickListener {
val dpd = DatePickerDialog(
this,
DatePickerDialog.OnDateSetListener { view, Year, Month, Day ->
// set to textView
if (Day < 10 && Month < 10) {
Enter_Begin_Date.text =
"0" + Day + "/0" + Month.toInt().plus(1) + "/" + Year
} else if (Day < 10 && Month >= 10) {
Enter_Begin_Date.text = "0" + Day + "/" + Month.toInt().plus(1) + "/" + Year
} else if (Day >= 10 && Month < 10) {
Enter_Begin_Date.text = "" + Day + "/0" + Month.toInt().plus(1) + "/" + Year
}
},
year,
month,
day
)
// show dialog
dpd.show()
}
Enter_End_Date.setOnClickListener {
val dpd = DatePickerDialog(
this,
DatePickerDialog.OnDateSetListener { view, Year, Month, Day ->
// set to textView
if (Day < 10 && Month < 10) {
Enter_End_Date.text = "0" + Day + "/0" + Month.toInt().plus(1) + "/" + Year
} else if (Day < 10 && Month >= 10) {
Enter_End_Date.text = "0" + Day + "/" + Month.toInt().plus(1) + "/" + Year
} else if (Day >= 10 && Month < 10) {
Enter_End_Date.text = "" + Day + "/0" + Month.toInt().plus(1) + "/" + Year
}
},
year,
month,
day
)
// show dialog
dpd.show()
}
searchButton.setOnClickListener {
if ((search_query_edittext.text.isNotEmpty()
&& Enter_Begin_Date.text.isEmpty()
&& Enter_End_Date.text.isEmpty()) && TravelTextBox.isChecked && !SportsTextBox.isChecked && !PoliticsTextBox.isChecked && !EntrepreneursTextBox.isChecked && !BusinessTextBox.isChecked && !ArtsCheckBox.isChecked
) {
val searchQuery: String = search_query_edittext.text.toString()
val query = searchQuery
val travelCategory: String = "Travel"
val intent = Intent(this#SearchActivity, ResultsActivity::class.java)
intent.putExtra("query", query)
intent.putExtra("travelCategory", travelCategory)
startActivity(intent)
}
}
}
}
My Result Activity:
class ResultsActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_results)
val bundle: Bundle? = intent.extras
val myQuery = bundle!!.getString("query")
val myTravelCategory = bundle!!.getString("travelCategory")
if (bundle.containsKey("query") && bundle.containsKey("travelCategory")) {
lifecycleScope.launch(IO) {
val result = ApiClient.getClient.getSearchResultWithCheckbox(query = myQuery!!,
category = myTravelCategory!!,
api_key = (MYAPIKEY)
withContext(Main) {
SearchResultsRecyclerView.apply {
layoutManager = LinearLayoutManager(this#ResultsActivity)
adapter = SearchResultsAdapter(result.response.docs)
}
}
}
}
}
}
As you can see, I only have coded enough to cover for the user to enter in the search query , to check the travel checkbox and to hit enter, there are like 145 more combinations (search query + begin date + politics checkbox, search query + end date + sports checkbox, etc etc..)
How can I add in all of the other combinations without using an extremely long If/Else block?
Anything to push me in the right direction would be highly appreciated , thanks!
check for the condition you want, not the combinations that produce it.
Check if the search query is filled and that at least one checkbox is checked. Without knowing how the API works is hard to recommend a particular way to pass the data but taking advantage of nullable types and/or optional paramaters with something like this should work:
private fun getCheckedCategories() : List<String> = listOfNotNull(
"Travel".takeIf { TravelTextBox.isChecked },
...
)
private fun atLeastOneCheckBoxChecked() = getCheckedCategories().isNotEmpty()
With this helper functions you can build a listener similar to this:
searchButton.setOnClickListener {
if (search_query_edittext.text.isNotEmpty() && atLeastOneCheckBoxChecked()) {
val searchQuery: String = search_query_edittext.text.toString()
val checkedCategories = getCheckedCategories()
val beginDate : String? = TODO()
val endDate : String? = TODO()
val intent = Intent(this#SearchActivity, ResultsActivity::class.java)
intent.putExtra("query", searchQuery)
intent.putStringArrayList("checkedCategories", ArrayList(checkedCategories))
intent.putExtra("beginDate", beginDate)
intent.putExtra("endDate", endDate)
startActivity(intent)
}
}
In the other end the bundle?.getString("beginDate") will return null if not passed, and whatever value it has if passed. Or retrieve the list of passed checkboxes with bundle?getStringArrayList("checkedCategories").orEmpty()

Format remaining time untill date

I have a date in the future and have to format the remaining time until this day like so.
4 days
1 month, 4 days
1 year, 1 month
I have looked at the DateUtils documentation but haven't seen this exact format.
I'm also fine using an external library like threetenabp.
Is there a library that can handle both the time calculation and the localization of the strings?
I wrote this blog a while ago, it shows how to do the opposite of what you are asking :-) https://blog.blundellapps.co.uk/creating-comments-with-timestamps-like-youtube/ i.e. given a time, say how long ago that was.
Android also offers this solution for times in the past: https://developer.android.com/reference/android/text/format/DateUtils.html#getRelativeDateTimeString(android.content.Context,%20long,%20long,%20long,%20int).
So you need the opposite of these!
It should not be too hard to inverse. Instead of using the time now and negating the difference of a time in the past. You use the time now and add the time in the future.
So the inverse of: https://github.com/blundell/YouTubeTimeStamps/blob/master/app/src/main/java/com/blundell/tut/TimeStampFormatter.kt
The main difference being you want a difference between now and a date in the future, like so:
private fun getMillisFromNow(futureTime: Date): Long {
val futureTimeMillis = futureTime.time
val nowMillis = System.currentTimeMillis()
return futureTimeMillis - nowMillis
}
And then format it. Something like this:
fun format(timestamp: Date): String {
val millisFromNow = getMillisFromNow(timestamp)
val minutesFromNow = TimeUnit.MILLISECONDS.toMinutes(millisFromNow)
if (minutesFromNow < 1) {
return "about now"
}
val hoursFromNow = TimeUnit.MILLISECONDS.toHours(millisFromNow)
if (hoursFromNow < 1) {
return formatMinutes(minutesFromNow)
}
val daysFromNow = TimeUnit.MILLISECONDS.toDays(millisFromNow)
if (daysFromNow < 1) {
return formatHours(hoursFromNow)
}
val weeksFromNow = TimeUnit.MILLISECONDS.toDays(millisFromNow) / 7
if (weeksFromNow < 1) {
return formatDays(daysFromNow)
}
val monthsFromNow = TimeUnit.MILLISECONDS.toDays(millisFromNow) / 30
if (monthsFromNow < 1) {
return formatWeeks(weeksFromNow)
}
val yearsFromNow = TimeUnit.MILLISECONDS.toDays(millisFromNow) / 365
return if (yearsFromNow < 1) {
formatMonths(monthsFromNow)
} else formatYears(yearsFromNow)
}
private fun getMillisFromNow(futureTime: Date): Long {
val futureTimeMillis = futureTime.time
val nowMillis = System.currentTimeMillis()
return futureTimeMillis - nowMillis
}
private fun formatMinutes(minutes: Long): String {
return format(minutes, " minute to go", " minutes to go")
}
private fun formatHours(hours: Long): String {
return format(hours, " hour to go", " hours to go")
}
private fun formatDays(days: Long): String {
return format(days, " day to go", " days to go")
}
private fun formatWeeks(weeks: Long): String {
return format(weeks, " week to go", " weeks to go")
}
private fun formatMonths(months: Long): String {
return format(months, " month to go", " months to go")
}
private fun formatYears(years: Long): String {
return format(years, " year to go", " years to go")
}
private fun format(hand: Long, singular: String, plural: String): String {
return if (hand == 1L) {
hand.toString() + singular
} else {
hand.toString() + plural
}
}
Just sanity checked it with this test:
#Test
fun test() {
val twoDaysInMillisInstant = Instant.now().plusMillis(TimeUnit.HOURS.toMillis(50))
val result = format(Date.from(twoDaysInMillisInstant))
assertEquals("2 days to go", result)
}
For the localization, you can convert this to using Strings.xml.
using threeten's LocalDate you can get the remaining years, months and day from one date to another. Having that values you can 0-check them and show only ones that are > 0.
Simple example:
val futureDate = LocalDate.of(2020,2,20)
val todayDate = LocalDate.now()
val remainingYears = futureDate.year - todayDate.year //output 0
val remainingMonth = futureDate.monthValue - todayDate.monthValue //output 0
val remainingDays = futureDate.dayOfMonth - todayDate.dayOfMonth // output 16
I hope that is what you wanted, cheers!

Categories

Resources