Using Android Studio, how to display Day(mon,tue,wed etc.), Date, Month and Year from selected date in Datepicker

I am looking to create a simple code that will take a date from my datepicker and display each the day of the week, day, month and year in seperate textview.

Then to display the same for a day an input number of days later.

Example: Choose 12/02/2020 and enter 2

Output Wenesday, 12 02 2020 Friday, 15 02 2020 I have the datepicker and the output to give myself output of the dates but i dont know how to get it to calculate the day of the week.

1 answer

  • answered 2020-02-13 04:15 Manikandan Karuppiyah

        @SuppressLint("NewApi") // Selected Date From Date Picker Format
        val resultFromDatePicker = SimpleDateFormat("dd/MM/yyyy", Locale.US)
    
        @SuppressLint("NewApi") // Convert Date To This Format
        val convertDateTo = SimpleDateFormat("EEEE, dd MM yyyy", Locale.US)
    
        // Result - Wednesday, 12 02 2020
        val result = ""+parseDate("12/02/2020", resultFromDatePicker, convertDateTo)
    
        // Parsing Date
        @SuppressLint("NewApi")
        fun parseDate(
            inputDateString: String?,
            inputDateFormat: SimpleDateFormat,
            outputDateFormat: SimpleDateFormat
        ): String? {
            val date: Date?
            var outputDateString: String? = null
            try {
                date = inputDateFormat.parse(inputDateString)
                outputDateString = outputDateFormat.format(date)
            } catch (e: ParseException) {
                e.printStackTrace()
            }
            return outputDateString
        }