Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import org.w3c.dom.Element;

import java.text.DateFormat;
import java.text.ParseException;

import java.util.Date;

public class Main {
    public static Date readDateIfExists(Element e, String attributeName, DateFormat format) throws ParseException {
        String dateStr = readAttributeValue(e, attributeName, null);

        if (dateStr == null) {
            return null;
        }

        return format.parse(dateStr);
    }

    public static String readAttributeValue(Element e, String attributeName) {
        if (!e.hasAttribute(attributeName)) {
            throw new IllegalStateException("Attribute '" + attributeName + "' is absent");
        }

        return e.getAttribute(attributeName);
    }

    public static String readAttributeValue(Element e, String attributeName, String defaultValue) {
        if (e.hasAttribute(attributeName)) {
            return e.getAttribute(attributeName);
        } else {
            return defaultValue;
        }
    }
}