import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

/**
 * @description:
 * @author: kejie.huang
 * @date: Created in 2019/10/16 14:36
 * @version:
 * @modified By:
 */
public class Demo01 {
    public static void main(String[] args) {
        LocalDateTime localDateTimeNow = LocalDateTime.now();
        System.out.println(DateUtils.formatDateToParse(DateUtils.localDateTimeConvertDate(localDateTimeNow),DateUtils.DATE_FORMART_YYYY_MM_DD_HH_MM_SS));
        Long milliSecond = getTimestampOfDateTime(localDateTimeNow);
        milliSecond = milliSecond + (7200L * 1000L);
        LocalDateTime dateTime = getDateTimeOfTimestamp(milliSecond);
        System.out.println(DateUtils.formatDateToParse(DateUtils.localDateTimeConvertDate(dateTime),DateUtils.DATE_FORMART_YYYY_MM_DD_HH_MM_SS));
    }
    public static long getTimestampOfDateTime(LocalDateTime localDateTime) {
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = localDateTime.atZone(zone).toInstant();
        return instant.toEpochMilli();
    }
    public static LocalDateTime getDateTimeOfTimestamp(long timestamp) {
        Instant instant = Instant.ofEpochMilli(timestamp);
        ZoneId zone = ZoneId.systemDefault();
        return LocalDateTime.ofInstant(instant, zone);
    }
}

往往在开发过程中,交互时,需要头部设置TOKEN,而这个TOKEN的有效期,该如何设置?以上的案例只是婉转的举例说明,7200 * 1000 代表两个小时,具体的算法是 1*60*60*2,而一秒的等于1000,以上引用了DateUtils,可以参考https://my.oschina.net/u/3155476/blog/3118522

10-17 10:13