I have some xml like:
<tag attr1="20190325235500 +0200">
</tag>
<tag attr1="20190326000000 +0200">
</tag>
<tag attr1="20190326000000 +0200">
</tag>
<tag attr1="20190326000000 +0200">
</tag>
I parse it with XMLPullParser like:
if (parser.getName().equals(tag)) {
attr1 = parser.getAttributeValue(0);
item = new SomeItem(attr1);
item.setAttr1Name(attr1);
items.add(item);
}
So I have a lot of records like attr1="20190326000000 +0200"
Now I want to filter all this records and leave only those which are starting 20190326
, for example.
I thought this would help me:
if (parser.getName().equals(tag) && parser.getAttributeValue(0).substring(0, 8).equals("20190326"))
but I was wrong and this if
causing nullpointer exception in my item.setAttr1Name(attr1);
What I can try to do? How to build right if
?
Maybe I should use something of Date, Calendar, DateFormat?..
Try this
if (parser.getName().equals(tag)) {
attr1 = parser.getAttributeValue(0);
if (attr1 != null && attr1.startsWith("20190326") {
item = new SomeItem(attr1);
item.setAttr1Name(attr1);
items.add(item);
}
}