Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
61 lines (46 loc) · 1.59 KB

File metadata and controls

61 lines (46 loc) · 1.59 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package chapter13;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import static java.lang.System.out;
/**
* 使用Calendar打印日历
*/
public class CalendarDemo {
public static void printCurrentMonth() {
Calendar calendar = Calendar.getInstance();
Map<Integer, String> weekStr = new HashMap<>();
weekStr.put(0, "日");
weekStr.put(1, "一");
weekStr.put(2, "二");
weekStr.put(3, "三");
weekStr.put(4, "四");
weekStr.put(5, "五");
weekStr.put(6, "六");
System.out.printf("%d-%d-%d 星期%s\n",
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH) + 1,
calendar.get(Calendar.DATE),
weekStr.get(calendar.get(Calendar.DAY_OF_WEEK)-1));
weekStr.values().forEach((val) -> out.print(val + "\t"));
out.println();
calendar.set(Calendar.DAY_OF_MONTH, 1);
int firstDayInMonth = calendar.get(Calendar.DAY_OF_WEEK) - 1;
int lastDate = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
for (int i = 1; i <= lastDate; i++) {
calendar.add(Calendar.DAY_OF_MONTH, 1);
if (i == 1) {
for (int j = 0; j < firstDayInMonth; j++) {
System.out.print("\t");
}
}
out.print(i + "\t");
if (calendar.get(Calendar.DAY_OF_WEEK) == 1) {
out.println();
}
}
}
public static void main(String[] args) {
CalendarDemo.printCurrentMonth();
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.