如何比较任何日期与当前日期,而不考虑年度 [英] How do I compare any date with the current date, without taking the year into account
本文介绍了如何比较任何日期与当前日期,而不考虑年度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将日期与当前日期进行比较,而不考虑年度,我想更改每一天的文本。
I want to compare the date with the current date, without taking the year into account, and I want to change the text for each day
这是我的代码
TextView textView = (TextView)findViewById(R.id.textView);
String valid_until = "7/3";
String valid_until1 = "8/3";
String valid_until2 = "9/3";
String valid_until3 = "10/3";
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM");
Date strDate = null;
Date strDate1 = null;
Date strDate2 = null;
Date strDate3 = null;
try {
strDate = sdf.parse(valid_until);
strDate1 = sdf.parse(valid_until1);
strDate2 = sdf.parse(valid_until2);
strDate3 = sdf.parse(valid_until3);
} catch (ParseException e) {
e.printStackTrace();
}
if (new Date() == (strDate)) {
textView.setText("7 m");
}
if (new Date() == (strDate1)) {
textView.setText("8 m");
}
if (new Date() == (strDate2)) {
textView.setText("9 m");
}
if (new Date() == (strDate3)) {
textView.setText("10 m");
}
推荐答案
如何比较任何日期与当前日期,而不考虑年度
How do I compare any date with the current date, without taking the year into account
一个选项,您可以使用Calendar对象。设置每个日期,然后简单地比较DAY_OF_YEAR值。
One option, you could use a Calendar object. Set the date on each, and then simply compare the "DAY_OF_YEAR" value.
https ://developer.android.com/reference/java/util/Calendar.html#DAY_OF_YEAR
这篇关于如何比较任何日期与当前日期,而不考虑年度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文