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

com.jelastic.api.core.utils.DateUtils Maven / Gradle / Ivy

The newest version!
/*Server class MD5: 48239d75173075499ee5596ef87387fb*/
package com.jelastic.api.core.utils;

import com.jelastic.api.utils.DateRange;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;

/**
 * @name Jelastic API Client
 * @version 8.11.2
 * @copyright Jelastic, Inc.
 */
public class DateUtils {

    public static final String SQL_DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

    public static final SimpleDateFormat SQL_DATE = new SimpleDateFormat("yyyy-MM-dd");

    public static final SimpleDateFormat SQL_DATETIME = new SimpleDateFormat(SQL_DATETIME_PATTERN);

    public static final SimpleDateFormat SQL_TIME = new SimpleDateFormat("HH:mm:ss");

    public static final SimpleDateFormat SQL_YEAR = new SimpleDateFormat("yyyy");

    public static final TimeZone GMT_TIME_ZONE = TimeZone.getTimeZone("GMT");

    public static final long ONE_HOUR_SEC = 60l * 60l;

    public static final long ONE_HOUR_MSEC = ONE_HOUR_SEC * 1000l;

    public static final long ONE_HOUR_NANOSEC = ONE_HOUR_MSEC * 1000000L;

    public static final int ONE_HOUR_MIN = 60;

    public static final long HALF_HOUR_SEC = ONE_HOUR_SEC / 2l;

    public static final long ONE_DAY_SEC = ONE_HOUR_SEC * 24l;

    public static final long ONE_DAY_MSEC = ONE_DAY_SEC * 1000l;

    public static final long ONE_DAY_MIN = ONE_DAY_SEC / 60l;

    public static final DateTimeFormatter SQL_LOCAL_DATETIME = DateTimeFormatter.ofPattern(SQL_DATETIME_PATTERN);

    public static final int INT_ONE_HOUR_SEC = 60 * 60;

    public static final int INT_ONE_HOUR_MSEC = INT_ONE_HOUR_SEC * 1000;

    public static final int INT_ONE_HOUR_NANOSEC = INT_ONE_HOUR_MSEC * 1000000;

    public static final int INT_HALF_HOUR_SEC = INT_ONE_HOUR_SEC / 2;

    public static final int INT_ONE_DAY_SEC = INT_ONE_HOUR_SEC * 24;

    public static final int INT_ONE_WEEK_SEC = INT_ONE_DAY_SEC * 7;

    public static final int INT_ONE_MONTH_SEC = INT_ONE_DAY_SEC * 30;

    public static final int INT_ONE_DAY_MSEC = INT_ONE_DAY_SEC * 1000;

    public static final int INT_ONE_DAY_MIN = INT_ONE_DAY_SEC / 60;

    private static final int MIN_VALID_YEAR = 1969;

    static {
        SQL_DATE.setTimeZone(GMT_TIME_ZONE);
        SQL_DATETIME.setTimeZone(GMT_TIME_ZONE);
        SQL_TIME.setTimeZone(GMT_TIME_ZONE);
        SQL_YEAR.setTimeZone(GMT_TIME_ZONE);
    }

    public static final String ISO_8601_TZ = "yyyy-MM-dd'T'HH:mm:ssZ";

    public static final String ISO_8601_Tz = "yyyy-MM-dd'T'HH:mm:ssz";

    public static final String ISO_8601_SECOND = "yyyy-MM-dd'T'HH:mm:ss";

    public static final String ISO_8601_MINUTE = "yyyy-MM-dd'T'HH:mm";

    public static final String ISO_8601_HOUR = "yyyy-MM-dd'T'HH";

    public static final String ISO_8601_SECOND_TZ = "yyyy-MM-dd'T'HH:mm:ssZ";

    public static final String ISO_8601_MINUTE_TZ = "yyyy-MM-dd'T'HH:mmZ";

    public static final String ISO_8601_HOUR_TZ = "yyyy-MM-dd'T'HHZ";

    public static final String ISO_8601_SECOND_Tz = "yyyy-MM-dd'T'HH:mm:ssz";

    public static final String ISO_8601_MINUTE_Tz = "yyyy-MM-dd'T'HH:mmz";

    public static final String ISO_8601_HOUR_Tz = "yyyy-MM-dd'T'HHz";

    public static final String ISO_8601_DATE = "yyyy-MM-dd";

