Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.Calendar;
import java.util.Date;

public class Main {
    /** Helper method - creates a <I>Date</I> object from the date string
    found in Phoenix XML documents.
       @param strXMLDate Date <I>String</I> from an XML document that
      follows the format "YYYY-MM-DDTHH:NN:SS.00000".
       @return <I>Date</I> object.
     */
    public static Date getDateFromXMLString(String strXMLDate) throws IllegalArgumentException {
        if (null == strXMLDate)
            return null;

        int nLength = strXMLDate.length();

        if (10 > nLength)
            throw new IllegalArgumentException(
                    "XML date value (" + strXMLDate + ") is not of the expected format (yyyy-mm-ddThh:nn:ss).");

        // Create date object from timestamp string.
        try {
            Calendar pCalendar = Calendar.getInstance();

            pCalendar.clear();

            int nHour = 0;
            int nMinute = 0;
            int nSecond = 0;

            if (13 <= nLength)
                nHour = Integer.parseInt(strXMLDate.substring(11, 13));

            if (16 <= nLength)
                nMinute = Integer.parseInt(strXMLDate.substring(14, 16));

            if (19 <= nLength)
                nSecond = Integer.parseInt(strXMLDate.substring(17, 19));

            pCalendar.set(Integer.parseInt(strXMLDate.substring(0, 4)),
                    Integer.parseInt(strXMLDate.substring(5, 7)) - 1, Integer.parseInt(strXMLDate.substring(8, 10)),
                    nHour, nMinute, nSecond);

            return pCalendar.getTime();
        }

        catch (NumberFormatException pEx) {
            throw new IllegalArgumentException(
                    "XML date value (" + strXMLDate + ") is not of the expected format (yyyy-mm-ddThh:nn:ss).");
        }
    }
}