YearMonth API के साथ वर्ष-महीना संयोजन प्राप्त करना
यह उदाहरण दर्शाता है कि Android में YearMonth API क्लास का उपयोग करके महीने की जानकारी कैसे प्राप्त करें।
चरण 1 - नया प्रोजेक्ट बनाएं
Android Studio में एक नया प्रोजेक्ट बनाएं: File → New Project पर जाएं और सभी आवश्यक विवरण भरकर नया प्रोजेक्ट बनाएं।
चरण 2 - लेआउट XML जोड़ें
res/layout/activity_main.xml में निम्न कोड जोड़ें:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="स्थानीय तारीख"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
उपरोक्त कोड में, हमने वर्तमान वर्ष और महीना दिखाने के लिए TextView लिया है।
चरण 3 - MainActivity.java जोड़ें
src/MainActivity.java में निम्न कोड जोड़ें। YearMonth API के लिए Android API लेवल 26+ आवश्यक है:
package com.example.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import java.time.YearMonth;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.date);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
YearMonth yearMonth = YearMonth.now();
textView.setText(yearMonth.toString());
}
}
}
मुख्य बिंदु
YearMonth.now()- वर्तमान वर्ष-महीना प्राप्त करता है- परिणाम ISO-8601 फॉर्मेट में होता है: yyyy-MM (उदाहरण: 2024-03)
- उपयोगी विधियाँ:
getYear(),getMonth(),getMonthValue(),lengthOfMonth()(महीने में दिनों की संख्या) - कैलेंडर UI, बिलिंग चक्र, मासिक रिपोर्ट के लिए आदर्श
ऐप चलाएं
अपने Android डिवाइस को कंप्यूटर से कनेक्ट करें। Android Studio में प्रोजेक्ट की एक्टिविटी फ़ाइल खोलें और टूलबार से Run आइकन पर क्लिक करें। अपना मोबाइल डिवाइस चुनें और डिफ़ॉल्ट स्क्रीन देखें।

उपरोक्त परिणाम में, यह वर्तमान स्थानीय वर्ष और महीना दिखा रहा है।