如何比较DatePicker日期和日历日期 [英] How can I compare a DatePicker date to the Calendar Date

查看:171
本文介绍了如何比较DatePicker日期和日历日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望这是一个简单的问题. 如何将在DatePicker中输入的日期与当前日历日期进行比较?

A simple question I hope. How can I compare the date entered into a DatePicker to the current Calendar date?

要说明一下,如果我将DatePicker日期存储为字符串,该如何将其与android系统日历上的当前日期进行比较?

To explain, If I have the DatePicker date stored as a string, how could I compare that to the current date on the android system calendar?

    protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dpresult);

textView1 = (TextView)findViewById(R.id.textView1);

button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener(){

public void onClick(View v){         

   Intent intent = new Intent(datePickerResult.this, calander.class);

    startActivity(intent);
}
});

Bundle extras = getIntent().getExtras(); 

year = extras.getInt("year"); 
day = extras.getInt("day"); 
month = extras.getInt("month");

textView1.setText(day+"");

} }

推荐答案

例如,您从日期选择器中获取了类似于字符串的日期,然后

for example you got the date like string from datepicker then

String sDate = "05-10-2012"; // suppose you create this type of date as string then

SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");

Date date = sdf.parse(sDate);

Calendar c = Calendar.getInstance();

c.getTime().compareTo(date);

这取决于您的字符串或如何获取?您可以从datepicker单独获取所有内容,然后直接在日历实例中设置

it depending on your string or how you can get? you can get all individually from datepicker then directly set in calendar instance

Calendar my = Calendar.getInstance();
my.set(year, month, day);

现在比较

my.compareTo(Calendar.getInstance());

这篇关于如何比较DatePicker日期和日历日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