Android How to Convert your dateTime to different formats using extension function

Abdul Qadir
4 min readNov 4, 2022

--

A very useful extension function for converting your date to different formats by using extension function.

******************************Let’s Start**********************************

Let’s start with the extension function String.kt this file contains all the function required for formatting your date.

Here is a single function from our String.kt file for date format

fun String.convertDateToYMDFullTimeDate(format: String): String? {
val dateFormat = SimpleDateFormat(format, Locale.getDefault())
val parsedDate = dateFormat.parse(this)
val dayMonthFormat = SimpleDateFormat(DateFormats.DATE_TIME_YY_MM_FORMAT.format, Locale.getDefault())
return parsedDate?.let { dayMonthFormat.format(parsedDate) }
}

Please make sure that before passing the string date parameter to the above function from MainActivity.kt you have to format it first otherwise it won’t work, for this purpose in MainActivity.kt I have added the code for formatting your date to a proper format in order to pass it to the extension function.

var formattedDate = date1.formatStringDate(DateFormats.DEFAULT_FORMAT_WITHOUT_TIME.format,DateFormats.DEFAULT_FORMAT.format)

In my case I am using a constant date which is already in the correct format but you have to convert your date to the same formate using above code.

var date1 = “2022–02–13T00:10:00”

*********************** Project Description *************************

In this project we have only 3 files

  • String.kt (Extension function)
  • DateFormats.kt
  • MainActivity.kt

String.kt

fun String.convertDefaultWithoutTime(format: String): String? {
val dateFormat = SimpleDateFormat(format, Locale.getDefault())
val parsedDate = dateFormat.parse(this)
val dayMonthFormat = SimpleDateFormat(DateFormats.DEFAULT_FORMAT_WITHOUT_TIME.format, Locale.getDefault())
return parsedDate?.let { dayMonthFormat.format(parsedDate) }
}

fun String.convertDateToYMDTime(format: String): String? {
val dateFormat = SimpleDateFormat(format, Locale.getDefault())
val parsedDate = dateFormat.parse(this)
val dayMonthFormat = SimpleDateFormat(DateFormats.DATE_TIME_YY_MM_FULL_TIME_FORMAT.format, Locale.getDefault())
return parsedDate?.let { dayMonthFormat.format(parsedDate) }
}

fun String?.formatStringDate(inputFormat : String, outputFormat : String) : String {
return if (this.isNullOrEmpty()){
""
}else{
val dateFormatter = SimpleDateFormat(inputFormat, Locale.getDefault())
val date = dateFormatter.parse(this)
date?.let { SimpleDateFormat(outputFormat, Locale.getDefault()).format(it) }.orEmpty()
}
}

fun String.convertDateToYMDFullTimeDate(format: String): String? {
val dateFormat = SimpleDateFormat(format, Locale.getDefault())
val parsedDate = dateFormat.parse(this)
val dayMonthFormat = SimpleDateFormat(DateFormats.DATE_TIME_YY_MM_FORMAT.format, Locale.getDefault())
return parsedDate?.let { dayMonthFormat.format(parsedDate) }
}


fun String.trimTimeFromDateMDY(format: String): String? {
val dateFormat = SimpleDateFormat(format, Locale.getDefault())
val parsedDate = dateFormat.parse(this)
val trimmed = SimpleDateFormat(DateFormats.DATE_MM_DD_YY_FORMAT.format, Locale.getDefault())
return parsedDate?.let { trimmed.format(parsedDate) }
}

fun String.convertDateToDMYFullTimeDate(format: String): String? {
val dateFormat = SimpleDateFormat(format, Locale.getDefault())
val parsedDate = dateFormat.parse(this)
val dayMonthFormat = SimpleDateFormat(DateFormats.FULL_DATE_TIME_DD_MM_FORMAT.format, Locale.getDefault())
return parsedDate?.let { dayMonthFormat.format(parsedDate) }
}

fun String.convertDateToYMD(format: String): String? {
val dateFormat = SimpleDateFormat(format, Locale.getDefault())
val parsedDate = dateFormat.parse(this)
val dayMonthFormat = SimpleDateFormat(DateFormats.DATE_YY_MM_FORMAT.format, Locale.getDefault())
return parsedDate?.let { dayMonthFormat.format(parsedDate) }
}

fun String.convertDateToMonthNameYear(format: String): String? {
val dateFormat = SimpleDateFormat(format, Locale.getDefault())
val parsedDate = dateFormat.parse(this)
val dayMonthFormat = SimpleDateFormat(DateFormats.DATE_MONTH_OF_YEAR_FORMAT.format, Locale.getDefault())
return parsedDate?.let { dayMonthFormat.format(parsedDate) }
}

fun String.convertDateToWeekNameYear(format: String): String? {
val dateFormat = SimpleDateFormat(format, Locale.getDefault())
val parsedDate = dateFormat.parse(this)
val dayMonthFormat = SimpleDateFormat(DateFormats.DAY_OF_WEEK_MONTH_FORMAT.format, Locale.getDefault())
return parsedDate?.let { dayMonthFormat.format(parsedDate) }
}
fun String.convertDateMonthName(format: String): String? {
val dateFormat = SimpleDateFormat(format, Locale.getDefault())
val parsedDate = dateFormat.parse(this)
val dayMonthFormat = SimpleDateFormat(DateFormats.DATE_MONTH_FORMAT.format, Locale.getDefault())
return parsedDate?.let { dayMonthFormat.format(parsedDate) }
}

