Here you can find the source of getLastDayOfMonth(java.util.Date date)
Parameter | Description |
---|---|
date | any day |
public static java.util.Date getLastDayOfMonth(java.util.Date date)
//package com.java2s; /**//from ww w. ja v a 2 s .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 { /** * getLastDayOfMonth: returns the last 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} getLastDayOfMonth(date). */ public static java.util.Date getLastDayOfMonth(java.util.Date date) { date = addMonths(date, 1); return addDays(getFirstDayOfMonth(date), -1); } /** * addMonths: returns date + given months * @param today any day * @param monthsToAdd difference in months * @return Date with UTC time * * {talendTypes} Date * * {Category} TimestampUtil * * {param} date today: any day. * * {param} int month: months to be added. (-1 for yesterday) * * {example} addDays(today, monthsToAdd). */ public static java.util.Date addMonths(java.util.Date today, int monthsToAdd) { if (today != null) { java.util.Calendar c = java.util.Calendar.getInstance(); c.setTime(today); c.add(Calendar.MONTH, monthsToAdd); return c.getTime(); } else { return null; } } /** * addDays: returns date of yesterday * @param today any day * @param daysToAdd difference in days * @return Date with UTC time * * {talendTypes} Date * * {Category} TimestampUtil * * {param} date today: any day. * * {param} long day: days to be added. (-1 for yesterday) * * {example} addDays(today, daysToAdd). */ public static java.util.Date addDays(java.util.Date today, long daysToAdd) { if (today != null) { return new java.util.Date(today.getTime() + (1000 * 60 * 60 * 24 * daysToAdd)); } else { return null; } } /** * addDays: returns the integer as date * * {talendTypes} String * * {Category} TimestampUtil * * {param} Integer(dateAsInt) dateAsInt : * {param} Integer(daysToAdd) daysToAdd : * * {example} addDays(dateAsInt, daysToAdd) # 20150403 */ public static Integer addDays(Integer dateAsInt, Integer daysToAdd) { if (dateAsInt != null) { java.util.Calendar c = java.util.Calendar.getInstance(); // 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); int year = dateAsInt / 10000; int monthDay = (dateAsInt - (year * 10000)); int month = monthDay / 100; int day = monthDay % 100; c.set(Calendar.YEAR, year); c.set(Calendar.MONTH, month - 1); c.set(Calendar.DAY_OF_MONTH, day); c.add(Calendar.DAY_OF_YEAR, daysToAdd); return getDateAsInt(c.getTime()); } else { return null; } } /** * 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; } } /** * getDateAsId: returns the current day as integer * * {talendTypes} String * * {Category} TimestampUtil * * {param} Date(date) date : date to format * * {example} getDateAsInt(date) # 20150403 */ public static Integer getDateAsInt(java.util.Date date) { if (date != null) { java.util.Calendar c = java.util.Calendar.getInstance(); c.setTime(date); // 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); int id = c.get(Calendar.YEAR) * 10000; id = id + (c.get(Calendar.MONTH) + 1) * 100; id = id + c.get(Calendar.DAY_OF_MONTH); return id; } 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(); } }