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

net.lulihu.dateTime.DateTime Maven / Gradle / Ivy

package net.lulihu.dateTime;

import net.lulihu.ObjectKit.StrKit;

import java.util.Date;

/**
 * 封装java.util.Date
 */
public class DateTime extends Date {

    /**
     * 转换JDK date为 DateTime
     *
     * @param date JDK Date
     * @return DateTime
     */
    public static DateTime parse(Date date) {
        return new DateTime(date);
    }

    /**
     * 当前时间
     */
    public DateTime() {
        super();
    }

    /**
     * 给定日期的构造
     *
     * @param date 日期
     */
    public DateTime(Date date) {
        this(date.getTime());
    }

    /**
     * 给定日期毫秒数的构造
     *
     * @param timeMillis 日期毫秒数
     */
    public DateTime(long timeMillis) {
        super(timeMillis);
    }

    /**
     * 获取精确到秒的时间戳
     */
    public Long getSecondTime() {
        String value = String.valueOf(getTime());
        String sub = StrKit.sub(value, 0, value.length() - 3);
        return Long.valueOf(sub);
    }

    @Override
    public String toString() {
        return DateTimeKit.formatDateTime(this);
    }

    public String toString(String format) {
        return DateTimeKit.format(this, format);
    }

    /**
     * @return 输出精确到毫秒的标准日期形式
     */
    public String toMsStr() {
        return DateTimeKit.format(this, DateTimeKit.NORM_DATETIME_MS_PATTERN);
    }

    /**
     * @return java.util.Date
     */
    public Date toDate() {
        return new Date(this.getTime());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy