gpt4 book ai didi

android - 如何在android YearMonth中更改日期格式

转载 作者:搜寻专家 更新时间:2023-11-01 09:21:01 29 4
gpt4 key购买 nike

我正在尝试使用 YearMonth 对象将当前月份和年份发送到 textview。

目前我得到的输出为“2019-04”,我想将输出更改为“2019 年 4 月”。我已经尝试了一些方法,但仍然无法获得预期的输出。

Java


package com.ceb.meterreaderassistant;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.app.DatePickerDialog;
import android.text.InputType;
import java.time.YearMonth;
import java.text.DateFormat;
import java.text.DateFormatSymbols;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

public class EnterReading extends AppCompatActivity {

private ImageButton back_button_er;
DatePickerDialog dPicker;
DatePickerDialog mPicker;
EditText datePicker;
TextView monthPicker;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_enter_reading);



back_button_er = (ImageButton)findViewById(R.id.bt_enter_reading_back);
back_button_er.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(EnterReading.this, PreviousStatement.class);
startActivity(intent);
finish();
}
});


monthPicker =(TextView) findViewById(R.id.mp_enter_reading);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
YearMonth year = YearMonth.now();
monthPicker.setText(String.valueOf(year));
}

datePicker =(EditText) findViewById(R.id.dp_enter_reading);
datePicker.setInputType(InputType.TYPE_NULL);
datePicker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Calendar cldr = Calendar.getInstance();
int day = cldr.get(Calendar.DAY_OF_MONTH);
int month = cldr.get(Calendar.MONTH);
int year = cldr.get(Calendar.YEAR);
//date picker dialog
dPicker = new DatePickerDialog(EnterReading.this,R.style.MyDialogTheme,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {


datePicker.setText(dayOfMonth + "/" + (monthOfYear + 1) + "/" + year);
//monthPicker.setText((monthOfYear + 1) + " " + year);
}
}, year, month, day);
dPicker.show();
}
});
}
}




我预计输出是 2019 年 4 月,但实际输出是 2019-04。

最佳答案

试试这个:-

final Calendar today = Calendar.getInstance();
final String dateToday = String.valueOf(today.get(Calendar.MONTH));
final String year=String.valueOf(today.get(Calendar.YEAR));
final String[] monthName = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
final String month = monthName[today.get(Calendar.MONTH)];
date.setText(month+" "+year);

这对我有帮助。希望这对你也有帮助。谢谢

关于android - 如何在android YearMonth中更改日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55492268/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com