DateTimeFormatter formatter = DateTimeFormatter.ofPattern("M/d/yyyy");LocalDate ld = LocalDate.parse("2/1/2013", formatter);System.out.println("From " + formatter.format(ld));ld = ld.plusMonths(1);System.out.println("To " + formatter.format(ld));
哪些印刷品
From 2/1/2013To 3/1/2013
但是,您永远不要将值应用回XML文档。正如我在上一个问题中试图演示的那样,您需要更改
textContent节点的…
node.setTextContent(formatter.format(ld))
这就是为什么我建议使用xPath而不是浏览文档内容的原因
例如…
import java.io.File;import java.io.IOException;import java.time.LocalDate;import java.time.format.DateTimeFormatter;import java.time.format.DateTimeParseException;import javax.xml.parsers.documentBuilder;import javax.xml.parsers.documentBuilderFactory;import javax.xml.parsers.ParserConfigurationException;import javax.xml.transform.OutputKeys;import javax.xml.transform.Transformer;import javax.xml.transform.TransformerException;import javax.xml.transform.TransformerFactory;import javax.xml.transform.TransformerFactoryConfigurationError;import javax.xml.transform.dom.DOMSource;import javax.xml.transform.stream.StreamResult;import javax.xml.xpath.XPath;import javax.xml.xpath.XPathConstants;import javax.xml.xpath.XPathexpressionException;import javax.xml.xpath.XPathFactory;import org.w3c.dom.DOMException;import org.w3c.dom.document;import org.w3c.dom.Node;import org.xml.sax.SAXException;public class UpdateXML { public static void main(String[] args) { try { documentBuilderFactory f = documentBuilderFactory.newInstance(); documentBuilder b = f.newdocumentBuilder(); document doc = b.parse(new File("Data.xml")); XPath xPath = XPathFactory.newInstance().newXPath(); Node startDateNode = (Node) xPath.compile("/data/startdate").evaluate(doc, XPathConstants.NODE); startDateNode.setTextContent(addMonthTo(startDateNode.getTextContent())); xPath = XPathFactory.newInstance().newXPath(); Node endDateNode = (Node) xPath.compile("/data/enddate").evaluate(doc, XPathConstants.NODE); endDateNode.setTextContent(addMonthTo(endDateNode.getTextContent())); Transformer tf = TransformerFactory.newInstance().newTransformer(); tf.setOutputProperty(OutputKeys.INDENT, "yes"); tf.setOutputProperty(OutputKeys.METHOD, "xml"); tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); DOMSource domSource = new DOMSource(doc); StreamResult sr = new StreamResult(new File("AData.xml")); tf.transform(domSource, sr); } catch (ParserConfigurationException | SAXException | IOException | XPathexpressionException | DOMException | TransformerFactoryConfigurationError | IllegalArgumentException | TransformerException exp) { exp.printStackTrace(); } } public static String addMonthTo(String value) { String patterns[] = {"M/d/yyyy", "M/dd/yyyy", "MM/d/yyyy", "MM/dd/yyyy"}; LocalDate ld = null; for (String pattern : patterns) { try { ld = LocalDate.parse(value, DateTimeFormatter.ofPattern(pattern)); break; } catch (DateTimeParseException exp) { } } if (ld == null) { throw new DateTimeParseException("Could not parse " + value + " with available patterns", value, -1); } ld = ld.plusMonths(1); return DateTimeFormatter.ofPattern("MM/dd/yyyy").format(ld); }}
花了…
<?xml version="1.0" encoding="UTF-8" standalone="no"?><data> <username>admin</username> <password>12345</password> <interval>1</interval> <timeout>90</timeout> <startdate>1/1/2013</startdate> <enddate>06/01/2013</enddate> <ttime>1110</ttime></data>
并输出…
<?xml version="1.0" encoding="UTF-8" standalone="no"?><data> <username>admin</username> <password>12345</password> <interval>1</interval> <timeout>90</timeout> <startdate>02/01/2013</startdate> <enddate>07/01/2013</enddate> <ttime>1110</ttime></data>
Java 8我希望开始日期早6个月,然后结束日期
日历String endDatevalue = "07/01/2013";DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");LocalDate endDate = LocalDate.parse(endDatevalue, formatter);LocalDate startDate = endDate.minusMonths(6);String startDatevalue = formatter.format(startDate);
我更喜欢Joda-Time,但是
String endDatevalue = "07/01/2013";SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");Date endDate = sdf.parse(endDatevalue);Calendar cal = Calendar.getInstance();cal.setTime(endDate);cal.add(Calendar.MONTH, -6);Date startDate = cal.getTime();String startDateVaue = sdf.format(startDate);
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)