Reputation: 1
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.
Upvotes: 0
Views: 415
Reputation: 1
fun Calendar.toDayAndDateFormat(outputPattern: String = "EEE, d MMM yyyy"): String {
val sdf = SimpleDateFormat(outputPattern, Locale.getDefault())
return try {
sdf.format(this.time)
} catch (e: Exception) {
this.toString()
}
}
now you can use with date like this:
val today = Calendar.getInstance()
today.toDayAndDateFormat()
Upvotes: 0
Reputation: 138
@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
}
Upvotes: 1