【问题标题】:Calculating elasped time between Midnight and current time计算午夜和当前时间之间经过的时间
【发布时间】:2014-12-19 03:48:45
【问题描述】:

我正在尝试确定一天中过去了多少小时、分钟、秒。

计时器在每个新的一天的午夜重新启动。

因此,如果当前时间是下午 2:15,我需要计算为 14 小时 15 分钟。

我知道如何在 .Net 中轻松做到这一点,但不幸的是,我对 Android 和 Java 的日历/日期类不太熟悉。

目前看来 Date 类有些过时了。

谁能指出我正确的方向?

谢谢。

【问题讨论】:

  • 尝试使用JodaTime库,有很多选项可以找到两个日期之间的经过时间。
  • 使用JodaTime(或Java 8)本身是一个很好的建议,但由于这个问题被标记为Android,它可能不适用。

标签: android date calendar elapsedtime


【解决方案1】:

您可以使用Calendar 类来获取此信息。您可以像这样将Calendar 设置为午夜:

Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);

然后你可以得到毫秒的时间或者使用其他方法得到小时/分钟/秒等。

cal.getTimeInMillis(); 

【讨论】:

  • 哦哇这么简单吧?非常感谢...我会试一试然后接受。感谢您花时间发布!
  • 我使用此代码,但警报管理器总是每 5 秒执行一次,而不是每个午夜执行一次。请帮忙
  • @baras - 您应该在单独的问题中发布您的代码(然后在此处发布链接,以便我们提供帮助)
【解决方案2】:

您可以执行以下操作:

// get a calendar instance for midnight time
Calendar midnightCalendar = Calendar.getInstance();
midnightCalendar.set(Calendar.HOUR_OF_DAY, 0);
midnightCalendar.set(Calendar.MINUTE, 0);
midnightCalendar.set(Calendar.SECOND, 0);

long midnightTimeInMillis = midnightCalendar.getTimeInMillis();

// get a calendar instance for current time
Calendar currentTimeCalendar = Calendar.getInstance();

long currentTimeInMillis = currentTimeCalendar.getTimeInMillis();


// get the difference
long diffInMillis = currentTimeInMillis - midnightTimeInMillis;

// get the hours
int hours = (int)(diffInMillis / 3600 * 1000);

// get the minutes
int minutes = (int)(diffInMillis % 3600000) / (60 * 1000);

【讨论】:

    【解决方案3】:
    package com.umeng.comm.utils;
    
    import android.annotation.SuppressLint;
    import android.text.TextUtils;
    
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    
    /**
     * 
     * @author mrsimple
     */
    public class TimeUtils {
    
    public static final long ONE_SECOND = 1000L;
    public static final long ONE_MINUTE = 60000L;
    public static final long ONE_HOUR = 3600000L;
    public static final long ONE_DAY = 86400000L;
    // private static final long ONE_WEEK = 604800000L;
    
    /**
     * 
     */
    public static final int ONE_YEAR_DAYS = 365;
    /**
     * 
     */
    public static final String JUST_NOW = "just now";
    
    /**
     * 
     */
    public static final String MINUTE_AGO = "munites ago";
    /**
     * 
     */
    public static final String HOUR_AGO = "hours ago ";
    
    // private static final String TODAY = "Today ";
    
    public static final String YESTERDAY = "Yestoday ";
    
    public static final String THE_DAY_BEFORE_YESTERDAY = "the day before_yestoday ";
    /**
     * 
     */
    public static final String YEAR_AGO = "years ago";
    /**
     * 
     */
    @SuppressLint("SimpleDateFormat")
    private static SimpleDateFormat mFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
    /**
     * 
     * @param timeStr
     * @return
     */
    public static String format(String timeStr) {
        if (!TextUtils.isEmpty(timeStr)) {
            try {
                Date date = mFormat.parse(timeStr);
                return format(date);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return timeStr;
    }
    
    /**
     * 
     * @param timeStr 
     * @return 
     */
    public static long getTime(String timeStr){
        if ( TextUtils.isEmpty(timeStr) ) {
           return 0; 
        }
        try {
            return mFormat.parse(timeStr).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }
    
    /**
     * 
     * @param date
     * @return
     */
    public static String format(Date date) {
    
        int days = getDayWithToday(date);
        long timeGap = getTimeGap(date);
    
        if (days == 0) {
            // return TODAY + getSimpleTimeStr(date);
            return getTodaysDateString(timeGap);
        }
    
        if (days == 1) {
            return YESTERDAY + getSimpleTimeStr(date);
        }
    
        if (days == 2) {
            return THE_DAY_BEFORE_YESTERDAY + getSimpleTimeStr(date);
        }
    
        return getSimpleDateStr(date);
    }
    
    private static long getTimeGap(Date date) {
        final long now = System.currentTimeMillis();
        return now - date.getTime();
    }
    
    private static String getTodaysDateString(long timegap) {
        if (timegap < ONE_MINUTE) {
            return JUST_NOW;
        } else if (timegap <= ONE_HOUR) {
            return timegap / ONE_MINUTE + MINUTE_AGO;
        }
        return timegap / ONE_HOUR + HOUR_AGO;
    }
    
    /**
     * @param date
     * @return
     */
    private static int getDayWithToday(Date date) {
        // today's calendar
        Calendar calendar = Calendar.getInstance();
        int today = calendar.get(Calendar.DAY_OF_YEAR);
        int year = calendar.get(Calendar.YEAR);
        calendar.setTime(date);
        int createYear = calendar.get(Calendar.YEAR);
    
        int feedCreateDay = calendar.get(Calendar.DAY_OF_YEAR);
    
        return (today - feedCreateDay) + (year - createYear) * ONE_YEAR_DAYS;
    }
    
    /**
     * @return
     */
    private static String getSimpleTimeStr(Date date) {
        SimpleDateFormat sdf = (SimpleDateFormat) SimpleDateFormat.getInstance();
        sdf.applyPattern("HH:mm");
        return sdf.format(date);
    }
    
    /**
     * @param date
     * @return
     */
    private static String getSimpleDateStr(Date date) {
        SimpleDateFormat sdf = (SimpleDateFormat) SimpleDateFormat.getInstance();
        int days = getDayWithToday(date);
        String prefix = "";
        if (days >= ONE_YEAR_DAYS) {
            sdf.applyPattern("MM-dd");
            prefix = days / ONE_YEAR_DAYS + YEAR_AGO;
        } else {
            sdf.applyPattern("MM-dd HH:mm");
        }
    
        return prefix + sdf.format(date);
    }
    

    }

    用法:

    TimeUtils.format("2014-12-19 10:24:01");

    【讨论】:

    • 你把它弄得太复杂了。
    • 我的项目需要这一切。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多