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

com.slimgears.util.stream.DateUtils Maven / Gradle / Ivy

There is a newer version: 0.7.58
Show newest version
package com.slimgears.util.stream;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.Date;
import java.util.Objects;

@SuppressWarnings("WeakerAccess")
public class DateUtils {
    private final static ThreadLocal LONG_DATE_TIME_FORMAT = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"));
    private final static ThreadLocal COMPACT_DATE_FORMAT = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyyMMdd"));

    public static Date round(Date time, Duration period) {
        long rounded = (time.getTime() / period.toMillis()) * period.toMillis();
        return new Date(rounded);
    }

    public static boolean equalsRounded(Date first, Date second, Duration duration) {
        return Objects.equals(round(first, duration), round(second, duration));
    }

    public static Date add(Date date, Duration duration) {
        return new Date(date.getTime() + duration.toMillis());
    }

    public static Date sub(Date date, Duration duration) {
        return new Date(date.getTime() - duration.toMillis());
    }

    public static Duration times(Duration duration, double times) {
        return Duration.ofMillis(Math.round((double)duration.toMillis() * times));
    }

    public static Date parseCompactDate(String dateString) {
        return parseDate(COMPACT_DATE_FORMAT, dateString);
    }

    public static Date parseLongDateTime(String dateString) {
        return parseDate(LONG_DATE_TIME_FORMAT, dateString);
    }

    private static Date parseDate(ThreadLocal format, String dateString) {
        try {
            return format.get().parse(dateString);
        } catch (ParseException e) {
            return null;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy