Day78 of #100DaysOfCode

Kushagra Kesav
3 min readApr 25, 2022
Day78 of #100DaysOfCode

Hii folks 🙌

Today I will be continuing the same pathway in which we will update pickup and summary fragments to viewModel.

Unit 3: Navigation

Pathway 3: Advanced navigation app Examples

https://developer.android.com/courses/android-basics-kotlin/course

Use the Create pickup options list

  • Date formatter

The Android framework provides a class called SimpleDateFormat, which is a class for formatting and parsing dates in a locale-sensitive manner. It allows for formatting (date → text) and parsing (text → date) of dates.

We can create an instance of SimpleDateFormat by passing in a pattern string and a locale:

SimpleDateFormat("E MMM d", Locale.getDefault())

A pattern string like "E MMM d" is a representation of Date and Time formats.

A Locale object represents a specific geographical, political, or cultural region. It represents a language/country/variant combination. Locales are used to alter the presentation of information such as numbers or dates to suit the conventions in the region.

Locale in Android is a combination of language and country code. The language codes are two-letter lowercase ISO language codes, such as “en” for english. The country codes are two-letter uppercase ISO country codes, such as “US” for the United States.

Now we can use SimpleDateFormat and Locale to determine the available pickup dates for the Cupcake app.

  • In OrderViewModel class, we will add the following function called getPickupOptions() to create and return the list of pickup dates. Within the method, create a val variable called options and initialize it to mutableListOf<String>().
private fun getPickupOptions(): List<String> {
val options = mutableListOf<String>()
}
  • Create a formatter string using SimpleDateFormat passing pattern string "E MMM d", and the locale. In the pattern string, E stands for day name in week and it parses to "Tue Dec 10".
val formatter = SimpleDateFormat("E MMM d", Locale.getDefault())

Import java.text.SimpleDateFormat and java.util.Locale, when prompted by Android Studio.

  • Get a Calendar instance and assign it to a new variable. Make it a val. This variable will contain the current date and time. Also, import java.util.Calendar.
val calendar = Calendar.getInstance()
  • Build up a list of dates starting with the current date and the following three dates. Because we’ll need 4 date options, repeat this block of code 4 times. This repeat block will format a date, add it to the list of date options, and then increment the calendar by 1 day.
repeat(4) {
options.add(formatter.format(calendar.time))
calendar.add(Calendar.DATE, 1)
}
  • Return the updated options at the end of the method. Here is our completed method:
private fun getPickupOptions(): List<String> {
val options = mutableListOf<String>()
val formatter = SimpleDateFormat("E MMM d", Locale.getDefault())
val calendar = Calendar.getInstance()
// Create a list of dates starting with the current date and the following 3 dates
repeat(4) {
options.add(formatter.format(calendar.time))
calendar.add(Calendar.DATE, 1)
}
return options
}
  • In OrderViewModel class, add a class property called dateOptions that's a val.We will initialize it using the getPickupOptions() method we just created.
val dateOptions = getPickupOptions()

That is all for Day78 ✅

Thanks for reading, See you tomorrow!

--

--