11

我的数据选择器有问题

我使用代码获取如下所示的日期、月份和年份

           DatePicker datePicker;
           datePicker = (DatePicker) findViewById(R.id.dateselect);

           int   day  = datePicker.getDayOfMonth();
           int   month= datePicker.getMonth() + 1;
           int   year = datePicker.getYear();

但是当我打印日期时,它显示的值是 7 而不是 07,而对于月份,它显示的值是 2 而不是 02

我希望这些整数数据采用日期格式,即;例如:02-02-2013, 24-12-2013
有没有可能的方法????

4

5 回答 5

46

您可以像这样格式化日期:

int   day  = datePicker.getDayOfMonth();
int   month= datePicker.getMonth();
int   year = datePicker.getYear();
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, day);

SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
String formatedDate = sdf.format(calendar.getTime());

您可以通过调用将 String 解析回 Date 对象

Date date = sdf.parse(formatedDate);
于 2013-02-13T10:30:25.620 回答
27
 public String checkDigit(int number)
    {
        return number<=9?"0"+number:String.valueOf(number);
    }

基本用途:

month=checkDigit(month);
input 7
output 07
   String date=checkDigit(monthOfYear+1)+"/"+checkDigit(dayOfMonth)+"/"+year;
于 2013-02-13T10:29:36.313 回答
3
<?xml version="1.0" encoding="utf-8"?>

<Button
    android:id="@+id/btnChangeDate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Change Date" />

<TextView
    android:id="@+id/lblDate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Current Date (M-D-YYYY): "
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/tvDate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=""
    android:textAppearance="?android:attr/textAppearanceLarge" />

<DatePicker
    android:id="@+id/dpResult"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

package com.mkyong.android;

   import java.util.Calendar;
 import android.app.Activity;
 import android.app.DatePickerDialog;
 import android.app.Dialog;
 import android.os.Bundle;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.widget.Button;
 import android.widget.DatePicker;
 impot android.widget.TextView;

 public class MyAndroidAppActivity extends Activity {

private TextView tvDisplayDate;
private DatePicker dpResult;
private Button btnChangeDate;

private int year;
private int month;
private int day;

static final int DATE_DIALOG_ID = 999;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    setCurrentDateOnView();
    addListenerOnButton();

}

// display current date
public void setCurrentDateOnView() {

    tvDisplayDate = (TextView) findViewById(R.id.tvDate);
    dpResult = (DatePicker) findViewById(R.id.dpResult);

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

    // set current date into textview
    tvDisplayDate.setText(new StringBuilder()
        // Month is 0 based, just add 1
        .append(month + 1).append("-").append(day).append("-")
        .append(year).append(" "));

    // set current date into datepicker
    dpResult.init(year, month, day, null);

}

public void addListenerOnButton() {

    btnChangeDate = (Button) findViewById(R.id.btnChangeDate);

    btnChangeDate.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            showDialog(DATE_DIALOG_ID);

        }

    });

}

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DATE_DIALOG_ID:
       // set date picker as current date
       return new DatePickerDialog(this, datePickerListener, 
                     year, month,day);
    }
    return null;
}

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) {
        year = selectedYear;
        month = selectedMonth;
        day = selectedDay;

        // set selected date into textview
        tvDisplayDate.setText(new StringBuilder().append(month + 1)
           .append("-").append(day).append("-").append(year)
           .append(" "));

        // set selected date into datepicker also
        dpResult.init(year, month, day, null);

    }
};

}

于 2013-02-13T10:25:01.953 回答
2

API 级别 12开始,DatePickerhas public CalendarView getCalendarView (),使用此视图可以获取日期:

long dateTime = datePicker.getCalendarView().getDate();
Date date = new Date(dateTime);

然后根据需要格式化:

DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
String formattedDate = dateFormat.format(date);
于 2014-10-09T07:49:22.370 回答
1

您可以使用自定义方法以所需格式设置日期和月份

    private static String  pad(int c)
    {
        return c>=10 ? ""+c : "0"+c;      
    }

而不是使用日期月份的值使用pad(date)pad(month)

希望这可以帮助。

于 2013-02-13T10:31:01.257 回答