2013-10-29 211 views
16

这是我目前所面对的如何显示在天毫秒为单位:小时:分钟:秒

Seconds = (60 - timeInMilliSeconds/1000 % 60); 
Minutes = (60 - ((timeInMilliSeconds/1000)/60) %60); 

,我觉得是正确的。 几个小时,天就应该是这样的 -

Hours = ((((timeInMilliSeconds/1000)/60)/60) % 24); 
Days = ((((timeInMilliSeconds/1000)/60)/60)/24) % 24; 

和则 -

TextView.SetText("Time left:" + Days + ":" + Hours + ":" + Minutes + ":" + Seconds); 

,但我的时间和日子即将是不正确

+0

那么你为什么不利用存在(本机)的日期时间类? – 2013-10-29 19:33:19

+1

为什么'%24'在几天内?一个月有24天吗? –

+0

@ user2511414:你错在本机和框架。本地意味着用os本地语言编写,无论是什么(在本例中为C/C++),它都可以属于框架或任何应用程序。您所指的类(如SimpleDateFormat)属于框架,但是它们是否是本地的并不相关。 – njzk2

回答

28

一个简单的方法来计算时间是使用类似

long seconds = timeInMilliSeconds/1000; 
long minutes = seconds/60; 
long hours = minutes/60; 
long days = hours/24; 
String time = days + ":" + hours % 24 + ":" + minutes % 60 + ":" + seconds % 60; 

这将工作,如果你有超过28天,但不如果你有负面的时间。

+0

这个工作如果你有少于28天但超过0? –

+0

@AkshatAgarwal它将适用于任何非负数天。 0+使用SimpleDateFormat可能无法工作超过30天。确切地说,是 –

+0

!我在SimpleDateFormat上遇到了诸如少于1天(5小时5分27秒等)的日期问题。我会试一试你的想法,它看起来是正确的。 –

7

SimpleDateFormat的是你的朋友! (我刚刚发现今天,它的真棒。)

SimpleDateFormat formatter = new SimpleDateFormat("dd:HH:mm:ss", Locale.UK); 

Date date = new Date(timeInMilliSeconds); 
String result = formatter.format(date); 
+1

+1你能解释为什么需要',Locale.UK'吗?我从来没有用过它。 –

+1

编辑:njzk2是正确的 - 这将工作时间少于一个月。这对于超过一个月的时间段*不起作用。 SimpleDateFormat格式日历日期,而不是经过时间。它将解释毫秒偏移量作为1970年第一个月的日期。 –

+1

@CharlesForsythe:事实上,在不到一个月的时间内,它将起作用。 (只是不包括你的格式的几年或几个月)。 – njzk2

1

也可以使用乔达时API(http://joda.org/joda-time/

“的类DateTimeFormat提供了通过图案支持的格式的单个方法forPattern(字符串)。这些‘基于模式的’格式化提供一个类似的方法来SimpleDateFormat的。“

LocalDate date = LocalDate.now(); 
DateTimeFormatter fmt = DateTimeFormat.forPattern("d MMMM, yyyy"); 
String str = date.toString(fmt); 
+2

对不起,我不想为小到我的任务包含一个4.1 MB的库 –

2

来格式化经过/在机器人剩余时间,使用android.text.format.DateUtils,特别getRelativeTimeSpanStringformatElapsedTime

+0

你能解释一下它们各自做什么吗? –

+0

该文档是相当完整的,我不明白我可以添加什么,不会在任何有关此类的教程中。 – njzk2

+0

这将添加一个“in”前缀或“ago”后缀,并且它不显示所有天,小时,分钟,秒,但仅根据标志显示其中的一个。 – sschuberth

0

嗨,这段代码怎么样?

example) 

       0 ms -> 0 ms 
      846 ms -> 846ms 
      1,000 ms -> 1s 
      1,034 ms -> 1s 34ms 
     60,000 ms -> 1m 
     94,039 ms -> 1m 34s 39ms 
     3,600,000 ms -> 1h 
    61,294,039 ms -> 17h 1m 34s 39ms 
    86,400,000 ms -> 1d 
    406,894,039 ms -> 4d 17h 1m 34s 39ms 
31,536,000,000 ms -> 1y 
50,428,677,591 ms -> 1y 218d 15h 57m 57s 591ms 



/** 
* 
* @since 2018. 1. 9. 
* @author Park_Jun_Hong_(fafanmama_at_naver_com) 
*/ 
public class TimeUtils { 

    /** 
    * 
    * @since 2018. 1. 9. 
    */ 
    private TimeUtils() { 
    } 

    private static void appendTimeAndUnit(StringBuffer timeBuf, long time, String unit) { 
     if (time < 1) { 
      return; 
     } 

     timeBuf.append(time); 
     timeBuf.append(unit); 
    } 

    private static void prependTimeAndUnit(StringBuffer timeBuf, long time, String unit) { 
     if (time < 1) { 
      return; 
     } 

     if (timeBuf.length() > 0) { 
      timeBuf.insert(0, " "); 
     } 

     timeBuf.insert(0, unit); 
     timeBuf.insert(0, time); 
    } 

    /** 
    * Provide the Millisecond time value in {year}y {day}d {hour}h {minute}m {second}s {millisecond}ms. <br> 
    * Omitted if there is no value for that unit. 
    * 
    * @param timeInMillis 
    * @return 
    * 
    * @since 2018. 1. 9. 
    */ 
    public static String toYYYYHHmmssS(long timeInMillis) { 

     if (timeInMillis < 1) { 
      return "0 ms"; 
     } 

     StringBuffer timeBuf = new StringBuffer(); 

     long millis = timeInMillis % 1000; 
     appendTimeAndUnit(timeBuf, millis, "ms"); 

     // second (1000ms) & above 
     long time = timeInMillis/1000; 
     if (time < 1) { 
      return timeBuf.toString(); 
     } 

     long seconds = time % 60; 
     prependTimeAndUnit(timeBuf, seconds, "s"); 

     // minute(60s) & above 
     time = time/60; 
     if (time < 1) { 
      return timeBuf.toString(); 
     } 

     long minutes = time % 60; 
     prependTimeAndUnit(timeBuf, minutes, "m"); 

     // hour(60m) & above 
     time = time/60; 
     if (time < 1) { 
      return timeBuf.toString(); 
     } 

     long hours = time % 24; 
     prependTimeAndUnit(timeBuf, hours, "h"); 

     // day(24h) & above 
     time = time/24; 
     if (time < 1) { 
      return timeBuf.toString(); 
     } 

     long day = time % 365; 
     prependTimeAndUnit(timeBuf, day, "d"); 

     // year(365d) ... 
     time = time/365; 
     if (time < 1) { 
      return timeBuf.toString(); 
     } 

     prependTimeAndUnit(timeBuf, time, "y"); 

     return timeBuf.toString(); 
    } 
} 
相关问题