【问题标题】:Android find reaming time or deference between two timesAndroid查找剩余时间或两次之间的差异
【发布时间】:2017-11-18 21:17:55
【问题描述】:

想求两次之间的差异 我有两次 12:16:4012:16:50。 我想要的差异是 10 秒 > 等等。

我想要这样的答案

2 秒前

5 分钟前

2 小时前

Date date = new Date();
DateFormat formatter = new SimpleDateFormat("HH:mm:ss"); // Hours:Minutes:Seconds
String dateFormatted = formatter.format(date); 

【问题讨论】:

  • 那么您想根据差异的大小以不同的方式显示以毫秒为单位的时间差异吗?使用一些 if 语句来区分大小写。
  • 发帖前搜索 Stack Overflow。这个话题已经被讨论过很多次次了。

标签: android performance datetime time


【解决方案1】:

您好,您可以使用下面的代码,这是我给您的示例,您需要根据您的要求进行更改

try {

                Calendar c2 = Calendar.getInstance();
                SimpleDateFormat df2 = new SimpleDateFormat("HH:mm:ss aa",Locale.US);
                String formattedTime = df2.format(c2.getTime());
                date2 = df2.parse(formattedTime);

                long mills = date2.getTime() - date1.getTime();


                int Hours = (int) (mills/(1000 * 60 * 60));
                int Minuets = (int) (mills/(1000*60)) % 60;
                int Seconds = (int) (mills/1000) % 60;

                String hr = String.format("%02d", Hours);
                String min = String.format("%02d", Minuets);
                String sec = String.format("%02d", Seconds);


                endTime = hr + ":" + min + ":" +sec; // updated value every1 second

            } catch (ParseException e) {
                e.printStackTrace();
            }

因此,对于您的问题,请使用以下函数来获取答案并导入以下类

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

private void cluclate()
{
    Date date1,date2;

    try {

        String  endTime;

        Calendar c1 = Calendar.getInstance();
        c1.add(Calendar.HOUR,12);
        c1.add(Calendar.MINUTE,16);
        c1.add(Calendar.SECOND,40);
        SimpleDateFormat df1 = new SimpleDateFormat("HH:mm:ss", Locale.US);
        String formattedTime1 = df1.format(c1.getTime());
        date1 = df1.parse(formattedTime1);


        Calendar c2 = Calendar.getInstance();
        c2.add(Calendar.HOUR,12);
        c2.add(Calendar.MINUTE,16);
        c2.add(Calendar.SECOND,50);
        SimpleDateFormat df2 = new SimpleDateFormat("HH:mm:ss",Locale.US);
        String formattedTime2 = df2.format(c2.getTime());
        date2 = df2.parse(formattedTime2);


        long mills = date2.getTime() - date1.getTime();


        int Hours = (int) (mills/(1000 * 60 * 60));
        int Minuets = (int) (mills/(1000*60)) % 60;
        int Seconds = (int) (mills/1000) % 60;

        String hr = String.format("%02d", Hours);
        String min = String.format("%02d", Minuets);
        String sec = String.format("%02d", Seconds);


        endTime = hr + ":" + min + ":" +sec; // updated value every1 second

        Log.d("=============>>>>",endTime);

    } catch (ParseException e) {
        e.printStackTrace();
    }
}

【讨论】:

    【解决方案2】:

    这可以使用 DateUtils 类轻松实现。

    getRelativeTimeSpanString(long time, long now, long minResolution)

    Calendar time = Calendar.getInstance();
    time.set(Calendar.HOUR, 12);
    time.set(Calendar.MINUTE, 16);
    time.set(Calendar.SECOND, 40);
    
    Calendar now = Calendar.getInstance();
    now.set(Calendar.HOUR, 12);
    now.set(Calendar.MINUTE, 16);
    now.set(Calendar.SECOND, 50);
    System.out.println(DateUtils.getRelativeTimeSpanString(time.getTimeInMillis(), now.getTimeInMillis(), DateUtils.SECOND_IN_MILLIS));
    

    如果时差以分钟或小时为单位,这也将有效。

    【讨论】:

    • 如果我要设置时间 time.set(Calendar.HOUR, 12); time.set(Calendar.MINUTE, 16); time.set(Calendar.SECOND, 40);现在日历 = Calendar.getInstance(); now.set(Calendar.HOUR, 2); now.set(日历.分钟, 11);现在.set(日历.SECOND, 50);它将在 10 小时内返回
    • 这取决于时间日期和现在的变量。
    【解决方案3】:

    在可运行文件中使用以下代码,以便它继续检查。

    Calendar c2 = Calendar.getInstance();
    SimpleDateFormat df2 = new SimpleDateFormat("HH:mm:ss aa",Locale.US);
    String formattedTime = df2.format(c2.getTime());
    date2 = df2.parse(formattedTime);
    //date1 is the first time, date2 is current time.
    long mills = date2.getTime() - date1.getTime();
    
    int Hours = (int) (mills/(1000 * 60 * 60));
    int Minuets = (int) (mills/(1000*60)) % 60;
    int Seconds = (int) (mills/1000) % 60;
    
    if(Hours == 0) {
        if(Minutes == 0) {
            displayString = Seconds.toString() + " ago";
        } else {
            displayString = Minutes.toString() + " ago";
        }
    } else {
        displayString = Hours.toString() + " ago";
    }
    

    【讨论】:

      【解决方案4】:

      试试这个:

      long firstTime = System.currentTimeMillis();
          /*
          your operations should do here
           */
          long secondTime = System.currentTimeMillis();
          long difference = secondTime - firstTime;
          Calendar calendar = Calendar.getInstance();
          calendar.setTimeInMillis(difference);
          SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss", Locale.GERMANY);
          String yourDate = simpleDateFormat.format(calendar);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-01-07
        • 2019-08-23
        • 2014-08-05
        • 1970-01-01
        • 2018-07-30
        • 2013-09-25
        • 1970-01-01
        相关资源
        最近更新 更多