ds park
ds park

Reputation: 55

How to string convert to date about "2021/03/03 00:51" in Kotlin

I want to change the data in the format of 2020/03/03 00:51 String into date format.

How can i solve it ?

Anyone can help?

Upvotes: 0

Views: 839

Answers (2)

Luiz Fernando de Moura
Luiz Fernando de Moura

Reputation: 216

You can use the same SimpleDateFormat

import java.text.SimpleDateFormat

val dateFormat = SimpleDateFormat("yyyy/MM/dd HH:mm")

val date = dateFormat.parse("2020/03/04 00:51")

Upvotes: 0

iamdhavalparmar
iamdhavalparmar

Reputation: 1218

You can use this to convert string to date.

This is predefined format

import java.time.LocalDate
import java.time.format.DateTimeFormatter

fun main(args: Array<String>) {
    // Format y-M-d or yyyy-MM-d
    val string = "2017-07-25"
    val date = LocalDate.parse(string, DateTimeFormatter.ISO_DATE)

    println(date)

using pattern formatters

import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.util.Locale

fun main(args: Array<String>) {
    val string = "July 25, 2017"

    val formatter = DateTimeFormatter.ofPattern("MMMM d, yyyy", Locale.ENGLISH)
    val date = LocalDate.parse(string, formatter)

    println(date)
}

Refer here for more information

Upvotes: 1

Related Questions