All Downloads are FREE. Search and download functionalities are using the official Maven repository.

tools.dynamia.commons.DateTimeUtils Maven / Gradle / Ivy

There is a newer version: 5.3.1
Show newest version
/*
 * Copyright (C) 2009 - 2019 Dynamia Soluciones IT S.A.S - NIT 900302344-1
 * Colombia - South America
 * All Rights Reserved.
 *
 * DynamiaTools is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License (LGPL v3) as
 * published by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * DynamiaTools 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with DynamiaTools.  If not, see .
 */
package tools.dynamia.commons;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;

/**
 * Utility class for common dates operations. Internally its use LocalDate, Instants and other Java 8 Date API
 *
 * @author Mario A. Serrano Leones
 */
public final class DateTimeUtils {

    /**
     * The Constant SHORT.
     */
    public static final int SHORT = DateFormat.SHORT;

    /**
     * The Constant MEDIUM.
     */
    public static final int MEDIUM = DateFormat.MEDIUM;

    /**
     * The Constant LONG.
     */
    public static final int LONG = DateFormat.LONG;

    /**
     * Return the last day of month
     *
     * @param month
     * @return last day
     */
    public static int getLastDayOfMonth(int month) {
        return getLastDayOfMonth(Month.of(month));
    }

    /**
     * Return the last day of month
     *
     * @param month
     * @return last day
     */
    public static int getLastDayOfMonth(Month month) {
        return month.length(LocalDate.now().isLeapYear());
    }

    /**
     * Instantiates a new date time utils.
     */
    private DateTimeUtils() {
    }

    /**
     * Checks if is future.
     *
     * @param date the date
     * @return true, if is future
     */
    public static boolean isFuture(Date date) {
        return date.after(new Date());
    }

    /**
     * Checks if is past.
     *
     * @param date the date
     * @return true, if is past
     */
    public static boolean isPast(Date date) {
        return date.before(new Date());
    }

    /**
     * Check if passed date has the same year, month and day of current system date
     *
     * @param date
     * @return if is today
     */
    public static boolean isToday(Date date) {
        Date today = new Date();

        return getYear(today) == getYear(date) && getMonth(today) == getMonth(date) && getDay(today) == getDay(date);
    }

    /**
     * is the same that DateTimeUtils.getYear(new Date());
     *
     * @return the current year
     */
    public static int getCurrentYear() {
        return getYear(new Date());
    }

    /**
     * Gets the current month.
     *
     * @return the current month
     */
    public static int getCurrentMonth() {
        return getMonth(new Date());
    }

    /**
     * Gets the current month name.
     *
     * @return the current month name
     */
    public static String getCurrentMonthName() {
        DateFormat df = new SimpleDateFormat("MMMM");
        return df.format(new Date());
    }

    /**
     * Gets the current day.
     *
     * @return the current day
     */
    public static int getCurrentDay() {
        return getDay(new Date());
    }

    /**
     * Millis between.
     *
     * @param date1 the date1
     * @param date2 the date2
     * @return the long
     */
    public static long millisBetween(Date date1, Date date2) {
        LocalDate localDate1 = toLocalDate(date1);
        LocalDate localDate2 = toLocalDate(date2);

        return localDate1.until(localDate2, ChronoUnit.MILLIS);
    }


    /**
     * Months between.
     *
     * @param date1 the date1
     * @param date2 the date2
     * @return the long
     */
    public static long yearsBetween(Date date1, Date date2) {
        LocalDate localDate1 = toLocalDate(date1);
        LocalDate localDate2 = toLocalDate(date2);

        return localDate1.until(localDate2, ChronoUnit.YEARS);
    }

    /**
     * Months between.
     *
     * @param date1 the date1
     * @param date2 the date2
     * @return the long
     */
    public static long monthsBetween(Date date1, Date date2) {
        LocalDate localDate1 = toLocalDate(date1);
        LocalDate localDate2 = toLocalDate(date2);

        return localDate1.until(localDate2, ChronoUnit.MONTHS);
    }


    /**
     * Days between.
     *
     * @param date1 the date1
     * @param date2 the date2
     * @return the long
     */
    public static long daysBetween(Date date1, Date date2) {
        return toInstant(date1).until(toInstant(date2), ChronoUnit.DAYS);
    }

    /**
     * Hours between.
     *
     * @param date1 the date1
     * @param date2 the date2
     * @return the long
     */
    public static long hoursBetween(Date date1, Date date2) {
        return toInstant(date1).until(toInstant(date2), ChronoUnit.HOURS);
    }

