【问题标题】:Display humanized time output显示人性化时间输出
【发布时间】:2013-10-06 04:59:11
【问题描述】:

我正在尝试在我的一个应用程序中显示以下消息以显示等待时间

2 小时 3 分钟 4 秒
1小时2秒

如您所见,可能会有很多变化,我正在努力完成这项工作。以下代码运行良好,给定的秒数给出了非零小时、分钟和秒,但如果我必须处理只有小时和秒要显示而没有分钟要显示的情况,这将完成。

也不知道在哪里添加那些字符串'and'和逗号。

我相信应该已经有一个我可能不知道的解决方案。

public class HumanizedWaitDisplay {

public static void main(String[] args) {
    int noOfSecToWait = 60*60*2 + 30;
    System.out.println("Waiting for " + getHumanizedTime(noOfSecToWait));
}

private static String getHumanizedTime(int seconds) {
    String out ="";
    int hr = seconds/(60*60);
    seconds = seconds%(60*60);
    int min = seconds/(60);
    seconds = seconds%(60);

    if(hr>0) {
        out +=  hr + " hour" +(hr == 1 ? " ":"s ");
    }
    if(min > 0){
        out +=  min + " min" +(min == 1 ? " ":"s ");
    }
    if(seconds > 0){
        out +=  seconds + " s";
    }
    return out;
}
}

如果你遇到过这样的事情,请告诉我。

【问题讨论】:

  • 您的代码似乎运行良好。有什么问题?在哪些输入上?
  • 看看这个answer
  • @Maroun,我无法确定“和”和逗号的位置。如果只有小时可以显示并且分钟和秒为零......有很多情况。希望你能得到照片..
  • @R.J 在您提供的链接上完美回答。谢谢。曾尝试搜索SO,但不清楚关键字。
  • @mtk - 没有问题!我也发生过很多次。

标签: java time humanize


【解决方案1】:

您可以使用 java 日期和时间 api。简要示例(使用您在问题正文中提供的方法签名):

static DateFormat dateFormat = new SimpleDateFormat("dd/MM/yy"); //specifiy the 
//representation pattern. This is for example. You can construct it more accurate:
//display the hours, mins, secs, etc. Also you can specify your own delimiters. 
//See more at docs below

static { //upd: set the proper timezone
    dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
}

private static String getHumanizedTime(int seconds) {
    long timeInMillis = seconds * 1000; //Date object is designed for millis
    Date date = new Date(timeInMillis);
    return dateFormat.format(date);
}

在 javadocs 上阅读更多内容: http://docs.oracle.com/javase/6/docs/api/java/util/Date.html http://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html

更新:如果您希望输出类似于“11 小时 15 分 34 秒”,请尝试:

static DateFormat dateFormat = new SimpleDateFormat("HH 'hours,' mm 'minutes and' ss 'seconds'");

【讨论】:

    【解决方案2】:

    这将返回您发布的结果

    2 小时 3 分 4 秒

    1 小时 2 秒

    请注意,逗号“,”仅在 mins 不为 0 时才包含,因此我们必须将其放在 min > 0 的 if 测试中。 "and" 仅在秒数不为 0 时出现,因此我们将其放入 if 测试中,秒数 > 0。

        if(hr > 0) {
            out += hr + " hour" +(hr == 1 ? "":"s");
        }       
        if(min > 0){
            out += ", " + min + " min" +(min == 1 ? "":"s");
        }
        if(seconds > 0){
            out += " and " + seconds + " s";
        }
        return out;
    

    【讨论】:

    • 如果 min 和 secs 为 0,这将给出一个尾随 ','。
    • 嘿.. 如果小时为零,它现在会给出一个前导 ','。我希望你明白这不是那么简单:)
    【解决方案3】:

    试试这个,这段代码可以解决你的问题

    private static String getHumanizedTime(int seconds) {
    String out ="";
    int hr = seconds/(60*60);
    seconds = seconds%(60*60);
    int min = seconds/(60);
    seconds = seconds%(60);
    
    boolean htrue = false;
    boolean mtrue = false;
    if(hr>0) {
        out +=  hr + " hour" +(hr == 1 ? " ":"s ");
        htrue = true;
    }
    if(min > 0){
        if(htrue && seconds>0)
            out += ",";
        else if(seconds==0)
            out += "and";
    
        mtrue = true;
        out +=  min + " min" +(min == 1 ? " ":"s ");
    
    }
    if(seconds > 0){
        if(htrue || mtrue)
            out += "and ";
        out +=  seconds + " s";
    }
    return out;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-10
      • 1970-01-01
      • 2021-09-20
      • 2012-06-27
      • 2010-12-23
      • 2018-07-05
      • 2011-12-17
      相关资源
      最近更新 更多