如何从日历中获取日期[复制]
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何从日历中获取日期[复制]相关的知识,希望对你有一定的参考价值。
这个问题在这里已有答案:
我有另一个课程,在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日期和时间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)中都有很好的解释。
如果碰巧不是美国/托尔托拉,请替换您想要的时区。
您对TextView DatePicker使用了错误的构造函数
更新代码:
TextViewDatePicker editTextDatePicker = new TextViewDatePicker(ProfileGeneral.this, editTextForAge);
同
TextViewDatePicker editTextDatePicker = new TextViewDatePicker(ProfileGeneral.this, editTextForAge,0,0);
以上是关于如何从日历中获取日期[复制]的主要内容,如果未能解决你的问题,请参考以下文章
js实现日历可获得指定日期周数及星期几示例分享(js获取星期几)
js实现日历可获得指定日期周数及星期几示例分享(js获取星期几)