Java判断是否周末
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import java.time.ZoneId;
public class WeekendTest {
public static void main(String[] args) throws Exception {
Date cnDate = Date.from(LocalDateTime.now().atZone(TimeZone.getTimeZone("GMT+8").toZoneId()).toInstant());
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+8")); //不影响
//Calendar calendar = Calendar.getInstance(); //不影响
//calendar.setTime(cnDate); //错误
calendar.setTime(Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant())); //正确
System.out.println(calendar.getTime());
if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
System.out.println("WEEKEND");
} else {
System.out.println("NOT WEEKEND");
}
}
}
版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/02/18/java-judge-weekend/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。
THE END
0
二维码
打赏
海报
Java判断是否周末
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import java.time.ZoneId;
public class ……
文章目录
关闭
共有 0 条评论