Android Calendar String Parse parseCalendar(int year, int month, int day, int hour, int minute, int second, int millisecond, TimeZone zone)

Here you can find the source of parseCalendar(int year, int month, int day, int hour, int minute, int second, int millisecond, TimeZone zone)

Description

Parses a date assuming all arguments are based on a Gregorian calendar.

License

Open Source License

Parameter

Parameter Description
year year value
month month of the year (first month is 1)
day day of the month (first day is 1)
hour hour of the day for the 24-hour clock
minute minute value within the hour
second second value within the minute
millisecond milliseconds value within the second
zone the given time zone

Return

a Gregorian calendar

Declaration

public static GregorianCalendar parseCalendar(int year, int month,
        int day, int hour, int minute, int second, int millisecond,
        TimeZone zone) 

Method Source Code

//package com.java2s;
/*/*from  w  ww  .jav a2 s.co m*/
 * Copyright (C) 2011-2012 Inaki Ortiz de Landaluce Saiz
 * 
 * This program is free software: you can redistribute it 
 * and/or modify it under the terms of the GNU General Public License 
 * as published by the Free Software Foundation, either 
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public 
 * License along with this program. If not, see 
 * <http://www.gnu.org/licenses/>
 */

import java.util.Calendar;

import java.util.GregorianCalendar;
import java.util.TimeZone;

public class Main {
    /**
     * Parses a date assuming all arguments are based on a Gregorian or Julian
     * calendar.
     * 
     * @param year
     *            year value
     * @param month
     *            month of the year (first month is 1)
     * @param day
     *            day of the month including fraction of hours, minutes, seconds
     *            and milliseconds
     * @param zone
     *            the given time zone
     * @return a calendar
     */
    public static GregorianCalendar parseCalendar(int year, int month,
            double day, TimeZone zone) {
        // calculate hour, minutes, seconds and milliseconds
        double hour = (day % 1) * 24; // decimal part of a day times 24
        double minute = (hour % 1) * 60; // decimal part of an hour times 60
        double second = (minute % 1) * 60; // decimal part of a minute times 60
        double millisecond = (second % 1) * 1000;

        return parseCalendar(year, month, (int) day, (int) hour,
                (int) minute, (int) second, (int) Math.round(millisecond),
                zone);
    }

    /**
     * Parses a date assuming all arguments are based on a Gregorian calendar.
     * 
     * @param year
     *            year value
     * @param month
     *            month of the year (first month is 1)
     * @param day
     *            day of the month (first day is 1)
     * @param hour
     *            hour of the day for the 24-hour clock
     * @param minute
     *            minute value within the hour
     * @param second
     *            second value within the minute
     * @param millisecond
     *            milliseconds value within the second
     * @param zone
     *            the given time zone
     * @return a Gregorian calendar
     */
    public static GregorianCalendar parseCalendar(int year, int month,
            int day, int hour, int minute, int second, int millisecond,
            TimeZone zone) {
        GregorianCalendar calendar = new GregorianCalendar(zone);

        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DAY_OF_MONTH, (int) day);
        calendar.set(Calendar.HOUR_OF_DAY, (int) hour);
        calendar.set(Calendar.MINUTE, (int) minute);
        calendar.set(Calendar.SECOND, (int) second);
        calendar.set(Calendar.MILLISECOND, (int) Math.round(millisecond));

        return calendar;
    }
}

Related

  1. format(String time)
  2. fromString(String dateString, String format)
  3. parseCalendar(int year, int month, double day, TimeZone zone)
  4. parseDA(Calendar c, String s, int off, int len)
  5. parseTM(Calendar c, String s, int off, int len)
  6. getAsCalendar(String dateString)
  7. getCalendarFromString(final String date)