在Android中显示错误值,第二天或前一天的日期名称

时间:2013-08-07 13:17:45

标签: android datepicker

public class Main extends Activity {    
    protected static final int dialog = 0;
    private int Day;
    private int Month;
    private int Year;
    Button mbt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mbt = (Button) findViewById(R.id.button1);
        mbt.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {
            showDialog(dialog);
        }
        });
    }

    private void  UpdateDisplay() {
        String day = (DateFormat.format("EEEEEE", new Date(Year, Month, Day))).toString();
        Toast.makeText(Main.this, day, Toast.LENGTH_LONG).show();
    }

    private DatePickerDialog.OnDateSetListener 
    mDateSetListener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker arg0, int year, int monthOfYear, int dayOfMonth) {
            Year = year;
            Month = monthOfYear;
            Day = dayOfMonth;
            UpdateDisplay();
        }
    };

    protected Dialog onCreateDialog(int id){
        switch(id) {
            case dialog:
                return new DatePickerDialog(this ,mDateSetListener ,Year , Month , Day);
        }
        return null;
    }
}

1 个答案:

答案 0 :(得分:2)

当您选择日期时,Android会将日期修改为您自己的时区,您应该使用Calendar创建Calendar.getInstance(TimeZone.getDefault())类型变量并将其calendar.set(int year, int month, int day)发出。如果您不确定某事,请查看http://developer.android.com/reference/java/util/Calendar.html! :)

正确的代码是:

public class Main extends Activity {    
protected static final int dialog = 0;
private int Day;
private Calendar c;
private int Month;
private int Year;

    Button mbt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        c = new Calendar(TimeZone.getDefault());
        mbt = (Button) findViewById(R.id.button1);
        mbt.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {
            showDialog(dialog);
        }
        });
    }

    private void  UpdateDisplay() {
        String day = c.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.getDefault());
        Toast.makeText(Main.this, day, Toast.LENGTH_LONG).show();
    }

    private DatePickerDialog.OnDateSetListener 
    mDateSetListener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker arg0, int year, int monthOfYear, int dayOfMonth) {
            Year = year;
            Month = monthOfYear;
            Day = dayOfMonth;
            c.set(Year, Month, Day);
            UpdateDisplay();
        }
    };

    protected Dialog onCreateDialog(int id){
        switch(id) {
            case dialog:
                return new DatePickerDialog(this ,mDateSetListener ,Year , Month , Day);
        }
        return null;
    }
}