fun String.convertDateCustom(format: String): String? {
val dateFormat = SimpleDateFormat(format, Locale.getDefault())
val parsedDate = dateFormat.parse(this)
val dayMonthFormat = SimpleDateFormat(DateFormats.DATE_TIME_FORMAT_CUSTOM_FORMAT.format, Locale.getDefault())
return parsedDate?.let { dayMonthFormat.format(parsedDate) }
}

fun String.convertDateToAmPm(format: String): String? {
val dateFormat = SimpleDateFormat(format, Locale.getDefault())
val parsedDate = dateFormat.parse(this)
val dayMonthFormat = SimpleDateFormat("hh:mm a", Locale.getDefault())
return parsedDate?.let { dayMonthFormat.format(parsedDate) }
}

DateFormats.kt

DEFAULT_FORMAT_WITHOUT_TIME("yyyy-MM-dd"), //2021-05-20 
DATE_TIME_YY_MM_FORMAT("yyyy-MM-dd'T'HH:mm"), //2021-05-20T11:28
DATE_TIME_YY_MM_FULL_TIME_FORMAT("yyyy-MM-dd'T'HH:mm:ss"), //2021-05-20T11:28
DATE_MM_DD_YY_FORMAT("MM-dd-yyyy"), //05-20-2021
DATE_MM_DD_FORMAT("MMM dd, yyyy"), //May 20, 2021
FULL_DATE_TIME_DD_MM_FORMAT("dd-MM-yyyy'T'HH:mm:ss"), //2021-05-20T11:28:24
DATE_YY_MM_FORMAT("yyyy-MM-dd"), //2021-05-20
DATE_MONTH_OF_YEAR_FORMAT("d MMMM, yyyy"), //20 May, 2021
DAY_OF_WEEK_MONTH_FORMAT("EEE, d MMM"), //Thu, 20 May
DATE_MONTH_FORMAT("d MMM"), // 20 MAY
DATE_TIME_FORMAT_CUSTOM_FORMAT("dd.MM.yyyy' - ' HH:mm:ss"), //21.06.2021 - 10:10:18
TIME_AM_PM_FORMAT("hh:mm a"), // 10:10:18 hh:mm aa

MainActivity.kt

class MainActivity : AppCompatActivity() {

var date1 = "2022-02-13T00:10:00"

@RequiresApi(Build.VERSION_CODES.N)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

var myText: TextView = findViewById(R.id.text1)

val spinner: Spinner = findViewById(R.id.planets_spinner)

var formattedDate = date1.formatStringDate(DateFormats.DEFAULT_FORMAT_WITHOUT_TIME.format,DateFormats.DEFAULT_FORMAT.format)

ArrayAdapter.createFromResource(
this,
R.array.format_date,
android.R.layout.simple_spinner_item
).also { adapter ->
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
// Apply the adapter to the spinner
spinner.adapter = adapter
spinner.onItemSelectedListener = object : OnItemSelectedListener {
override fun onItemSelected(
parentView: AdapterView<*>?,
selectedItemView: View,
position: Int,
id: Long
) {
myText.text = date1
when (position) {
1 -> {
var dayMonth = date1
dayMonth =
dayMonth.convertDefaultWithoutTime(DateFormats.DEFAULT_FORMAT.format)
.toString()
myText.text = dayMonth
}
2 -> {
var trimhour = date1
trimhour =
trimhour.convertDateToYMDFullTimeDate(DateFormats.DEFAULT_FORMAT.format).toString()
myText.text = trimhour
}
3 -> {
var timeAMPM = date1
timeAMPM = timeAMPM.convertDateToYMDFullTimeDate(DateFormats.DEFAULT_FORMAT.format)
.toString()
myText.text = timeAMPM
}
4 -> {
var timeAMPM = date1
timeAMPM = timeAMPM.trimTimeFromDateMDY(
DateFormats.DEFAULT_FORMAT.format,
).toString()
myText.text = timeAMPM
}

5 -> {
var timeAMPM = date1
timeAMPM = timeAMPM.convertDateToDMYFullTimeDate(
DateFormats.DEFAULT_FORMAT.format
).toString()
myText.text = timeAMPM
}
6 -> {
var timeAMPM = date1
timeAMPM = timeAMPM.convertDateToYMD(
DateFormats.DEFAULT_FORMAT.format
).toString()
myText.text = timeAMPM
}

7 -> {
var timeAMPM = date1
timeAMPM = timeAMPM.convertDateToMonthNameYear(
DateFormats.DEFAULT_FORMAT.format
).toString()
myText.text = timeAMPM
}

8 -> {
var timeAMPM = date1
timeAMPM = timeAMPM.convertDateToWeekNameYear(
DateFormats.DEFAULT_FORMAT.format
).toString()
myText.text = timeAMPM
}

9 -> {
var timeAMPM = date1
timeAMPM = timeAMPM.convertDateMonthName(
DateFormats.DEFAULT_FORMAT.format
).toString()
myText.text = timeAMPM
}
10 -> {
var timeAMPM = date1
timeAMPM = timeAMPM.convertDateCustom(
DateFormats.DEFAULT_FORMAT.format
).toString()
myText.text = timeAMPM
}
11 -> {
var timeAMPM = date1
timeAMPM = timeAMPM.convertDateToAmPm(
DateFormats.DEFAULT_FORMAT.format
).toString()
myText.text = timeAMPM
}
}
}

override fun onNothingSelected(parentView: AdapterView<*>?) {
// your code here
}
}
}

}
}

Source code:

Please find the source code for sample project.

https://github.com/abdulqadirtr/Kotlin-Date-Conversion

--

--

Abdul Qadir

Android developer, my strengths include but not limited to Java, Android Mobile Application Development, Kotlin, Unity3D