解释一下为什么会在java学习教程中放js的代码.
1,最直接的肯定是我有时候会写js,而一些经典的逻辑,又不想新开前端文章,索性就放一起.
2,java的面向对象太完善了,这也是我写文章的原因,导致写java代码很难学习到面向过程的编程思想,正好,原生js的代码,很大程度上写起来用的思维方式,很雷同面向过程的思考方式.算个补全,对写java代码,理解java代码,大有裨益.(这不是作者胡乱找补,写js真能帮助理解java)
java
Date , LocalDateTime , Calendar
java中操作时间,记住上述三个类就行了.具体怎么用,用到的时候再百度.(这样真的挺高效的)
还是举个例子吧.
// 获取当前时间(需要计算的话用这个) LocalDateTime now = LocalDateTime.now(); // 减去一个小时 LocalDateTime oneHourAgo = now.minusHours(1); // 如果需要格式化的字符串 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); String createStartDate = oneHourAgo.format(formatter);// 获取当前时间(不需要计算但可能在字符串或时间戳转来转去的话用这个)
String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
// 获取最小月份 Collections.sort(month); int minMonth = month.get(0); // 获取最大月份 int maxMonth = month.get(month.size() - 1); // 创建日期格式对象 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 创建 Calendar 实例用于设置日期 Calendar calendar = Calendar.getInstance(); // 设置开始时间和结束时间 calendar.set(yearClass, minMonth - 1, 1); // 注意月份是从0开始的 Date startTime = calendar.getTime(); calendar.set(yearClass, maxMonth - 1, getDaysInMonth(yearClass, maxMonth)); // 设置为该月最后一天 Date endTime = calendar.getTime(); // 辅助方法:计算某个月份的天数 private static int getDaysInMonth(int year, int month) { if (month == 2) { // 二月 if (isLeapYear(year)) return 29; else return 28; } else if (month == 4 || month == 6 || month == 9 || month == 11) { // 四、六、九、十一月 return 30; } else { // 其他月份 return 31; } } // 辅助方法:判断是否为闰年 private static boolean isLeapYear(int year) { return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); }
String dateStr = "2024-10"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM"); YearMonth yearMonth = YearMonth.parse(dateStr, formatter); LocalDate localStartDate = yearMonth.atDay(1); LocalDate localEndDate = yearMonth.atEndOfMonth(); ZoneId zoneId = ZoneId.systemDefault(); Date startTime = Date.from(localStartDate.atStartOfDay(zoneId).toInstant()); Date endTime = Date.from(localEndDate.atStartOfDay(zoneId).toInstant());
js (挺有意思,具体什么意思,各位自己找人工智能)
attached() {
let that = this;
that.countDown(that.properties.time * 1000, function (msg) {
that.setData({
countdown: msg
});
});
},
//倒计时15分钟
countDown(time, fn) {
let that = this;
let minute = 15 * 60 * 1000;
var maxtime = (new Date((time + minute)) - new Date()) / 1000; //剩余秒
var timer = setInterval(function () {
if (maxtime >= 0) {
// var dd = parseInt(maxtime / 60 / 60 / 24, 10);//计算剩余的天数
// var hh = parseInt(maxtime / 60 / 60 % 24, 10);//计算剩余的小时数
var mm = parseInt(maxtime / 60 % 60, 10); //计算剩余的分钟数
var ss = parseInt(maxtime % 60, 10); //计算剩余的秒数
// hh = checkTime(hh);
mm = that.checkTime(mm);
ss = that.checkTime(ss);
let msg = "剩 " + mm + ":" + ss + ' 自动取消';
fn(msg);
--maxtime;
} else {
clearInterval(timer);
fn("已取消");
// that.sendtoparent();
}
}, 1000);
},