Java TimeZone Format getCalendar(String dateString, String format, TimeZone tz)

Here you can find the source of getCalendar(String dateString, String format, TimeZone tz)

Description

get Calendar

License

LGPL

Declaration

public static Calendar getCalendar(String dateString, String format,
            TimeZone tz) throws Exception 

Method Source Code

//package com.java2s;
/*// w w w  .  j a  v a 2  s  . com
 *   This software is distributed under the terms of the FSF 
 *   Gnu Lesser General Public License (see lgpl.txt). 
 *
 *   This program is distributed WITHOUT ANY WARRANTY. See the
 *   GNU General Public License for more details.
 */

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

import java.util.TimeZone;

public class Main {
    public static Calendar getCalendar(String dateString, String format,
            TimeZone tz) throws Exception {
        if (dateString == null || format == null || dateString.equals("")
                || tz == null)
            return null;

        Calendar cal = null;
        try {
            Date date = getDate(dateString, format, tz);
            cal = Calendar.getInstance();
            cal.setTime(date);
        } catch (Exception ex) {
            throw new Exception("Error parsing date " + dateString
                    + " with this format " + format);
        }

        return cal;
    }

    public static Calendar getCalendar(String dateString, String format)
            throws Exception {
        if (dateString == null || format == null || dateString.equals(""))
            return null;

        Calendar cal = null;
        try {
            Date date = getDate(dateString, format);
            cal = Calendar.getInstance();
            cal.setTime(date);
        } catch (Exception ex) {
            throw new Exception("Error parsing date " + dateString
                    + " with this format " + format);
        }

        return cal;
    }

    public static Date getDate(String dateString, String format, TimeZone tz)
            throws Exception {
        if (dateString == null || format == null || dateString.equals("")
                || tz == null)
            return null;

        SimpleDateFormat formatter = new SimpleDateFormat(format);
        formatter.setTimeZone(tz);
        ParsePosition pos = new ParsePosition(0);
        return formatter.parse(dateString, pos);
    }

    public static Date getDate(String dateString, String format)
            throws Exception {
        return getDate(dateString, format, TimeZone.getDefault());
    }
}

Related

  1. createDateFormatsForLocaleAndTimeZone(Locale locale, TimeZone timeZone)
  2. formatISO8601TimeZone(TimeZone timeZone, boolean extended)
  3. formatOffset(TimeZone zone)
  4. formatTimeZoneOffset(TimeZone timeZone)
  5. getCalendar(String dateString, String dateTimeFormat, String timeZoneName)
  6. getDefaultDateFormatWithoutTimeZone( TimeZone zone)
  7. getDefaultDateFormatWithTimeZone()
  8. getFormattedTime(Date time, String formatTime, String timezone)
  9. getSimpleDateFormat(TimeZone timeZone, String pattern)