date to string kotlin

//create an extension function on a date class which returns a string
    private fun Date.dateToString(format: String): String {
    //simple date formatter
        val dateFormatter = SimpleDateFormat(format, Locale.getDefault())
        
        //return the formatted date string
        return dateFormatter.format(this)
    }
    
    
    //call the extension function on a date object
    val timestampt = Date()
    val dateString = timestamp.dateToString("hh:mm a E dd-MMM")

4
6
Kalesh P V 105 points

                                    //create an extension function on a date class which returns a string
    private fun Date.dateToString(format: String): String {
    //simple date formatter
        val dateFormatter = SimpleDateFormat(format, Locale.getDefault())
        
        //return the formatted date string
        return dateFormatter.format(this)
    }
    
    
    //call the extension function on a date object
    val timestampt = Date()
    val dateString = timestamp.dateToString("hh:mm a E dd-MMM")

4 (6 Votes)
0
0
1
Lore777 85 points

                                    //create an extension function on a date class which returns a string
    private fun Date.dateToString(format: String): String {
    //simple date formatter
        val dateFormatter = SimpleDateFormat(format, Locale.getDefault())
        
        //return the formatted date string
        return dateFormatter.format(this)
    }
    
    
    //call the extension function on a date object
    val timestampt = Date()
    val dateString = timestamp.dateToString("hh:mm a E dd-MMM")

0
0
4.5
2

                                    import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

fun main(args: Array<String>) {
    val current = LocalDateTime.now()
    val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")
    val formatted = current.format(formatter)
    println("Current Date and Time is: $formatted")
}
// Current Date and Time is: 2021-04-10 10:28:21.052

4.5 (2 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source