获取起始时间

获取当天,本周,本月的起始时间

        LocalDate today = LocalDate.now();// 取当前日期
        LocalDate inputDate = LocalDate.parse(formatter(today));
        //当天的
        System.out.println(ToStringPattern(getBeginOfDay(ToDatePattern(formatter(today)))));//2018-03-21 00:00:00
        System.out.println(ToStringPattern(getEndOfDay(ToDatePattern(formatter(today)))));//2018-03-21 23:59:59

        //本周的第一天
        TemporalAdjuster FIRST_OF_WEEK = TemporalAdjusters.ofDateAdjuster(localDate -> localDate.minusDays(localDate.getDayOfWeek().getValue() - DayOfWeek.MONDAY.getValue()));
        System.out.println(inputDate.with(FIRST_OF_WEEK));
        System.out.println(ToStringPattern(getBeginOfDay(ToDatePattern(formatter(inputDate.with(FIRST_OF_WEEK))))));//2018-03-19 00:00:00
        //本周的最后一天
        TemporalAdjuster LAST_OF_WEEK = TemporalAdjusters.ofDateAdjuster(localDate -> localDate.plusDays(DayOfWeek.SUNDAY.getValue() - localDate.getDayOfWeek().getValue()));
        System.out.println(inputDate.with(LAST_OF_WEEK));
        System.out.println(ToStringPattern(getEndOfDay(ToDatePattern(formatter(inputDate.with(LAST_OF_WEEK))))));//2018-03-25 23:59:59

        // 取本月第1天:
        LocalDate firstDayOfThisMonth = today.with(TemporalAdjusters.firstDayOfMonth()); // 2014-12-01
        System.out.println(firstDayOfThisMonth);
        System.out.println(ToStringPattern(getBeginOfDay(ToDatePattern(formatter(firstDayOfThisMonth)))));//2018-03-01 00:00:00
        // 取本月最后一天,再也不用计算是28,29,30还是31:
        LocalDate lastDayOfThisMonth = today.with(TemporalAdjusters.lastDayOfMonth()); // 2014-12-31
        System.out.println(ToStringPattern(getEndOfDay(ToDatePattern(formatter(lastDayOfThisMonth)))));//2018-03-31 23:59:59

String转化为Date

public static Date ToDatePattern(String convertString) {
        Date result = null;
        if (!org.springframework.util.StringUtils.isEmpty(convertString)) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                sdf.setLenient(false);
                result = sdf.parse(convertString);
            } catch (Exception ex) {
            }
        } else {
        }
        return result;
    }

Date转化为String

 public static String ToStringPattern(Date date){
        String result=null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            result=sdf.format(date);
        } catch (Exception ex) {

        }
        return result;
    }

localDate转化Date

  public static String formatter(LocalDate localDate) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        return localDate.format(dateTimeFormatter);
    }

获取指定日期的起始时间

  public static Date getBeginOfDay(Date date) {
        Calendar cursorCal = Calendar.getInstance();
        cursorCal.setTime(date);
        cursorCal.set(Calendar.HOUR_OF_DAY, 0);
        cursorCal.set(Calendar.MINUTE, 0);
        cursorCal.set(Calendar.SECOND, 0);
        return cursorCal.getTime();
    }

获取指定日期的结束时间

  public static Date getEndOfDay(Date date) {
        Calendar cursorCal = Calendar.getInstance();
        cursorCal.setTime(date);
        cursorCal.set(Calendar.HOUR, 23);
        cursorCal.set(Calendar.MINUTE, 59);
        cursorCal.set(Calendar.SECOND, 59);
        return cursorCal.getTime();
    }

可以使用 HTML 的 `<input type="time">` 控件来获取起始时间值,然后使用 JavaScript 计算相隔时间数,并保留 2 位小数。 以下是一个示例代码: ```html <label for="startTime">起始时间:</label> <input type="time" id="startTime"> <br> <label for="endTime">结束时间:</label> <input type="time" id="endTime"> <br> <button onclick="calcTime()">计算相隔时间数</button> <p id="result"></p> <script> function calcTime() { var startTime = document.getElementById("startTime").value; var endTime = document.getElementById("endTime").value; // 将时间字符串转换为 Date 对象 var startDate = new Date("1970-01-01T" + startTime + "Z"); var endDate = new Date("1970-01-01T" + endTime + "Z"); // 计算相隔时间数(单位:毫秒) var diffTime = endDate.getTime() - startDate.getTime(); var diffSeconds = diffTime / 1000; // 显示结果 var result = document.getElementById("result"); result.innerHTML = "相隔时间数:" + diffSeconds.toFixed(2) + " 秒"; } </script> ``` 以上代码中,我们首先在 HTML 中定义了两个 `<input type="time">` 控件,分别用于输入起始时间和结束时间。然后定义了一个按钮,当点击按钮时会调用 `calcTime()` 函数。 在 `calcTime()` 函数中,我们首先通过 `getElementById()` 方法获取起始时间和结束时间的值,并将其转换为 Date 对象。由于 `<input type="time">` 控件返回的是本地时间,所以我们需要将时间字符串与 "1970-01-01T" 字符串拼接,然后使用 `new Date()` 构造函数将其转换为 Date 对象。 接着,我们计算起始时间和结束时间的相隔时间数(单位:毫秒),并将其转换为秒数。最后,我们将结果显示在页面中,使用 `toFixed(2)` 方法保留 2 位小数。 需要注意的是,上述代码中的时间计算是基于本地时间的,如果需要基于其他时区进行计算,需要进行时区转换。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值