Java Date Parse asDate(final String literal)

Here you can find the source of asDate(final String literal)

Description

Method formats the given literal as date literal.

License

Apache License

Parameter

Parameter Description
literal the string literal

Exception

Parameter Description
ParseException thrown if literal cannot be format as date

Return

the date literal

Declaration

public static final Calendar asDate(final String literal) throws ParseException 

Method Source Code

//package com.java2s;
/*/*from  w ww  .j  a v  a2s .  c  o m*/
 * Copyright: Copyright 2010 Topic Maps Lab, University of Leipzig. http://www.topicmapslab.de/    
 * License:   Apache License, Version 2.0 http://www.apache.org/licenses/LICENSE-2.0.html
 *  
 * @author Sven Krosse
 * @email krosse@informatik.uni-leipzig.de
 *
 */

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.LinkedList;
import java.util.List;

public class Main {
    /**
     * translation patterns of date
     */
    private static final List<String> datePatterns = new LinkedList<String>();

    /**
     * Method formats the given literal as date literal.
     * 
     * @param literal
     *            the string literal
     * @return the date literal
     * @throws ParseException
     *             thrown if literal cannot be format as date
     */
    public static final Calendar asDate(final String literal) throws ParseException {
        Date date = null;
        for (String pattern : datePatterns) {
            try {
                date = new SimpleDateFormat(pattern).parse(literal);
            } catch (ParseException e) {
                // VOID
            }
        }
        if (date == null) {
            throw new ParseException("Invalid date pattern", -1);
        }
        Calendar c = new GregorianCalendar();
        c.setTime(date);
        return c;
    }
}

Related

  1. asDate(Date date, String format)
  2. asDate(final long time)
  3. asDate(final Map map, final String key)
  4. asDate(String date)
  5. asDate(String param, SimpleDateFormat format)
  6. asDate(String s)
  7. asDate(String string)