【问题标题】:Convert seconds to Relative time将秒转换为相对时间
【发布时间】:2020-07-07 13:10:15
【问题描述】:

我一直在尝试制作一个应用程序,我需要将时间(以秒为单位)转换为相对时间,有人可以帮助我做到这一点。 示例:

time in seconds:1594564500  
date:12 July 2020 20:05:00 GMT+05:30 
relative time: In 5 days 

【问题讨论】:

  • 如果其中一个答案解决了您的问题,您可以通过将其标记为已接受来帮助社区。接受的答案有助于未来的访问者自信地使用该解决方案。

标签: java android datetime time instant


【解决方案1】:

我一直在尝试制作一个需要将时间转换为 秒到相对时间

一种简单的方法是从给定时间获取Instant(例如instant)的对象,然后使用Instant.now().until(instant, ChronoUnit.DAYS)。

演示:

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.temporal.ChronoUnit;

public class Main {
    public static void main(String[] args) {
        // Create an object of Instant from the given no. of seconds
        Instant instant = Instant.ofEpochSecond(1594564500);

        // Get the no. of days from the current time to the given time
        long days = Instant.now().until(instant, ChronoUnit.DAYS);
        System.out.println("No. of days: " + days);

        // ##########If you want to get date-time information##########
        // Create OffsetDateTime from Instant
        OffsetDateTime odt = instant.atOffset(ZoneOffset.UTC);// UTC
        System.out.println("Date-time at UTC:" + odt);
        odt = instant.atOffset(ZoneOffset.ofHours(1));// GMT
        System.out.println("Date-time at GMT:" + odt);

        // Get LocalDateTime from OffsetDateTime
        LocalDateTime ldt = odt.toLocalDateTime();
        System.out.println("Date-time without time-zone infromation: " + ldt);
    }
}

输出:

No. of days: 5
Date-time at UTC:2020-07-12T14:35Z
Date-time at GMT:2020-07-12T15:35+01:00
Date-time without time-zone infromation: 2020-07-12T15:35

一些重要说明:

  1. 从here 了解有关现代日期时间 API 的更多信息。
  2. LocalDateTime 从中删除了时区和时区偏移的重要信息。根据您的要求,从下面给出的list 中选择正确的日期时间对象:

  1. 将 Java SE 8 日期时间类向后移植到 Java SE 6 和 7: 检查 ThreeTen-Backport 和 How to use ThreeTenABP

【讨论】:

  • 噢,我喜欢漂亮的图表!做得好。一些想法:(a) 你的标题Seconds* 有一个星号,但我看不到脚注。 (b) 同样,我在底行看到三个星号,但不知道这表示什么。 (c) 我建议将Instant、ZonedDateTime、OffsetDateTime 一起移动。并添加一个列,指示哪些代表一个时刻(时间轴上的一个特定点),就是这 3 个类。
【解决方案2】:

你会使用类似的东西:

        long timeInSeconds = 1594564500;
        long interval;
        String relative;

        long difference = Math.abs(System.currentTimeMillis() - (timeInSeconds * 1000));

        if (difference > DateUtils.WEEK_IN_MILLIS) {
            interval = difference / DateUtils.WEEK_IN_MILLIS;
            relative = " weeks";
        } else if (difference > DateUtils.DAY_IN_MILLIS) {
            interval = difference / DateUtils.DAY_IN_MILLIS;
            relative = " days";
        } else if (difference > DateUtils.HOUR_IN_MILLIS) {
            interval = difference / DateUtils.HOUR_IN_MILLIS;
            relative = " hours";
        } else {
            interval = Long.MIN_VALUE;
            relative = "Unknown";
        }
        interval = Math.round(interval);
        Log.d("Relative Time", String.valueOf(interval) + relative);

【讨论】:

    猜你喜欢
    • 2015-04-10
    • 1970-01-01
    • 2011-08-28
    • 1970-01-01
    • 1970-01-01
    • 2011-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多