यह उदाहरण दर्शाता है कि Kotlin में SimpleDateFormat का उपयोग करके मिलीसेकंड को पठनीय दिनांक प्रारूप में कैसे बदला जाए।
चरण 1 - नया प्रोजेक्ट बनाएं
Android Studio: File → New Project।
चरण 2 - activity_main.xml लेआउट
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:background="#008080"
android:padding="5dp"
android:text="TutorialsPoint"
android:textColor="#fff"
android:textSize="24sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="@android:color/holo_orange_dark"
android:textSize="24sp"
android:textStyle="bold" />
</RelativeLayout>
चरण 3 - MainActivity.kt में दिनांक रूपांतरण
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import java.text.SimpleDateFormat
import java.util.Locale
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
val textView = findViewById<TextView>(R.id.textView)
val sdf = SimpleDateFormat("dd/MM/yyyy", Locale.getDefault())
val milliseconds = 9897546853323L // उदाहरण टाइमस्टैम्प
val dateString = sdf.format(milliseconds)
textView.text = "दिनांक: $dateString"
}
}
चरण 4 - AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.millistodate">
<application ...>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
ऐप चलाएं
ऐप चलाएं। TextView में परिवर्तित दिनांक प्रदर्शित होगी (उदाहरण: "दिनांक: 15/03/2001")।
आधुनिक दृष्टिकोण: Java 8+ / API 26+ के लिएjava.timeपैकेज (Instant,DateTimeFormatter,ZoneId) का उपयोग करने की सलाह दी जाती है जो थ्रेड-सेफ और अधिक शक्तिशाली है।