简体   繁体   中英

How to add month in java xml

I am currently working on xml data project. So far I have successfully connect my data.xml file to my java project using dom parser. Furthermore. I am also able to get the node values and print in on the console. What I am struggling with now is I want to write a logic loop at the end of the main class which the purpose is to increase the month of the start date by one such as 1/1/2002 -> 2/1/2002 ->3/1/2002. My date format is MM/dd/yyyy . I have part of my code below to show what I currently have. Help will be appertained. Thanks.

data.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<data>
   <username>theBigAristotle</username>
   <startdate>01/01/2002</startdate>
   <enddate>01/31/2002</enddate>
</data>

main.java

 public class main
        {      
          public static void main(String[] args) 
          {
              Calendar cal =null;

              String username = null;
              String startdate = null;
              String enddate = null;
              String date = null;
              String date_end = null;

            try {   

                  //read the xml file 

                  File data = new File("data.xml");  
                  DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();         
                  DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();           
                  Document doc = dBuilder.parse(data);         
                  doc.getDocumentElement().normalize();

                  NodeList nodes = doc.getElementsByTagName("data");      

                    for (int i = 0; i < nodes.getLength(); i++) {      
                  Node node = nodes.item(i);           
                      if (node.getNodeType() == Node.ELEMENT_NODE) {       
                           Element element = (Element) node;    

                           username = getValue("username", element);
                           startdate = getValue("startdate", element);
                           enddate = getValue("enddate", element);
                         }
                     }

           date = startdate;    

           //initial date
           Date date_int = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH).parse(date);
           cal = Calendar.getInstance();
           cal.setTime(date_int);

           //end date
           Date end_date = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH).parse(enddate);
           Calendar end_date_cal = Calendar.getInstance();  
           end_date_cal.setTime(end_date);

           date = date_end; 

              //write the content in xml file
                TransformerFactory transformerFactory = TransformerFactory.newInstance();
                Transformer transformer = transformerFactory.newTransformer();
                DOMSource source = new DOMSource(doc);
                StreamResult result = new StreamResult(new File("data.xml"));
                transformer.transform(source, result);

        } catch (Exception ex) {    
          log.error(ex.getMessage());       
          ex.printStackTrace();       
        }


      private static String getValue(String tag, Element element) {  
            NodeList nodes = element.getElementsByTagName(tag).item(0).getChildNodes();   
            Node node = (Node) nodes.item(0);   
            return node.getNodeValue();   
          }

      private static void setValue(String tag, Element element , String input) {  
            NodeList nodes = element.getElementsByTagName(tag).item(0).getChildNodes();   
            Node node = (Node) nodes.item(0); 
            node.setTextContent(input);


          } 

This code adds a month to a date

        final String sdate = "2012-01-01";
        final SimpleDateFormat df = new SimpleDateFormat( "yyyy-MM-dd" );
        final Date date = df.parse( sdate ); // conversion from String
        final java.util.Calendar cal = GregorianCalendar.getInstance();
        cal.setTime( date );
        cal.add( GregorianCalendar.MONTH, 1 ); // date manipulation
        System.out.println( "result: " + df.format( cal.getTime() ) ); // conversion to String

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM