DateTimeFormatter 时间戳与String类型日期/时间互转

时间:2025-04-02 07:21:29
 /**
     * 时间戳转日期/时间
     *
     * @param seconds 时间戳
     * @param pattern 时间格式
     * @return 格式化的时间
     */
    @RequiresApi(api = Build.VERSION_CODES.O)
    public static String timeStamp2Date(long seconds, String pattern) {
        String time = "暂无数据";
        if ((pattern)) pattern = "yyyy-MM-dd  HH:mm:ss";
        LocalDateTime dateTime = (seconds / 1000L, 0, (8));
        if (seconds != 0) time = ((pattern));
        return time;
    }

    /**
     * 日期/时间转时间戳
     * @param date 时间
     * @param pattern 时间格式
     * @return  时间戳
     */
    @RequiresApi(api = Build.VERSION_CODES.O)
    public static long date2TimeStamp(String date,String pattern) {
        long timeStamp ;
        if ((pattern)) pattern = "yyyy-MM-dd  HH:mm:ss";
        DateTimeFormatter formatter = (pattern);
        LocalDateTime localDateTime = (date,formatter);
        timeStamp = ((8)).toEpochMilli();
        ("String转时间戳" + timeStamp);
        return timeStamp;
    }