Java Year Month getBeginEndDayOfMonth(String yearMonthString)

Here you can find the source of getBeginEndDayOfMonth(String yearMonthString)

Description

Get the begin/end day of a month

License

Open Source License

Parameter

Parameter Description
yearMonthString a parameter

Return

an Date array with begin date at 0 and end date at 1 or null if any error occured

Declaration

public static Date[] getBeginEndDayOfMonth(String yearMonthString) 

Method Source Code

//package com.java2s;
/**// w  ww.  j av a 2  s  . c om
 * Project: CarTracServer
 * 
 * File Created at Apr 6, 2013
 * $id$
 * 
 * Copyright 2013, Shawn Chain (shawn.chain@gmail.com).
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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

public class Main {
    /**
     * Get the begin/end day of a month
     * @param yearMonthString
     * @return an Date array with begin date at 0 and end date at 1 or null if any error occured
     */
    public static Date[] getBeginEndDayOfMonth(String yearMonthString) {
        try {
            Date begin, end;
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            begin = sdf.parse(yearMonthString);

            Calendar cal = getCalendar();
            cal.setTime(begin);
            cal.add(Calendar.MONTH, 1);
            cal.add(Calendar.MILLISECOND, -1);
            end = cal.getTime();
            return new Date[] { begin, end };
        } catch (Exception e) {
            // NOOP
        }

        return null;
    }

    public static Calendar getCalendar() {
        return Calendar.getInstance(TimeZone.getTimeZone("GMT+8"));
    }
}

Related

  1. date(int year, int month, int day)
  2. Date2Long(int year, int month, int date)
  3. encodeDateTime(int year, int month, int day, int hour, int minutes, int seconds)
  4. getAddedDate(Date date, int addYear, int addMonth, int addDay, int addHour, int addMinute, int addSecond)
  5. getAllDate(String year_month)
  6. getCurrentYearMonthDay()
  7. getCurYearMonth()
  8. getDate(int year, int month, int day)
  9. getDate(int year, int month, int day, int hour, int minute)