如何不使用任何库就显示现在或1分钟前的时间?

时间:2019-05-26 14:24:03

标签: android

当日期如下时,如何在1分钟前或现在在android中计时: “ requestedDate”:1558779849000,无需使用任何库

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

我已经使用此方法将时间戳转换为您之前的时间 您需要对其进行修改以使其分叉,以便您在数小时前感觉到此节目

    private void setTime(int date) {
    long time = System.currentTimeMillis() / 1000;

    long mills = time - date;
    int hours = safeLongToInt(mills / (60 * 60));
    int mins = safeLongToInt((mills - (hours * 3600) / (60)) % 60);

    String diff = hours + ":" + mins;

    if (hours < 24) {

        if (hours < 1) {
            if (mins < 1)
                Log.d("tessssst", "just now");
            else
                Log.d("tessssst", mins + "min ago");
        } else
        Log.d("tessssst", diff + " hours ago");

    } else {
        Date dateTemp = new Date(date * 1000L);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss ");

        sdf.setTimeZone(getTimeZone("GMT+2"));
        String formattedDate = sdf.format(dateTemp);

        System.out.println(formattedDate);
        Log.d("tessssst", formattedDate);

    }
}

private static int safeLongToInt(long l) {
    if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) {
        throw new IllegalArgumentException
                (l + " cannot be cast to int without changing its value.");
    }
    return (int) l;
}

编辑:这对您应该很好用,结果可以在日志Log.d("tessssst", "just now");

中找到
相关问题