Java Calendar Parse stringToCalendar(final String str, String format, boolean lenient)

Here you can find the source of stringToCalendar(final String str, String format, boolean lenient)

Description

Convert string to Calendar.

License

Apache License

Parameter

Parameter Description
str a parameter
format a parameter
lenient a parameter

Exception

Parameter Description
ParseException an exception

Return

Calendar

Declaration

public static final Calendar stringToCalendar(final String str, String format, boolean lenient)
        throws ParseException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class Main {
    public static final String FORMAT_DATE = "dd/MM/yyyy";

    /**//from ww w .ja  v a2s.  c  o  m
     * Convert DD/MM/YYYY string to Calendar.
     * 
     * @param str
     * @param lenient
     * @return Calendar
     * @throws ParseException
     */
    public static final Calendar stringToCalendar(final String str, boolean lenient) throws ParseException {
        return stringToCalendar(str, FORMAT_DATE, lenient);
    }

    /**
     * Convert string to Calendar.
     * 
     * @param str
     * @param format
     * @param lenient
     * @return Calendar
     * @throws ParseException
     */
    public static final Calendar stringToCalendar(final String str, String format, boolean lenient)
            throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.FRANCE);
        sdf.setLenient(lenient);
        Date date = sdf.parse(str);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal;
    }

    /**
     * Convert DD/MM/YYYY string to calendar (lenient is active).
     * 
     * @param str
     * @return Calendar
     * @throws ParseException
     */
    public static final Calendar stringToCalendar(final String str) throws ParseException {
        return stringToCalendar(str, true);
    }

    /**
     * Convert string to calendar (lenient is active).
     * 
     * @param str
     * @param format
     * @return Calendar
     * @throws ParseException
     */
    public static final Calendar stringToCalendar(final String str, String format) throws ParseException {
        return stringToCalendar(str, format, true);
    }
}

Related

  1. getCalendarObject(String value, boolean hasMills)
  2. str2Calendar(String pString)
  3. str2Calendar(String str, String format)
  4. string2Calendar(String data)
  5. stringCalendar(Calendar cal)
  6. stringToCalendar(String fecha, String formato)
  7. toCalendar(Object value, String format)
  8. toCalendar(String str)
  9. toGregorianCalendar(String value)