Java Day of Month getFirstDayOfMonth(java.util.Date date)

Here you can find the source of getFirstDayOfMonth(java.util.Date date)

Description

getFirstDayOfMonth: returns the first day of the given dates month

License

Apache License

Parameter

Parameter Description
date any day

Return

Date with no time {talendTypes} Date {Category} TimestampUtil {param} Date date: any day. {example} getFirstDayOfMonth(date).

Declaration

public static java.util.Date getFirstDayOfMonth(java.util.Date date) 

Method Source Code

//package com.java2s;
/**/* w ww.j a va  2s.  co  m*/
 * Copyright 2015 Jan Lolling jan.lolling@gmail.com
 * 
 * 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.util.Calendar;

import java.util.Locale;

public class Main {
    /**
     * getFirstDayOfMonth: returns the first day of the given dates month
     * @param date any day
     * @return Date with no time
     * 
     * {talendTypes} Date
     * 
     * {Category} TimestampUtil
     * 
     * {param} Date date: any day.
     * 
     * {example} getFirstDayOfMonth(date).
     */
    public static java.util.Date getFirstDayOfMonth(java.util.Date date) {
        date = truncateToDay(date);
        if (date != null) {
            java.util.Calendar c = java.util.Calendar
                    .getInstance(Locale.GERMANY);
            c.setTime(date);
            c.set(Calendar.DAY_OF_MONTH, 1);
            return c.getTime();
        } else {
            return null;
        }
    }

    /**
     * truncateToDay: returns the start of the given day.
     * 
     * {Category} TimestampUtil
     * 
     * {talendTypes} Date
     * 
     * {param} Date() day: any day.
     * 
     * {example} truncateToDay(TalendDate.getCurentDate()).
     */
    public static java.util.Date truncateToDay(java.util.Date timestamp) {
        if (timestamp == null)
            return null;
        java.util.Calendar c = java.util.Calendar.getInstance();
        c.setTime(timestamp);
        // cut time
        c.set(java.util.Calendar.MINUTE, 0);
        c.set(java.util.Calendar.SECOND, 0);
        c.set(java.util.Calendar.MILLISECOND, 0);
        c.set(java.util.Calendar.HOUR_OF_DAY, 0);
        return c.getTime();
    }
}

Related

  1. getDayOfMonth(Date date)
  2. getDayOfMonth(Date dt)
  3. getDayOfMonth(final Date date)
  4. getFirstDayOfMonth(Date date)
  5. getFirstDayOfMonth(Date date)
  6. getFirstMiddleDayOfMonth(Date date)
  7. getFistDayOfMonth(Date date)
  8. getFromDate(boolean invoiceSearchFromFirstDayOfMonth)
  9. getLastDateOfCurMonth()