Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    private static final SimpleDateFormat FULL_DATE = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z");
    private static final SimpleDateFormat FULL_DATE2 = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss a z");

    public static Date parseNodeAsDate(Node node) {
        if (node == null)
            return null;
        String text = parseNodeAsText(node);

        Date date = null;
        try {
            date = FULL_DATE.parse(text);
        } catch (ParseException e) {
            try {
                date = FULL_DATE2.parse(text);
            } catch (ParseException e2) {
                e2.printStackTrace();
            }
        }
        return date;
    }

    public static String parseNodeAsText(Node node) {
        if (node == null)
            return null;
        String text = null;
        if (node.getNodeType() == 3) {
            text = node.getNodeValue();
        } else {
            StringBuffer sb = new StringBuffer();
            NodeList children = node.getChildNodes();
            for (int i = 0; children != null && i < children.getLength(); i++) {
                Node child = children.item(i);
                if (child != null) {
                    int childType = child.getNodeType();
                    if (childType == 3 || childType == 4 || childType == 8) {
                        String childText = child.getNodeValue();
                        sb.append(childText);
                    } else {
                        sb.append(parseNodeAsText(child));
                    }
                }
            }

            if (sb.length() > 0)
                text = sb.toString();
        }
        return text;
    }
}