我想存储所有出生日期并与当前日期相匹配。
我得到了所有出生日期,但是当我想要匹配时,我会遇到时间和日期格式错误。
LoadData();
ArrayList<User> arrayList = new ArrayList<>();
String CurrentData = GetCurrentData();
String BirthdayData = GetBirthdayData(arrayList);
Log.e("String", CurrentData + " " + BirthdayData);
if (CurrentData.equals(BirthdayData)) {
localData.set_hour(0);
localData.set_min(0);
NotificationScheduler.setReminder(NavDrawerActivity.this,
AlarmReceiver.class, localData.get_hour(), localData.get_min());
} else {
}
此方法用于获取所有出生日期:
public String GetBirthdayData(ArrayList<User> bdayList)
{
String birthday = " ";
for (int i=0;i<bdayList.size();i++)
{
User user=bdayList.get(i);
birthday =user.getBday();
}
Log.e("GetBirthdayData1212", birthday);
User user=new User();
String birthday = user.getBday();
SimpleDateFormat dateFormat = new SimpleDateFormat("d/M/yyyy");
Date myDate = null;
try {
myDate = dateFormat.parse(birthday);
Log.e("myDate", birthday);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat timeFormat = new SimpleDateFormat("d/M");
String birthdayData = timeFormat.format(myDate);
return birthdayData.toString();
}
此方法获取当前日期:
public String GetCurrentData() {
//get current Date
DateFormat dayf = new SimpleDateFormat("d/M");
String currentDate = dayf.format(new Date());
return currentDate;
}
此方法用于获取所有数据并存储在ArrayList
:
public void LoadData() {
mDatabaseReference =
FirebaseDatabase.getInstance().getReference("users");
mDatabaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
BdayList = new ArrayList<User>();
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
User value = dataSnapshot1.getValue(User.class);
User fire = new User();
String bday = value.getBday();
fire.setBday(bday);
BdayList.add(fire);
GetBirthdayData(BdayList);
Log.e("BdayList", BdayList.toString());
}
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.w("Hello", "Failed to read value.", error.toException());
}
});
}
logcat的:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual
method 'long java.util.Date.getTime()' on a null object reference
at java.util.Calendar.setTime(Calendar.java:1089)
at java.text.SimpleDateFormat.format(SimpleDateFormat.java:953)
at java.text.SimpleDateFormat.format(SimpleDateFormat.java:946)
at java.text.DateFormat.format(DateFormat.java:337)
答案 0 :(得分:0)
在这里你创建一个没有生日的用户..
// Is this even the format that user.getBday() returns?
SimpleDateFormat dateFormat = new SimpleDateFormat("d/M/yyyy");
Date myDate = null;
try {
// If not, this is wrong
myDate = dateFormat.parse(birthday);
Log.d("myDate", birthday); // Do you see this in the logs?
} catch (ParseException e) {
e.printStackTrace(); // or this?
}
// Both these lines should be within the try block
// You're not sure if the Date was assigned
SimpleDateFormat timeFormat = new SimpleDateFormat("d/M");
String birthdayData = timeFormat.format(myDate);
然后你尝试解析什么,所以打印出一个错误,你会抓住它,但是然后继续重新格式化它,但是不要抓住
private Date birthday;
public Date getBday() { return this.birthday==null ? new Date() : this.birthday ; }
就个人而言,如果没有给出生日
,我会让用户对象返回当天的Date对象String birthday = " ";
for (int i=0;i<bdayList.size();i++)
{
User user=bdayList.get(i);
birthday =user.getBday();
}
或者你需要输入构造函数,而不是使用空的。
其次,此循环不执行任何操作,因此您可以将其删除..或实际使用其中的值而不是立即创建空白用户/生日
public List<String> getBirthdayData(List<User> bdayList) {
List<String> dates = new ArrayList<>();
SimpleDateFormat timeFormat = new SimpleDateFormat("d/M");
SimpleDateFormat dateFormat = new SimpleDateFormat("d/M/yyyy");
for (User u : bdayList) {
String birthday =u.getBday();
Log.d("myDate", birthday);
try {
Date d = dateFormat.parse(birthday);
dates.add(timeFormat.format(d));
} catch (ParseException e) {
e.printStackTrace();
}
}
return dates;
}
例如,如果您打算从用户列表中格式化所有生日字符串
{{1}}