定时器 - 倒计时并以特定格式显示结果 [英] Timer - counting down and displaying result in specific format

查看:101
本文介绍了定时器 - 倒计时并以特定格式显示结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有柜台,你设置一个日期,然后你可以得到这个日期的时间,但是我需要以选定的格式显示。



EG:
如果我有1天,13小时,43分30秒,并将格式设置为:


你有:# d#days,#h#hours,#m#minutes to die


然后只显示:


你有1天,13小时43分钟死亡


但是如果您设置格式为:


你有:#h#小时,#m#分钟死


然后我需要显示:


你有37小时,43分钟死


(所以缺少的类型(天)被转换为其他(小时))






我有这个代码:
PS:S,M,H,D,W,以毫秒为单位,以毫秒为单位等等...

  public static S tring getTimerData(long time,String format){
int seconds = -1,minutes = -1,hours = -1,days = -1,weeks = -1;
if(format.contains(#s#)){
秒=(int)(time / S);
}
if(format.contains(#m#)){
if(seconds == -1){
minutes =(int)(time / M) ;
} else {
minutes =(seconds / 60);
秒%= 60;
}
}
if(format.contains(#h#)){
if(minutes == -1){
hours =(int) (时间/ H);
} else {
hours =(minutes / 60);
minutes%= 60;
}
}
if(format.contains(#d#)){
if(hours == -1){
days =(int)时间/ D);
} else {
days =(hours / 24);
小时%= 24;
}
}
if(format.contains(#w#)){
if(days == -1){
weeks =(int) (时间/ W);
} else {
weeks =(days / 7);
days%= 7;
}
}
return format.replace(#w#,Integer.toString(weeks))。replace(#d#,Integer.toString(days))replace #h#,Integer.toString(hours))。replace(#m#,Integer.toString(minutes))。replace(#s#,Integer.toString(seconds));
}

还有更好的方式来做?

解决方案

  DateTimeFormatter formatter = DateTimeFormatter.ofPattern(yyyy MM dd); 
String text = date.toString(formatter);
LocalDate date = LocalDate.parse(text,formatter);

更多信息这里



或者,如果你不使用java8 这里


I have counter, you set a date, and then you can get time to this date, but I need display it in chosen format.

EG: If I have 1 day, 13 hours, 43 minutes and 30 seconds left, and set format to:

"You have: #d# days, #h# hours, #m# minutes to die"

Then just display:

"You have 1 day, 13 hours, 43 minutes to die"

But if you set format to:

"You have: #h# hours, #m# minutes to die"

Then I need display:

"You have 37 hours, 43 minutes to die"

(so missing type (days) are converted to other (hours))


I have this code: PS: S,M,H,D,W that just second in milliseconds, minutes in milliseconds etc...

public static String getTimerData(long time, String format) {
    int seconds = -1, minutes = -1, hours = -1, days = -1, weeks = -1;
    if (format.contains("#s#")) {
        seconds = (int) (time / S);
    }
    if (format.contains("#m#")) {
        if (seconds == -1) {
            minutes = (int) (time / M);
        } else {
            minutes = (seconds / 60);
            seconds %= 60;
        }
    }
    if (format.contains("#h#")) {
        if (minutes == -1) {
            hours = (int) (time / H);
        } else {
            hours = (minutes / 60);
            minutes %= 60;
        }
    }
    if (format.contains("#d#")) {
        if (hours == -1) {
            days = (int) (time / D);
        } else {
            days = (hours / 24);
            hours %= 24;
        }
    }
    if (format.contains("#w#")) {
        if (days == -1) {
            weeks = (int) (time / W);
        } else {
            weeks = (days / 7);
            days %= 7;
        }
    }
    return format.replace("#w#", Integer.toString(weeks)).replace("#d#", Integer.toString(days)).replace("#h#", Integer.toString(hours)).replace("#m#", Integer.toString(minutes)).replace("#s#", Integer.toString(seconds));
}

And... any better way to do that?

解决方案

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy MM dd");
String text = date.toString(formatter);
LocalDate date = LocalDate.parse(text, formatter);

more info here

or, if you're not using java8 here

这篇关于定时器 - 倒计时并以特定格式显示结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