Java判断是否周末

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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");
}
}

}

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :