2

I'm trying to get current month and year to textview using YearMonth object.

Currently I'm getting the out put as "2019-04" and I want to change the output to "April 2019". I've tried some ways but still couldn't able to get expected output.

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();
            }
        });
    }
}




I expect the output to be April 2019 but the actual output is 2019-04.

  • https://stackoverflow.com/questions/17290916/how-do-i-change-date-time-format-in-android You can check this post. – Shoaib Mirza Apr 03 '19 at 10:01
  • 1
    You need to add a date format MM represents Month in digits and MMM represents month in alphabets give it a try. `String myFormat = "EEE MMM dd HH:mm:ss Z yyyy"; //In which you need put here SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US); String dateField = sdf.format(myCalendar.getTime());` – kvaruna Apr 03 '19 at 10:01
  • Possible duplicate of [Month name as a string](https://stackoverflow.com/questions/6192781/month-name-as-a-string) – Ashish Apr 03 '19 at 10:02

1 Answers1

2

Try this:-

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);

This helps for me.Hope this will also help you.Thanks

Rahul Kushwaha
  • 5,473
  • 3
  • 26
  • 30