我试图使用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。
答案 0 :(得分:2)
尝试一下:-
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);
这对我有帮助。希望这对您也有帮助。谢谢
答案 1 :(得分:0)
这取决于您使用了多少M
如果使用2 M( MM ): 您的输出将为数字格式。
如果使用3 M( MMM ): 您的输出将以简短的月份名称显示。例如。俊
如果使用4 M( MMMM ): 您的输出将以长月份名称显示。例如。六月
代码:
val timeStampFormat = SimpleDateFormat("yyyy/MMMM/dd HH:mm:ssz")