get Calendar From String - Android java.util

Android examples for java.util:Calendar

Description

get Calendar From String

Demo Code


//package com.java2s;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import android.net.ParseException;

public class Main {
    public static Calendar getCalendarFromString(String dateString,
            String template) {// ww  w  .j a v a2  s  .  c om

        if (dateString != null) {
            SimpleDateFormat format = new SimpleDateFormat(template,
                    Locale.US);

            try {

                Date date = format.parse(dateString);
                return DateToCalendar(date);
            } catch (ParseException e) {
                e.printStackTrace();
            } catch (java.text.ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        } else {
            return null;
        }
        return null;
    }

    public static Calendar DateToCalendar(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal;
    }
}

Related Tutorials