在android中获取月份和年份?

Posted

技术标签:

【中文标题】在android中获取月份和年份?【英文标题】:Getting Month and Year in android? 【发布时间】:2014-08-28 07:59:33 【问题描述】:

如何 (当我们单击左右箭头时,按升序和降序排列)。

我附上截图供参考

https://s28.postimg.org/gyuzzumkd/time_date.jpg

我使用了他的 0x5f3759df 答案中的一些代码:

我的代码:

import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class Fragment_A extends Fragment implements OnClickListener 

    private ListView list;
    ImageButton next,previous;
    ImageView newmemory;
    TextView tv;

    public Fragment_A() 
        // Required empty public constructor
    

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) 
        // Inflate the layout for this fragment
        View rootview = inflater.inflate(R.layout.fragment_a, container, false);
        // call the views with this layout
        list = (ListView) rootview.findViewById(R.id.listView1);
        next = (ImageButton) rootview.findViewById(R.id.ibLeftarrow);
        previous = (ImageButton) rootview.findViewById(R.id.ibRightarrow);
        newmemory = (ImageView)rootview.findViewById(R.id.ivNewMemory);
        tv = (TextView) rootview.findViewById(R.id.tvMonth);
        next.setOnClickListener(this);
        previous.setOnClickListener(this);
        newmemory.setOnClickListener(this);
        CustomAdapterA adapter = new CustomAdapterA(getActivity());
        list.setAdapter(adapter);
        return rootview;    
    

    @Override
    public void onClick(View v) 
        // TODO Auto-generated method stub
        if(v.getId()==R.id.ibLeftarrow)
        
            createDialogWithoutDateField().show();
        
        else if (v.getId()==R.id.ibRightarrow)
        
            createDialogWithoutDateField().show();
        
        else  if (v.getId()==R.id.ivNewMemory)
        
             Intent i = new Intent(getActivity(), NewMemory.class);
             startActivity(i);
               
    

     private DatePickerDialog createDialogWithoutDateField()

            DatePickerDialog dpd = new DatePickerDialog(getActivity(), datePickerListener ,2014,1, 24);
            try
            java.lang.reflect.Field[] datePickerDialogFields = dpd.getClass().getDeclaredFields();
            for (java.lang.reflect.Field datePickerDialogField : datePickerDialogFields)  
                if (datePickerDialogField.getName().equals("mDatePicker")) 
                    datePickerDialogField.setAccessible(true);
                    DatePicker datePicker = (DatePicker) datePickerDialogField.get(dpd);
                    java.lang.reflect.Field[] datePickerFields = datePickerDialogField.getType().getDeclaredFields();
                    for (java.lang.reflect.Field datePickerField : datePickerFields) 
                        Log.i("test", datePickerField.getName());
                       if ("mDaySpinner".equals(datePickerField.getName())) 
                          datePickerField.setAccessible(true);
                          Object dayPicker = new Object();
                          dayPicker = datePickerField.get(datePicker);
                          ((View) dayPicker).setVisibility(View.GONE);
                       
                    
                 

              
            catch(Exception ex)
            
            return dpd;

             

     private DatePickerDialog.OnDateSetListener datePickerListener 
     = new DatePickerDialog.OnDateSetListener() 

// when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear,
        int selectedMonth, int selectedDay) 

    int year = selectedYear;
    int month = selectedMonth;

    tv.setText(new StringBuilder().append(month+1)
            .append(" ").append(year)
               .append(" "));


 
;


【问题讨论】:

您可以使用pickers。 Related SO question @0x5f3759df:感谢您的回复,我解决了这个问题。 【参考方案1】:

按下上一个按钮时

if (mCalendar.get(Calendar.MONTH) == mCalendar
                                .getActualMinimum(Calendar.MONTH)) 
                            mCalendar.set((mCalendar.get(Calendar.YEAR) - 1),
                                    mCalendar.getActualMaximum(Calendar.MONTH), 1);
                         else 
                            mCalendar.set(Calendar.MONTH,
                                    mCalendar.get(Calendar.MONTH) - 1);

                        

按下下一步按钮

if (mCalendar.get(Calendar.MONTH) == mCalendar
                                    .getActualMaximum(Calendar.MONTH)) 
                                mCalendar.set((mCalendar.get(Calendar.YEAR) + 1),
                                        mCalendar.getActualMinimum(Calendar.MONTH), 1);
                             else 
                                mCalendar.set(Calendar.MONTH,
                                        mCalendar.get(Calendar.MONTH) + 1);

                            

onCreate() 中像这样初始化 mCalendar

Calendar mCalendar = Calendar.getInstance();

获取月份和年份

String str = android.text.format.DateFormat.format("MMMM yyyy", mCalendar).toString();

【讨论】:

【参考方案2】:

您可以使用 getInstance() 函数获取日历日期,然后根据按钮事件侦听器(左右箭头)将年份和月份解析为字符串数组。下面是getinstance()函数的代码:

 final Calendar c = Calendar.getInstance();
    year = c.get(Calendar.YEAR);
    month = c.get(Calendar.MONTH);

【讨论】:

【参考方案3】:
    dateFormat = new SimpleDateFormat("dd-MMMM-yyyy", locale);


    c = Calendar.getInstance();
    String date = dateFormat.format(c.getTime());


    mYear = c.get(Calendar.YEAR);
    mMonth = c.get(Calendar.MONTH);
    mDay = c.get(Calendar.DAY_OF_MONTH);
    updateDisplayLeaveDate();


private void updateDisplayLeaveDate() 
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.YEAR, mYear);
    cal.set(Calendar.MONTH, mMonth);
    cal.set(Calendar.DAY_OF_MONTH, mDay);
    dateMorning = cal.getTime();

    String date = dateFormat.format(dateMorning);
    tvLeaveDateDisplay.setText(date);
    leaveDate = Util.getDateFormat(mDay, mMonth, mYear);



// the callback received when the user "sets" the date in the dialog
private DatePickerDialog.OnDateSetListener mDateSetListenerMorning = new DatePickerDialog.OnDateSetListener() 
    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) 
        mYear = year;
        mMonth = monthOfYear;
        mDay = dayOfMonth;
        updateDisplayLeaveDate();
    
;

【讨论】:

以上是关于在android中获取月份和年份?的主要内容,如果未能解决你的问题,请参考以下文章

Android DatePicker仅更改为月份和年份

如何仅显示月份和年份字段的 android 日期选择器?

如何只用月份和年份字段显示android的日期选择器?

Android随时显示当前时间

仅包含日期和月份的Android日期选择器

PHP 从 43556 等值中获取月份和年份