java实现倒计时

做的是个彩票的倒计时  每天早上10:20开盘,每周的2 5 7 晚上20:20停盘  在10:20--20:20时间段显示的是停盘时间  其他时间是显示开盘时间


public class TimeCountDownService {
    private SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public String getTime() throws ParseException {
        Calendar calendar = Calendar.getInstance();
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        String houStr = (hour >= 10) ? hour + "" : "0" + hour;
        String minStr = (minute >= 10) ? minute + "" : "0" + minute;
        String temp = houStr + ":" + minStr;
        int week = calendar.get(Calendar.DAY_OF_WEEK);
        if ((temp.compareTo("10:20")) <= 0&&(temp.compareTo("20:20"))>0) {//50400
            if(temp.compareTo("20:20")>0&&temp.compareTo("23:59")<=0){//如果是20:20-24:00
                return getReturnStr2(calendar);
            }else {//24:00 - 10:20
                return getReturnStr1(calendar);
            }
        } else {
            if (week == 2 && (temp.compareTo("20:20")) <= 0) {//122400
                return  getReturnStr(calendar,week);
            } else if (week == 3 && (temp.compareTo("20:20")) <= 0) {//36000
                return getReturnStr(calendar,week);
            } else if (week == 4 && (temp.compareTo("20:20")) <= 0) {//208800
               return getReturnStr(calendar,week);
            } else if (week == 5 && (temp.compareTo("20:20")) <= 0) {//122400
               return getReturnStr(calendar,week);
            } else if (week == 6 && (temp.compareTo("20:20")) <= 0) {//36000
               return getReturnStr(calendar,week);
            } else if (week == 7 && (temp.compareTo("20:20")) <= 0) {//122400
               return getReturnStr(calendar,week);
            } else if (week == 1 && (temp.compareTo("20:20")) <= 0) {//36000
                return getReturnStr(calendar,week);
            }
        }
        return "";
    }

    public String getReturnStr1(Calendar calendar) throws ParseException {

        long nowsec = (calendar.getTime().getTime())/1000;
        String now = sdf.format(calendar.getTime());
        String hm = sdf.format(calendar.getTime()).substring(11);
        String target = now.replace(hm, "10:20:00");
        long tarsec = (sdf.parse(target).getTime())/1000;
        long left=tarsec-nowsec;

        int day=(int) Math.floor(left/3600/24);
        int yday=(int)left%(24*60*60);

        int hours = (int) Math.floor(yday / 60 / 60);
        int ys=(int)yday%(60*60);

        int mins =  (int) Math.floor((ys / 60));
        int ym=(int)ys%60;

        int seconds = ym;
        return "距离开盘时间还有:"+day+"天"+hours+"小时"+mins+"分钟"+seconds+"秒";
    }

    public String getReturnStr2(Calendar calendar) throws ParseException {
        long nowsec = (calendar.getTime().getTime())/1000;
        String now = sdf.format(calendar.getTime());
        String hm = sdf.format(calendar.getTime()).substring(11);
        String target = now.replace(hm, "23:59:59");
        long tarsec = (sdf.parse(target).getTime())/1000;
        long left=tarsec-nowsec;

        int day=(int) Math.floor(left/3600/24);
        int yday=(int)left%(24*60*60);

        int hours = (int) Math.floor(yday / 60 / 60);
        int ys=(int)yday%(60*60);

        int mins =  (int) Math.floor((ys / 60));
        int ym=(int)ys%60;

        int seconds = ym;
        return "距离开盘时间还有:"+day+"天"+hours+"小时"+mins+"分钟"+seconds+"秒";
    }

    public String getReturnStr(Calendar calendar,int week) throws ParseException {
        long nowsec = (calendar.getTime().getTime())/1000;
        String now = sdf.format(calendar.getTime());

        String hm = sdf.format(calendar.getTime()).substring(11);

        String target = now.replace(hm, "20:20:00");
        long tarsec = (sdf.parse(target).getTime())/1000;
        if(week==4){
            tarsec=tarsec+24*60*60*2;
        }else if(week==2|week==5|week==7){
            tarsec=tarsec+24*60*60;
        }
        long left=tarsec-nowsec;

        int day=(int) Math.floor(left/3600/24);
        int yday=(int)left%(24*60*60);

        int hours = (int) Math.floor(yday / 60 / 60);
        int ys=(int)yday%(60*60);

        int mins =  (int) Math.floor((ys / 60));
        int ym=(int)ys%60;

        int seconds = ym;
        return "距离停盘时间还有:"+day+"天"+hours+"小时"+mins+"分钟"+seconds+"秒";
    }

}

前段通过get 请求 定时向后台请求 字符串(

"距离停盘时间还有:"+day+"天"+hours+"小时"+mins+"分钟"+seconds+"秒"

)返回值,然后赋值到指定地方。

兄弟不才 仅供参考

猜你喜欢

转载自blog.csdn.net/flyxiang0220/article/details/80312886