    public static final String[] ISO_FORMATS = { ISO_8601_TZ, ISO_8601_Tz, ISO_8601_SECOND, ISO_8601_SECOND_TZ, ISO_8601_SECOND_Tz, ISO_8601_MINUTE, ISO_8601_MINUTE_TZ, ISO_8601_MINUTE_Tz, ISO_8601_HOUR, ISO_8601_HOUR_TZ, ISO_8601_HOUR_Tz, ISO_8601_DATE };

    public static final String COLON_MATCH = "([0-9]+) *([-+][0-9]+):([0-9]+)$";

    public static final String COLOR_REP = "$1$2$3";

    public static final String T_SPACE_MATCH = "([0-9]+)-([0-9]+)-([0-9]+) +([0-9]+)";

    public static final String PAD_ZERO_TZ_MATCH = "(:[0-9]+) *([-+][0-9]{1,2})$";

    public static final String PAD_ZERO_TZ_MATCH2 = "(T[0-9]+) *([-+][0-9]{1,2})$";

    public static final String T_SPACE_REP = "$1-$2-$3T$4";

    public static final String PAD_ZERO_TZ_REP = "$1$200";

    public static final String PAD_ZERO_TZ_REP2 = "$1$200";

    public static Date commonParse(String dateTime, String[] formats, TimeZone tz) throws ParseException {
        for (String format : formats) {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            sdf.setTimeZone(tz);
            ParsePosition pos = new ParsePosition(0);
            Date result = sdf.parse(dateTime, pos);
            if (pos.getIndex() != dateTime.length() || result == null) {
                continue;
            }
            return result;
        }
        throw new ParseException(dateTime, 0);
    }

    public static String normalizeDateTime(String dateTime) {
        dateTime = dateTime.replaceAll(COLON_MATCH, COLOR_REP);
        dateTime = dateTime.replaceAll(T_SPACE_MATCH, T_SPACE_REP);
        dateTime = dateTime.replaceAll(PAD_ZERO_TZ_MATCH, PAD_ZERO_TZ_REP);
        dateTime = dateTime.replaceAll(PAD_ZERO_TZ_MATCH2, PAD_ZERO_TZ_REP2);
        return dateTime;
    }

    public static Date parseIso(String dateTime, TimeZone tz) throws ParseException {
        dateTime = normalizeDateTime(dateTime);
        return commonParse(dateTime, ISO_FORMATS, tz);
    }

    public static Date parseIso(String dateTime) throws ParseException {
        return parseIso(dateTime, GMT_TIME_ZONE);
    }

    public static String formatIsoTz(Date d) {
        SimpleDateFormat sdf = new SimpleDateFormat(ISO_8601_TZ);
        sdf.setTimeZone(GMT_TIME_ZONE);
        return sdf.format(d);
    }

    public static String formatIsoTz(Date date, TimeZone timeZone) {
        SimpleDateFormat sdf = new SimpleDateFormat(ISO_8601_TZ);
        sdf.setTimeZone(timeZone);
        return sdf.format(date);
    }

    public static synchronized final Date parseSqlDate(String date) throws ParseException {
        return SQL_DATE.parse(date);
    }

    public static synchronized final String formatSqlDate(Date date) {
        return SQL_DATE.format(date);
    }

    public static synchronized final Date parseSqlDateTime(String date) throws ParseException {
        Date parsedDate = SQL_DATETIME.parse(date);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(parsedDate);
        if (calendar.get(Calendar.YEAR) < MIN_VALID_YEAR) {
            throw new ParseException("Illegal year. Please use year greater than 1969.", -1);
        }
        return parsedDate;
    }

    public static synchronized final String formatSqlDateTime(Date date) {
        return SQL_DATETIME.format(date);
    }

    public static synchronized final Date parseSqlTime(String date) throws ParseException {
        return SQL_TIME.parse(date);
    }

    public static synchronized final String formatSqlTime(Date date) {
        return SQL_TIME.format(date);
    }

    public static synchronized final Date parseSqlYear(String date) throws ParseException {
        return SQL_YEAR.parse(date);
    }

    public static synchronized final String formatSqlYear(Date date) {
        return SQL_YEAR.format(date);
    }

    public static synchronized final int getDays(Date end, Date start) {
        return (int) ((end.getTime() - start.getTime()) / ONE_DAY_MSEC);
    }

    public static synchronized final int getSeconds(Date end, Date start) {
        return (int) ((end.getTime() - start.getTime()) / 1000);
    }

