Android Calendar Get getCalenderFromDateString(String dateString)

Here you can find the source of getCalenderFromDateString(String dateString)

Description

parse Date string like: "Sat Sep 21 23:39:40 +0800 2013" to java.util.Date object

Parameter

Parameter Description
dateString a parameter

Return

Calendar representation or epoch if parse error

Declaration

public static Calendar getCalenderFromDateString(String dateString) 

Method Source Code

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

public class Main {
    /**//from  ww  w  .java2 s  . c  o  m
     * H: 00-23, h: 0-11(am/pm) Z: timezone (+8000) see full doc <a href=
     * "http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html"
     * >docs.oracle.com</a>. Locale.US is always available on all platform (see
     * Locale javadoc)
     */

    private static final java.text.DateFormat weiboCreateDateFormat = new SimpleDateFormat(
            "EEE MMM dd HH:mm:ss Z yyyy", Locale.US);

    /**
     * parse Date string like: "Sat Sep 21 23:39:40 +0800 2013" to
     * java.util.Date object
     * 
     * @param dateString
     * @return Calendar representation or epoch if parse error
     */
    public static Calendar getCalenderFromDateString(String dateString) {
        Calendar cal = Calendar.getInstance();
        try {
            cal.setTime(weiboCreateDateFormat.parse(dateString));
        } catch (ParseException e) {
            cal.setTime(new Date(0));
        }
        return cal;
    }
}

Related

  1. getYear(Calendar c)
  2. extractDateFromCalendarTime(Calendar time)
  3. extractTimeFromCalendarTime(Calendar time)
  4. getCurrentCalendar()
  5. getCurrentDateByOffset(String format, int calendarField, int offset)