    /**
     * Minutes between.
     *
     * @param date1 the date1
     * @param date2 the date2
     * @return the long
     */
    public static long minutesBetween(Date date1, Date date2) {
        return toInstant(date1).until(toInstant(date2), ChronoUnit.MINUTES);
    }

    /**
     * Seconds between.
     *
     * @param date1 the date1
     * @param date2 the date2
     * @return the long
     */
    public static long secondsBetween(Date date1, Date date2) {
        return toInstant(date1).until(toInstant(date2), ChronoUnit.SECONDS);
    }

    /**
     * Creates the date.
     *
     * @param timestamp the timestamp
     * @return the date
     */
    public static Date createDate(long timestamp) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(timestamp);
        return calendar.getTime();
    }

    /**
     * Creates the date.
     *
     * @param dayOfMonth the day of month
     * @return the date
     */
    public static Date createDate(int dayOfMonth) {
        return toDate(LocalDate.now().withDayOfMonth(dayOfMonth));
    }

    /**
     * Creates the date.
     *
     * @param year  the year
     * @param month the month from 1 to 12
     * @param day   the day
     * @return the date
     */
    public static Date createDate(int year, int month, int day) {
        return toDate(LocalDate.of(year, month, day));
    }

    /**
     * Creates the date.
     *
     * @param year      the year
     * @param month     the month
     * @param day       the day
     * @param hourOfDay the hour of day
     * @param minute    the minute
     * @param second    the second
     * @return the date
     */
    public static Date createDate(int year, int month, int day, int hourOfDay, int minute, int second) {
        return toDate(LocalDateTime.of(year, month, day, hourOfDay, minute, second));
    }

    /**
     * Creates the date.
     *
     * @param year      the year
     * @param month     the month
     * @param day       the day
     * @param hourOfDay the hour of day
     * @param minute    the minute
     * @return the date
     */
    public static Date createDate(int year, int month, int day, int hourOfDay, int minute) {
        return toDate(LocalDateTime.of(year, month, day, hourOfDay, minute));
    }

    /**
     * Creates the date.
     *
     * @param dateText the date text
     * @return the date
     * @throws ParseException the parse exception
     */
    public static Date createDate(String dateText) throws ParseException {
        DateFormat df = DateFormat.getDateInstance();
        return df.parse(dateText);
    }

    /**
     * Creates the date.
     *
     * @param dateText the date text
     * @param pattern  the pattern
     * @return the date
     * @throws ParseException the parse exception
     */
    public static Date createDate(String dateText, String pattern) throws ParseException {
        DateFormat df = new SimpleDateFormat(pattern);
        return df.parse(dateText);
    }

    /**
     * Adds the days.
     * This method is immplemented using Java 7+ DateTime API
     *
     * @param date the date
     * @param days the days
     * @return the date
     */
    public static Date addDays(Date date, int days) {
        return toDate(toLocalDateTime(date).plusDays(days));

    }

    /**
     * Adds the months.
     * 

* This method is immplemented using Java 7+ DateTime API * * @param date the date * @param months the months * @return the date */ public static Date addMonths(Date date, int months) { return toDate(toLocalDateTime(date).plusMonths(months)); } /** * Adds the years. * * @param date the date * @param years the years * @return the date */ public static Date addYears(Date date, int years) { return toDate(toLocalDateTime(date).plusYears(years)); } /** * Adds weeks. * * @param date the date * @param weeks * @return the date */ public static Date addWeeks(Date date, int weeks) { return toDate(toLocalDateTime(date).plusWeeks(weeks)); } /** * Adds the hours. * * @param date the date * @param hours the hours * @return the date */ public static Date addHours(Date date, int hours) { return toDate(toLocalDateTime(date).plus(hours, ChronoUnit.HOURS)); } /** * Adds the minutes. * * @param date the date * @param minutes the minutes * @return the date */ public static Date addMinutes(Date date, int minutes) { return toDate(toLocalDateTime(date).plus(minutes, ChronoUnit.MINUTES)); } /** * Format. * * @param date the date * @param pattern the pattern * @return the string */ public static String format(Date date, String pattern) { DateFormat df = new SimpleDateFormat(pattern); return df.format(date); } /** * Format date. * * @param date the date * @param style the style * @return the string */ public static String formatDate(Date date, int style) { DateFormat df = DateFormat.getDateInstance(style); return df.format(date); } /** * Format date. * * @param date the date * @return the string */ public static String formatDate(Date date) { return formatDate(date, DateFormat.MEDIUM); } /** * Format time. * * @param date the date * @param style the style * @return the string */ public static String formatTime(Date date, int style) { DateFormat df = DateFormat.getTimeInstance(style); return df.format(date); } /** * Format time. * * @param date the date * @return the string */ public static String formatTime(Date date) { return formatTime(date, DateFormat.MEDIUM); } /** * return the month value in the date, where january is 1 and december is * 12. * * @param date the date * @return the month */ public static int getMonth(Date date) { return toLocalDate(date).getMonth().getValue(); } /** * Gets the day. * * @param date the date * @return the day */ public static int getDay(Date date) { return toLocalDate(date).getDayOfMonth(); } /** * Gets the year. * * @param date the date * @return the year */ public static int getYear(Date date) { return toLocalDate(date).getYear(); } /** * Create a new instance of Date and add one day * * @return date */ public static Date tomorrow() { return addDays(now(), 1); } /** * Just create a new instance of date * * @return date */ public static Date now() { return new Date(); } /** * Create a new date and substract one day * * @return date */ public static Date yesterday() { return addDays(now(), -1); } /** * Create a tomorrow date and add one day * * @return */ public static Date dayAfterTomorrow() { return addDays(tomorrow(), 1); } /** * Convert a Date to LocalDate * * @param input * @return */ public static LocalDate toLocalDate(Date input) { if (input instanceof java.sql.Date) { return ((java.sql.Date) input).toLocalDate(); } else { Instant instant = input.toInstant(); ZonedDateTime zdt = instant.atZone(ZoneId.systemDefault()); return zdt.toLocalDate(); } } public static LocalDateTime toLocalDateTime(Date input) { Instant instant = null; if (input instanceof java.sql.Date) { instant = Instant.ofEpochMilli(input.getTime()); } else { instant = input.toInstant(); } ZonedDateTime zdt = instant.atZone(ZoneId.systemDefault()); return zdt.toLocalDateTime(); } public static Instant toInstant(Date input) { if (input instanceof java.sql.Date) { LocalDate localDate = ((java.sql.Date) input).toLocalDate(); Instant instant = null; try { instant = Instant.from(localDate); } catch (DateTimeException e) { instant = localDate.atStartOfDay(ZoneId.systemDefault()).toInstant(); } return instant; } else { return input.toInstant(); } } /** * Convert a LocalDate to Date * * @param localDate * @return */ public static Date toDate(LocalDate localDate) { return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant()); } public static Date toDate(LocalDateTime localDateTime) { return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()); } public static Date getEndOfDay(Date date) { return Date.from(toLocalDate(date).atTime(23, 59, 59).atZone(ZoneId.systemDefault()).toInstant()); } public static Date getStartOfDay(Date date) { return Date.from(toLocalDate(date).atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()); } /** * Return a date range from first day of the current month to last day of the current month. Like 1 to 31 of December * * @return DateRange */ public static DateRange getCurrentMonthRange() { Date startDate = createDate(getCurrentYear(), getCurrentMonth(), 1); Date endDate = createDate(getCurrentYear(), getCurrentMonth(), getLastDayOfMonth(getCurrentMonth())); return new DateRange(startDate, endDate); } /** * Parse string to {@link Date} using pattern * * @param source * @param pattern * @return date */ public static Date parse(String source, String pattern) throws ParseException { DateFormat df = new SimpleDateFormat(pattern); return df.parse(source); } /** * The same as now() * * @return */ public static Date today() { return now(); } /** * Create a new Date with specific time * * @param hourOfDay * @param minutes * @param seconds * @return */ public static Date today(int hourOfDay, int minutes, int seconds) { Calendar calendar = Calendar.getInstance(); calendar.setTime(now()); calendar.set(Calendar.HOUR_OF_DAY, hourOfDay); calendar.set(Calendar.MINUTE, minutes); calendar.set(Calendar.SECOND, seconds); return calendar.getTime(); } /** * Format start date and end date from {@link DateRange} * @param dateRange * @return */ public static String formatDate(DateRange dateRange) { return formatDate(dateRange.getStartDate()) + " - " + formatDate(dateRange.getEndDate()); } /** * Format start date and end date from {@link DateRange} using pattern * @param dateRange * @param pattern * @return */ public static String format(DateRange dateRange, String pattern) { return format(dateRange.getStartDate(), pattern) + " - " + format(dateRange.getEndDate(), pattern); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy