java - 将字符串日期与今天的xml文件和当前日期进行比较,如果日期过期,则将该节点从xml中删除 在格式 dd/MM/yy 中,字符串日期为

  显示原文与译文双语对照的内容
0 0
 public void BrisiOdXML(List<Nastani> lista)
 {
 try {
//reading from xml
 File fXmlFile = new File("/Users/Darko/workspace/Seminarska_1/nastani.xml");
 DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
 DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
 Document doc = dBuilder.parse(fXmlFile);
 doc.getDocumentElement().normalize();
 NodeList nList = doc.getElementsByTagName("Nastan");

//today's 日期//无法在没有时间的情况下以 dd/MM/yy 格式获取日期

 Calendar today = Calendar.getInstance();
 today.set(Calendar.HOUR_OF_DAY, 0);
 today.set(Calendar.MINUTE, 0);
 today.set(Calendar.SECOND, 0);
 today.set(Calendar.MILLISECOND, 0);

//searching 列表

 for (int temp = 0; temp <nList.getLength(); temp++) {
 Node nNode = nList.item(temp); 
 if (nNode.getNodeType() == Node.ELEMENT_NODE) {
 Element eElement = (Element) nNode;
//string date from xml dile in format dd/MM/yy 
 Date datexml = (Date) new SimpleDateFormat("dd/MM/yy").parse(eElement.getElementsByTagName("Datum").item(temp).getTextContent()); 
//comparing today's date with date from the xml file
 if (today.after(datexml))
 {
//removing node
 nNode.getParentNode().removeChild(nNode);
 doc.normalize();
 prettyPrint(doc);
 }
 }
 }
 } catch (Exception e) {
 e.printStackTrace();
 }
 } 

//printing 新建xml文件

 public static final void prettyPrint(Document xml) throws Exception {
 Transformer tf = TransformerFactory.newInstance().newTransformer();
 tf.setOutputProperty(OutputKeys.ENCODING,"UTF-8");
 tf.setOutputProperty(OutputKeys.INDENT,"yes");
 Writer out = new StringWriter();
 tf.transform(new DOMSource(xml), new StreamResult(out));
 System.out.println(out.toString());
 }
}

如果日期过期,//假定该节点从xml文件中 delete,但当我读取xml列表时,它只显示错误信息。

时间: 原作者:

0 0

你在方法 after的日历 vs 日期中所做的比较。 我不确定这是否正确。 尝试将 datexml 转换为 Calendar,方法如下:

Calendar calendar = Calendar.getInstance();
calendar.setTime(datexml);
...