如何从日历中获取日期[复制]

问题描述 投票:-4回答:2

这个问题在这里已有答案:

我有另一个课程,在textview中显示日历到用户和设置日期。但是如何获取日期(从这里开始的年份?用于计算一般用户的年龄)。

这是我正在使用的代码。这是:TextViewDatePicker editTextDatePicker = new TextViewDatePicker implement class,它在文本视图中显示用户和设置日期的日历。我不知道为此代码设置此日期:Calendar dateOfYourBirth = new GregorianCalendar();

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.profile_fragment);
    editTextForAge = (TextView)  findViewById(R.id.ed_calendar);
    Window window = this.getWindow();

      TextViewDatePicker editTextDatePicker = new TextViewDatePicker(ProfileGeneral.this, editTextForAge);

    Calendar dateOfYourBirth = new GregorianCalendar();

   Calendar today = Calendar.getInstance();
   int yourAge = today.get(Calendar.YEAR) - dateOfYourBirth.get(Calendar.YEAR);
   dateOfYourBirth.add(Calendar.YEAR, yourAge);
   if (today.before(dateOfYourBirth)) {
      yourAge--;
   }

这是显示用户日历的类:

public class TextViewDatePicker
    implements View.OnClickListener, DatePickerDialog.OnDateSetListener {
public static final String DATE_SERVER_PATTERN = "yyyy-MM-dd";
private DatePickerDialog mDatePickerDialog;
private TextView mView;
private Context mContext;
private long mMinDate;
private long mMaxDate;

public TextViewDatePicker(Context context, TextView view) {
    this(context, view, 0, 0);
}

public TextViewDatePicker(Context context, TextView view, long minDate, long maxDate) {
    mView = view;
    mView.setOnClickListener(this);
    mView.setFocusable(false);

    mContext = context;
    mMinDate = minDate;
    mMaxDate = maxDate;
}

@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.YEAR, year);
    calendar.set(Calendar.MONTH, monthOfYear);
    calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
    Date date = calendar.getTime();

    SimpleDateFormat formatter = new SimpleDateFormat(DATE_SERVER_PATTERN);
    mView.setText(formatter.format(date));
}

@Override
public void onClick(View v) {
    Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
    mDatePickerDialog = new DatePickerDialog(mContext, this, calendar.get(Calendar.YEAR),
            calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
    if (mMinDate != 0) {
        mDatePickerDialog.getDatePicker().setMinDate(mMinDate);
    }
    if (mMaxDate != 0) {
        mDatePickerDialog.getDatePicker().setMaxDate(mMaxDate);
    }
    mDatePickerDialog.show();
}
java android android-studio calendar
2个回答
1
投票

使用现代Java日期和时间API java.time可以轻松实现这一目标。

@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
    // date picker month is 0-based, so add 1 to it
    LocalDate datePickerDate = LocalDate.of(year, monthOfYear + 1, dayOfMonth);
    mView.setText(datePickerDate.toString());

    LocalDate today = LocalDate.now(ZoneId.of("America/Tortola"));
    long yourAge = ChronoUnit.YEARS.between(datePickerDate, today);
}

你能在Android上使用它吗?当然!对于大多数Android设备,你需要获得ThreeTenABP,java.time从Java 8到Android Java 7的后端。在this question: How to use ThreeTenABP in Android Project(这是JSR-310的ThreeTen,java.time首次描述,以及Android Backport的ABP)中都有很好的解释。

如果碰巧不是美国/托尔托拉,请替换您想要的时区。


0
投票

您对TextView DatePicker使用了错误的构造函数

更新代码:

TextViewDatePicker editTextDatePicker = new TextViewDatePicker(ProfileGeneral.this, editTextForAge);

TextViewDatePicker editTextDatePicker = new TextViewDatePicker(ProfileGeneral.this, editTextForAge,0,0);
© www.soinside.com 2019 - 2024. All rights reserved.