    public static final synchronized int getHours(Date end, Date start) {
        return getMinutes(end, start) / 60;
    }

    public static synchronized final int getMinutes(Date end, Date start) {
        return (int) (getSeconds(end, start) / 60);
    }

    public static synchronized final Date getDiffHours(Date date, int diff) {
        return new Date(date.getTime() - diff * ONE_HOUR_MSEC);
    }

    public static synchronized final Date getDiffSeconds(Date date, int diff) {
        return new Date(date.getTime() - diff * 1000);
    }

    public static final String getCopyrightYears() {
        return "2008-" + Calendar.getInstance().get(Calendar.YEAR);
    }

    public static Date getMonthStart(Date date, int monthShift) {
        Calendar calendar = GregorianCalendar.getInstance(TimeZone.getTimeZone("UTC"));
        calendar.setTime(date);
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + monthShift);
        calendar.set(Calendar.DATE, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    public static Date getBillableDate(Date date, Date chargeDate) {
        Calendar now = GregorianCalendar.getInstance(TimeZone.getTimeZone("UTC"));
        now.setTime(chargeDate);
        Calendar calendar = GregorianCalendar.getInstance(TimeZone.getTimeZone("UTC"));
        calendar.setTime(date);
        calendar.set(Calendar.MONTH, now.get(Calendar.MONTH));
        calendar.set(Calendar.YEAR, now.get(Calendar.YEAR));
        if (calendar.after(now)) {
            calendar.add(Calendar.MONTH, -1);
        }
        return calendar.getTime();
    }

    public static Date getPrevMonthStart(Date date) {
        return getMonthStart(date, -1);
    }

    public static Date getMonthEnd(Date date, int monthShift) {
        Calendar calendar = GregorianCalendar.getInstance(TimeZone.getTimeZone("UTC"));
        calendar.setTime(date);
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + monthShift);
        calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTime();
    }

    public static Date getPrevMonthEnd(Date date) {
        return getMonthEnd(date, -1);
    }

    public static Date truncateMinutes(Date date) {
        return new Date(date.getTime() / 1000 / 3600 * 1000 * 3600);
    }

    public static Date truncateHoursAndMinutes(Date date) {
        return new Date(date.getTime() / 1000 / 3600 * 1000);
    }

    public class TimeIgnoringComparator implements Comparator {

        public int compare(Calendar c1, Calendar c2) {
            if (c1.get(Calendar.YEAR) != c2.get(Calendar.YEAR))
                return c1.get(Calendar.YEAR) - c2.get(Calendar.YEAR);
            if (c1.get(Calendar.MONTH) != c2.get(Calendar.MONTH))
                return c1.get(Calendar.MONTH) - c2.get(Calendar.MONTH);
            return c1.get(Calendar.DAY_OF_MONTH) - c2.get(Calendar.DAY_OF_MONTH);
        }
    }

    public static List splitDateRanges(Date start, Date end) {
        List ranges = new ArrayList<>();
        Date shiftedStart = DateUtils.truncateMinutes(start);
        long startTime = start.getTime();
        long endTime = end.getTime();
        long intervalEnd = startTime + DateUtils.INT_ONE_HOUR_MSEC;
        ranges.add(new DateRange(shiftedStart, new Date(intervalEnd - 1000)));
        while (intervalEnd < endTime) {
            long intervalStart = intervalEnd;
            intervalEnd = intervalEnd + DateUtils.INT_ONE_HOUR_MSEC;
            ranges.add(new DateRange(new Date(intervalStart), new Date(intervalEnd - 1000)));
        }
        return ranges;
    }

    public static synchronized final String formatSqlDateTime(LocalDateTime date) {
        return date.format(SQL_LOCAL_DATETIME);
    }

    public static Date convertToDate(LocalDate dateToConvert) {
        return java.util.Date.from(dateToConvert.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
    }

    public static Date addDays(Date date, int days) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, days);
        return cal.getTime();
    }

    public static DateRange getCurrentHour() {
        LocalDateTime start = LocalDateTime.now().truncatedTo(ChronoUnit.HOURS);
        LocalDateTime end = start.plusHours(1).truncatedTo(ChronoUnit.HOURS).minusSeconds(1);
        Date endDate = Date.from(end.atZone(ZoneId.systemDefault()).toInstant());
        Date startDate = Date.from(start.atZone(ZoneId.systemDefault()).toInstant());
        return new DateRange(startDate, endDate);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy