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;

public class Main {
    private static final ThreadLocal<SimpleDateFormat> w3cdtf = new ThreadLocal<SimpleDateFormat>() {
        protected SimpleDateFormat initialValue() {
            try {
                return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
            } catch (Exception e) {
                throw new Error("Exception in static initializer: " + e.toString());
            }
        }
    };

    /**
     * Creates a Date object from the string passed to it. The passed string
     * needs to represent the date is W3C Date and Time Format
     * 
     * @param date
     *            Source string containing date in W3C Date and Time Format
     * @return Date object containing the date extracted from the string it is
     *         passed
     * @throws ParseException
     *             If the begginning of <var>date</var> cannot be parsed
     */
    public static Date dateFromW3CDTF(String date) throws ParseException {
        int len = date.length();
        if (len > 5 && date.charAt(len - 3) == ':') {
            char c = date.charAt(len - 6);
            if (c == '+' || c == '-')
                date = date.substring(0, len - 3) + date.substring(len - 2);
        }
        return (Date) w3cdtf.get().parseObject(date);
    }
}