avatar
童琦杰
Apr 2, 2019Technology

JAVA - 时间类处理

DateTime.java
java
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateTime implements Comparable<DateTime> {
    public static DateTime now() {
        return new DateTime(new Date());
    }
    public static DateTime today() {
        return DateTime.now().getDate();
    }
    public static DateTime minValue() {
        return new DateTime();
    }
    public static DateTime maxValue() { return new DateTime(9999, 12, 31, 23, 59, 59); }
    public static DateTime parse(String dateString, String format) throws ParseException {
        return new DateTime(new SimpleDateFormat(format, Locale.CHINESE).parse(dateString));
    }
    public static DateTime parse(String dateString, String format, DateTime defaultValue) {
        try {
            return new DateTime(new SimpleDateFormat(format, Locale.CHINESE).parse(dateString));
        }
        catch (ParseException ignored) { }
        return defaultValue;
    }
    private Date date;
    private DateTime(Date date) {
        this.date = date;
    }
    public DateTime() {
        try {
            date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINESE).parse("0001-01-01 00:00:00");
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
    public DateTime(long timestamp) {
        date = new Date(timestamp);
    }
    public DateTime(Calendar calendar) {
        date = calendar.getTime();
    }
    public DateTime(int year, int month, int day) {
        try {
            date = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINESE).parse(String.format("%04d-%02d-%02d", year, month, day));
        }
        catch (ParseException e) {
            e.printStackTrace();
        }
    }
    public DateTime(int year, int month, int day, int hour, int minute, int second) {
        try {
            date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINESE).parse(String.format("%04d-%02d-%02d %02d:%02d:%02d", year, month, day, hour, minute, second));
        }
        catch (ParseException e) {
            e.printStackTrace();
        }
    }
    public DateTime getDate() {
        return new DateTime(getYear(), getMonth(), getDay());
    }
    public int getYear() {
        return Integer.parseInt(new SimpleDateFormat("yyyy", Locale.CHINESE).format(date));
    }
    public int getMonth() {
        return Integer.parseInt(new SimpleDateFormat("MM", Locale.CHINESE).format(date));
    }
    public int getDay() {
        return Integer.parseInt(new SimpleDateFormat("dd", Locale.CHINESE).format(date));
    }
    public int getHour() {
        return Integer.parseInt(new SimpleDateFormat("HH", Locale.CHINESE).format(date));
    }
    public int getMinute() {
        return Integer.parseInt(new SimpleDateFormat("mm", Locale.CHINESE).format(date));
    }
    public int getSecond() {
        return Integer.parseInt(new SimpleDateFormat("ss", Locale.CHINESE).format(date));
    }
    public int getMillisecond() {
        return Integer.parseInt(new SimpleDateFormat("SSS", Locale.CHINESE).format(date));
    }
    public long getTimestamp() {
        return date.getTime();
    }
    public int getDayOfWeek() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.DAY_OF_WEEK) - 1;
    }
    public String toString(String format) {
        return new SimpleDateFormat(format, Locale.CHINESE).format(date);
    }
    @Override
    public String toString() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.CHINESE).format(date);
    }
    public DateTime addYears(int year) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.YEAR, year);
        return new DateTime(cal.getTime());
    }
    public DateTime addMonths(int months) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, months);
        return new DateTime(cal.getTime());
    }
    public DateTime addDays(int days) {
        return new DateTime(new Date(date.getTime() + ((long)days) * 24 * 60 * 60 * 1000));
    }
    public DateTime addHours(int hours) {
        return new DateTime(new Date(date.getTime() + ((long)hours) * 60 * 60 * 1000));
    }
    public DateTime addMinutes(int minutes) {
        return new DateTime(new Date(date.getTime() + ((long)minutes) * 60 * 1000));
    }
    public DateTime addSeconds(int seconds) {
        return new DateTime(new Date(date.getTime() + ((long)seconds) * 1000));
    }
    public DateTime addMilliseconds(int milliseconds) {
        return new DateTime(new Date(date.getTime() + ((long)milliseconds)));
    }
    public Calendar toCalendar() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal;
    }
    public Timestamp toTimestamp() {
        return new Timestamp(date.getTime());
    }
    @Override
    public boolean equals(Object obj) {
        if (obj instanceof DateTime) {
            return ((DateTime) obj).date.getTime() == date.getTime();
        }
        return false;
    }
    @Override
    public int compareTo(DateTime dateTime) {
        return Long.compare(date.getTime(), dateTime.date.getTime());
    }
    public DateTime plus(TimeSpan timeSpan) {
        return new DateTime(new Date(date.getTime() + timeSpan.getMilliseconds()));
    }
    public TimeSpan minus(DateTime dateTime) {
        return new TimeSpan(date.getTime() - dateTime.date.getTime());
    }
}
TimeSpan.java
java
public class TimeSpan implements Comparable<TimeSpan> {
    public static TimeSpan fromMilliseconds(long milliseconds) {
        return new TimeSpan(milliseconds);
    }

    public static TimeSpan fromSeconds(int seconds) {
        return new TimeSpan(seconds * 1000);
    }

    public static TimeSpan fromMinutes(int minutes) {
        return new TimeSpan(minutes * 60 * 1000);
    }

    public static TimeSpan fromHours(int hours) {
        return new TimeSpan(hours * 60 * 60 * 1000);
    }

    public static TimeSpan fromDays(int days) {
        return new TimeSpan(days * 24 * 60 * 60 * 1000);
    }

    private long _milliseconds;

    public TimeSpan(long milliseconds) {
        _milliseconds = milliseconds;
    }

    public int getDays() {
        return (int) (_milliseconds / (24 * 60 * 60 * 1000));
    }

    public int getHours() {
        return (int) ((_milliseconds / (60 * 60 * 1000)) % 24) ;
    }

    public int getMinutes() {
        return (int) ((_milliseconds / (60 * 1000)) % 60);
    }

    public int getSeconds() {
        return (int) ((_milliseconds / 1000) % 60);
    }

    public int getMilliseconds() {
        return (int)(_milliseconds % 1000);
    }

    public double getTotalDays() {
        return _milliseconds / (double) (24 * 60 * 60 * 1000);
    }

    public double getTotalHours() {
        return _milliseconds / (double) (60 * 60 * 1000);
    }

    public double getTotalMinutes() {
        return _milliseconds / (double) (60 * 1000);
    }

    public double getTotalSeconds() {
        return _milliseconds / (double) (1000);
    }

    public double getTotalMilliseconds() {
        return _milliseconds;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof TimeSpan) {
            return ((TimeSpan)obj)._milliseconds == _milliseconds;
        }
        return false;
    }
    @Override
    public int compareTo(TimeSpan timeSpan) {
        return Long.compare(_milliseconds, timeSpan._milliseconds);
    }
}
© 2015-2022 tongqijie.com 版权所有沪ICP备17000682号