用java和html两种方式实现2023兔年春节倒计时

1,889 阅读2分钟

我正在参加「兔了个兔」创意投稿大赛,详情请看:「兔了个兔」创意投稿大赛

兔年春节将至,愿每一个人所愿皆所得,“钱兔”似锦。

运行效果

  • java运行效果图

java实现兔年春节倒计时的截图.png

  • html运行效果(平时不怎么写前端,写不了好看的样式,见谅)

html实现兔年春节倒计时的截图.png

代码实现

java实现

  1. 思路步骤
  • 获取当前时间
  • 设置结束时间(2023兔年春节的具体时间)
  • 获取这两个时间相差的年月日时分秒
  1. 用到的API
  • 主要用到了LocalDateTime类,它是Java8新特性之一,提供常用的方法,方法名通俗易懂并按照一定规则进行命名,如加、减、格式化、解析等等。

  • LocalDateTime.now() 从默认时区的系统时钟获取当前的日期时间

  • LocalDateTime.of(int year, int month, int dayOfMonth, int hour, int minute, int second) 返回指定年月日时分秒的LocalDateTime对象

  • LocalDateTime.until(Temporal endExclusive, TemporalUnit unit) 根据指定的单位计算此日期时间和结束日期时间之间的时间量

  • LocalDateTime.plusDays(long days) 参数是要添加的天数,它返回带有已添加天数的LocalDateTime对象

  • LocalDateTime.plusHours(long hours) 参数是要添加的小时数,它返回带有已添加小时数的LocalDateTime对象

  • LocalDateTime.plusMinutes(long minutes) 参数是要添加的分钟数,它返回带有已添加分钟数的LocalDateTime对象

  • 上面三个plus都是指定了单位的添加,返回已添加的LocalDateTime对象。增加时间使用plus,相反减少时间使用minus。

  1. 代码

package cn.juejin.demo;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

/**
 * 兔年春节倒计时
 */
public class CountDown {

    public static void main(String[] args) {
        // 1.获取当前时间
        LocalDateTime nowTime = LocalDateTime.now();
        // 2.设置结束时间(2023兔年春节的具体时间)
        LocalDateTime endTime = LocalDateTime.of(2023, 01, 22, 00, 00, 00);
        // 3. until是获取两个时间相差的年月日时分秒...
        long days = nowTime.until(endTime, ChronoUnit.DAYS);
        // 开始时间添加计算到的天数
        nowTime = nowTime.plusDays(days);
        long hours = nowTime.until(endTime, ChronoUnit.HOURS);
        nowTime = nowTime.plusHours(hours);
        long minutes = nowTime.until(endTime, ChronoUnit.MINUTES);
        nowTime = nowTime.plusMinutes(minutes);
        long seconds = nowTime.until(endTime, ChronoUnit.SECONDS);
        if (!(seconds < 0)) {
            String outputStr = "🐇" + "                                   距离2023兔年春节还有:" + days + "天" + hours + "小时" + minutes + "分" + seconds + "秒                                    " + "🐇";
            System.out.println("🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇");
            System.out.println("🐇                                                                                                        🐇");
            System.out.println("🐇                                                                                                        🐇");
            System.out.println("🐇                                                                                                        🐇");
            System.out.println(outputStr);
            System.out.println("🐇                                                                                                        🐇");
            System.out.println("🐇                                                                                                        🐇");
            System.out.println("🐇                                                                                                        🐇");
            System.out.println("🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇");
        } else {
            System.out.println("🐇兔年至,祝您春节快乐!");
        }

    }

}


html实现

平时写的前端代码不多,只会写一点简单的,样式也不太会,这里只是实现了倒计时的功能。

  1. 思路步骤
  • 获取当前时间并转换为指定格式(年月日时分秒 星期几)
  • 设置春节时间并计算倒计时
  • 打印输出,页面设置定时刷新。
  1. 主要用到的方法
  • Math.floor() 返回小于或等于一个给定数字的最大整数,可以理解为向下取整
  • 自动刷新该页面,content是时间 <meta http-equiv="refresh" content="1">
  1. 代码
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>2023兔年春节倒计时</title>
    <!-- 每隔1秒自动刷新该页面 -->
    <meta http-equiv="refresh" content="1" > 
    <style>
      body {
            text-align: center;
            margin: 100px auto;
        }
    </style>
</head>
<body>
    <script>
		// 获取当前时间
        function getTime() {
            var arr = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
            var time = new Date(),
                year = time.getFullYear(), //获取年份
                month = time.getMonth() + 1, //获取月份,月份由0~11表示
                date = time.getDate(), //获取日期
                day = time.getDay(); //获取星期
 
            var h = time.getHours(); //获取小时
            h = h < 10 ? '0' + h : h; //如果小于10,前面补0
            var m = time.getMinutes(); //获取分钟
            m = m < 10 ? '0' + m : m;
            var s = time.getSeconds(); //获取秒
            s = s < 10 ? '0' + s : s;
            return '当前时间是:' + year + '年' + month + '月' + date + '日 ' + '&nbsp;' + '&nbsp;' + h + ' : ' + m + ' : ' + s + '&nbsp;' + '&nbsp;' + '&nbsp;' + arr[day];
 
        }
		// 倒计时
        function countDown() {
            var currentTime = +new Date(); //获取当前时间戳
            var ednTime = +new Date('2023-01-22 00:00:00'); //设置结束时间
            var time = (ednTime - currentTime) / 1000; //两个时间相差的秒数
            var day = Math.floor(time / 60 / 60 / 24); //转化为天数,向下取整
            day = day < 10 ? '0' + day : day;
            var hours = Math.floor((time / 60 / 60 % 24)) //对天数取余,向下取整
            hours = hours < 10 ? '0' + hours : hours;
            var minutes = Math.floor((time / 60 % 60)) //对小时取余,向下取整
            minutes = minutes < 10 ? '0' + minutes : minutes;
            var seconds = Math.floor((time % 60)) //对分钟取余,向下取整
            if (time > 0) {
                return ('距离兔年春节还有 : ' + '&nbsp;' + '&nbsp;' + day + '天' + '&nbsp;' + '&nbsp;' + hours + '小时' + '&nbsp;' + '&nbsp;' + minutes + '分' + '&nbsp;' + '&nbsp;' + seconds + '秒');
            } else {
                return '"🐇兔年至,祝您春节快乐!'
            }
        }
        document.write(getTime());
        document.write('<br>');
        document.write('<br>');
        document.write(countDown());
    </script>
</body>
</html>

码上掘金

  • java

  • html

最后预祝各位掘友新春快乐、万事如意、幸福安康。