Java 时间格式之间的相互转换(Date、Calendar、timestamp时间戳)

时间:2025-04-02 07:28:33

存在以下6种情况:

目录

-> Calendar

-> 时间戳

           -> 时间戳

-> Date

           5.时间戳 -> Date

           6.时间戳 -> Calendar


-> Calendar

    /**
     * Date -> Calendar
     * @param date
     * @return
     */
    private Calendar Date2Calendar(Date date) {
        Calendar calendar = ();
        (date);
        return calendar;
    }

-> 时间戳

    /**
     * Date -> 时间戳
     * @param date
     * @return
     */
    private Long date2Timestamp(Date date) {
        return ();
    }

-> 时间戳

    /**
     * Calendar -> 时间戳
     * @param calendar  日历类对象
     * @return
     */
    private Long calender2Timestamp(Calendar calendar) {
        return ().getTime();
    }

-> Date

    /**
     * Calendar -> Date
     * @param calendar  日历类对象
     * @return
     */
    private Date calender2Date(Calendar calendar) {
        return ();
    }

5.时间戳 -> Date

    /**
     * 时间戳 -> Date
     * @param timestamp 时间戳
     * @return
     */
    private Date timestamp2Date(Long timestamp) {
        return new Date(timestamp);
    }

6.时间戳 -> Calendar

    /**
     * 时间戳 -> Calendar
     * @param timestamp 时间戳
     * @return
     */
    private Calendar timestamp2Calendar(Long timestamp) {
        Date date = new Date(timestamp);
        Calendar calendar = ();
        (date);
        return calendar;
    }