ツールの方向性: フロントエンドとバックエンドのタイムスタンプと String の間の変換と T を使用した LocalDateTime の解を完全に解決するツール クラス

前提:

  1. データベース内の時刻形式は dateTime です

ここに画像の説明を挿入します

  1. JAVA クラスの LocalDateTime はデータベースの dateTime に対応しますここに画像の説明を挿入します

ツール:

注: デフォルトの粒度はミリ秒ですが、秒の粒度を解放する必要があります。

/**
 * @author luo qinfeng
 */
@Component
public class TimeUtils {

    /**
     * 格式化传入的时间戳(毫秒)
     * @param timeStamp
     * @return
     */
    private String TimeStampToTime(String timeStamp){
        String formats = "yyyy-MM-dd HH:mm:ss";
        //粒度:毫秒
        Long timestamp = Long.parseLong(timeStamp) ;
        //粒度:秒
        //  Long timestamp = Long.parseLong(timeStamp)  * 1000 ;
        return new SimpleDateFormat(formats, Locale.CHINA).format(new Date(timestamp));
    }

    /**
     * Stinrg -> LocalDateTime
     * @param time
     * @return
     */
    private LocalDateTime StringTimeToLocalDateTime(String time){

        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime localTime = LocalDateTime.parse(time,df);
        return localTime;
    }

    /**
     * LocalDateTime -> String
     * @param time
     * @return
     */
    private String LocalDateTimeTimeToString(LocalDateTime time){

        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String stringTime = df.format(time);
        return stringTime;
    }


    /**
     * 格式化返回给前端的时间 以毫秒为单位
     * @param stringTime
     * @return
     */
    private Long StringTimeToTimeStamp(String stringTime) {
        Long timestamp = null;
        try {
            timestamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(stringTime).getTime();

        } catch (ParseException e) {
            e.printStackTrace();
        }
        //是毫秒
        return timestamp;

    }


    /**
     *  格式化localDateTime (2020-11-20T 19:30:56)
     * @param timeFromDataBase
     * @return 2020-11-20 19:30:56
     */
    public String standerFormat(LocalDateTime timeFromDataBase){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String s_timeFromDataBase = formatter.format(timeFromDataBase);
        String time = s_timeFromDataBase.replace("T", " ");
        //毫秒
        return time;
        //秒
        // return time/1000;
    }
}


おすすめ

転載: blog.csdn.net/qq_44716086/article/details/109692867
おすすめ