Returns a calendar object based on the date provided and the format of that date - Android java.util

Android examples for java.util:Calendar Format

Description

Returns a calendar object based on the date provided and the format of that date

Demo Code


//package com.java2s;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import android.util.Log;

public class Main {
    public static final String DateFormat = "EEE, d MMM yyyy HH:mm:ss";

    /**// w ww.java 2s  .  c o  m
     * Returns a calendar object based on the date provided and the format of that date
     * @param date
     * @param dateFormat
     * @return
     * @throws ParseException 
     */
    public static Calendar createCalendarFromDateAndFormat(String itemDate,
            String itemDateFormat) {
        Calendar cal = new GregorianCalendar();
        try {
            DateFormat dateFormat = new SimpleDateFormat(itemDateFormat,
                    Locale.US);
            Date date = dateFormat.parse(itemDate);
            cal.setTime(date);
        } catch (ParseException e) {
            Log.e("createCalendarFromDateAndFormat",
                    "Caught ParseException while processing date");
        }
        return cal;
    }
}

Related Tutorials