android kotlin current date example

In this An article today learn get current date android for using kotlin language to follow the code:

 

val current = LocalDateTime.now()

 

        val formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy. HH:mm:ss")

 

        label_date.text = current.format(formatter)

 

Follow the code for android kotlin current date example:


1.MainActivity.kt:

import java.time.LocalDateTime

import java.time.format.DateTimeFormatter


class MainActivity: AppCompatActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_main)


        val current = LocalDateTime.now()


        val formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy. HH:mm:ss")


        label_date.text = current.format(formatter)

    }




 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

        val current = LocalDateTime.now()

        val formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy. HH:mm:ss")

        var answer: String =  current.format(formatter)

        Log.d("answer",answer)

    } else {

        var date = Date();

        val formatter = SimpleDateFormat("MMM dd yyyy HH:mma")

        val answer: String = formatter.format(date)

        Log.d("answer",answer)

    }


}




2.activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:gravity="center"

    android:layout_height="match_parent">

    <TextView

        android:id="@+id/label_date"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_centerHorizontal="true"

        android:textSize="30sp"

        android:layout_marginBottom="36dp" />

</RelativeLayout>


Comments

Popular posts from this blog

Kotlin camera intent example

android kotlin listview example