SQLite में unixepoch टाइमस्टैम्प कन्वर्जन
उदाहरण से पहले, Android में SQLite डेटाबेस को समझें। SQLite एक ओपन-सोर्स SQL डेटाबेस है।
unixepoch मॉडिफायर strftime() के साथ उपयोग करके यूनिक्स टाइमस्टैम्प (सेकंड्स सिन्स 1970) को मानव-पठनीय तारीख/समय में बदलता है।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project पर जाएं और प्रोजेक्ट बनाएं।
चरण 2: लेआउट फ़ाइल (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical">
<EditText android:id="@+id/name" android:layout_width="match_parent"
android:hint="नाम दर्ज करें" android:layout_height="wrap_content" />
<EditText android:id="@+id/salary" android:layout_width="match_parent"
android:inputType="numberDecimal" android:hint="वेतन दर्ज करें"
android:layout_height="wrap_content" />
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button android:id="@+id/save" android:text="सेव करें"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<Button android:id="@+id/refresh" android:text="रिफ्रेश"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<Button android:id="@+id/update" android:text="अपडेट"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<Button android:id="@+id/delete" android:text="सभी हटाएं"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>
<ListView android:id="@+id/listView" android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
चरण 3: MainActivity.java
package com.example.myapp;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
EditText name, salary;
ArrayAdapter<String> arrayAdapter;
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final DatabaseHelper helper = new DatabaseHelper(this);
final ArrayList<String> arrayList = helper.getAllContacts();
name = findViewById(R.id.name);
salary = findViewById(R.id.salary);
listView = findViewById(R.id.listView);
arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, arrayList);
listView.setAdapter(arrayAdapter);
findViewById(R.id.delete).setOnClickListener(v -> {
if (helper.deleteAll()) Toast.makeText(this, "हटाया गया", Toast.LENGTH_LONG).show();
else Toast.makeText(this, "नहीं हटाया गया", Toast.LENGTH_LONG).show();
});
findViewById(R.id.update).setOnClickListener(v -> {
String n = name.getText().toString().trim();
String s = salary.getText().toString().trim();
if (!n.isEmpty() && !s.isEmpty()) {
if (helper.update(n, s)) Toast.makeText(this, "अपडेट किया गया", Toast.LENGTH_LONG).show();
else Toast.makeText(this, "अपडेट विफल", Toast.LENGTH_LONG).show();
} else {
name.setError("नाम दर्ज करें");
salary.setError("वेतन दर्ज करें");
}
});
findViewById(R.id.refresh).setOnClickListener(v -> {
arrayList.clear();
arrayList.addAll(helper.getAllContacts());
arrayAdapter.notifyDataSetChanged();
});
findViewById(R.id.save).setOnClickListener(v -> {
String n = name.getText().toString().trim();
String s = salary.getText().toString().trim();
if (!n.isEmpty() && !s.isEmpty()) {
if (helper.insert(n, s)) Toast.makeText(this, "इन्सर्ट किया गया", Toast.LENGTH_LONG).show();
else Toast.makeText(this, "इन्सर्ट विफल", Toast.LENGTH_LONG).show();
} else {
name.setError("नाम दर्ज करें");
salary.setError("वेतन दर्ज करें");
}
});
}
}
चरण 4: DatabaseHelper.java - unixepoch का उपयोग
package com.example.myapp;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "SalaryDatabase10";
public static final String TABLE_NAME = "SalaryDetails";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 2);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + " (id INTEGER PRIMARY KEY, name TEXT, salary BLOB, datetime DEFAULT CURRENT_TIMESTAMP)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean insert(String name, String salary) {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", name);
values.put("salary", salary);
return db.insert(TABLE_NAME, null, values) != -1;
}
// unixepoch से वर्तमान समय प्राप्त करें
public ArrayList<String> getAllContacts() {
SQLiteDatabase db = getReadableDatabase();
ArrayList<String> list = new ArrayList<>();
// strftime('%s','now') वर्तमान यूनिक्स टाइमस्टैम्प देता है
// unixepoch इसे लोकल टाइम में बदलता है
String query = "SELECT id || ' : ' || TRIM(name) || ' : ' || salary || ' : ' || " +
"datetime(strftime('%s','now'), 'unixepoch', 'localtime') AS fullname FROM " + TABLE_NAME;
Cursor cursor = db.rawQuery(query, null);
if (cursor.moveToFirst()) {
do {
list.add(cursor.getString(cursor.getColumnIndexOrThrow("fullname")));
} while (cursor.moveToNext());
}
cursor.close();
return list;
}
public boolean update(String name, String salary) {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", name);
values.put("salary", salary);
return db.update(TABLE_NAME, values, "name = ?", new String[]{name}) > 0;
}
public boolean deleteAll() {
SQLiteDatabase db = getWritableDatabase();
return db.delete(TABLE_NAME, null, null) > 0;
}
}
ऐप चलाएं
डिवाइस कनेक्ट करें और ऐप चलाएं।

परिणाम में, हम रिकॉर्ड को तारीख और समय के साथ दिखा रहे हैं। unixepoch यूनिक्स टाइमस्टैम्प को पठनीय फॉर्मेट में बदलने के लिए आवश्यक है।