从现在起3年后获得unix时间戳

Pri*_*alj 0 android unix-timestamp android-date

使用Android和Java 8,如何及时获得unix时间戳?

例如

  • 从现在开始整整3年
  • 从现在起整整2个月
  • 从现在起整整一天
  • 等等

我知道我可以使用当前的时间戳System.currentTimeMillis() / 1000L(您也可以使用Instant.now().getEpochSecond()新的java.time,但这需要Android API> 25).现在我需要得到偏移并减去它.我可以使用TimeUnit.Days.toSeconds(),但是如果我想减去年份,它就没有YEAR单位而且我不想自己捣乱闰年.

有一个简单的方法吗?

sus*_*dlh 5

试试这个来获取时间戳Calender....

2个月后.

    Calendar date= Calendar.getInstance();
    date.add(Calendar.MONTH, 2);//instead of 2 use -2 value in your case
    date.getTimeInMillis();
Run Code Online (Sandbox Code Playgroud)

一天后.

    Calendar date= Calendar.getInstance();
    date.add(Calendar.DAY_OF_MONTH, 1);//instead of 1 use -1 value in your case
    date.getTimeInMillis();
Run Code Online (Sandbox Code Playgroud)

3年后.

    Calendar date= Calendar.getInstance();
    date.add(Calendar.YEAR, 3);//instead of 3 use -3 value in your case
    date.getTimeInMillis();
Run Code Online (Sandbox Code Playgroud)

注意: - 使用负值表示返回日期.

希望它能解决你的问题.