Java Calendar Parse str2Calendar(String str, String format)

Here you can find the source of str2Calendar(String str, String format)

Description

str Calendar

License

Apache License

Declaration

public static Calendar str2Calendar(String str, String format) 

Method Source Code


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

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

public class Main {
    private static final String FORMAT = "yyyy-MM-dd HH:mm:ss";

    public static Calendar str2Calendar(String str) {
        return str2Calendar(str, null);

    }/* w w w. j a v  a  2  s.  c om*/

    public static Calendar str2Calendar(String str, String format) {

        Date date = str2Date(str, format);
        if (date == null) {
            return null;
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);

        return c;

    }

    public static Date str2Date(String str) {
        return str2Date(str, null);
    }

    public static Date str2Date(String str, String format) {
        if (str == null || str.length() == 0) {
            return null;
        }
        if (format == null || format.length() == 0) {
            format = FORMAT;
        }
        Date date = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            date = sdf.parse(str);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;

    }
}

Related

  1. convertStringToCalendar(String time)
  2. getCalendar(Object source)
  3. getCalendarObject(String value, boolean hasMills)
  4. str2Calendar(String pString)
  5. string2Calendar(String data)
  6. stringCalendar(Calendar cal)
  7. stringToCalendar(final String str, String format, boolean lenient)
  8. stringToCalendar(String fecha, String formato)