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);
...