当前位置:网站首页>Use localdate class to complete calendar design
Use localdate class to complete calendar design
2022-07-26 00:32:00 【YYniannian】
Preface
Before we finish this calendar design , Need to know Java Predefined classes in LocalDate Some uses of
grammar
LocalDate.now() // 2022-07-01
A new object will be constructed , Represents the date when the object was constructed .
LocalDate.of(1999, 1, 1)
Can provide years 、 Month and day to construct an object corresponding to a specific date :
Of course , Usually we want to save the constructed object in an object variable :
LocalDate newYearsEve = LocalDate.of(1999, 1, 1);
When there is one LocalDate object , It can be done in different ways getYear 、 getMonthValue and getDayOfMonth Get years 、 Month and day :
int year = newYearsEve.getYear(); // 1999 int month = newYearsEve.getMonthValue(); // 1 int day = newYeaersEve.getDayOfMonth(); // 1
The above method seems meaningless , Because these are the values used when constructing objects . however , Sometimes there may be a calculated date , Then you want to call these methods to learn more about it . for example , plusDays Method will get a new LocalDate , If the object to which this method is applied is called the current object , The new date object is a new date from the specified number of days of the current object :
LocalDate aThousandDaysLater = newYearsEve.plusDays(1000); year = aThousandDaysLater.getYear(); // 2002 month = aThousandDaysLater.getMonthValue(); // 09 day = aThousandDaysLater.getDayOfMonth(); // 26
aThousandDaysLater Is added to the original date 1000 God , At this time, the above method is effective
Calendar practice
demand : Use LocalDate Class shows the calendar of the current month , The format is as follows :
Mon Tue Wed Thu Fri Sat Sun
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 The current date uses * Number mark . You can see , This program needs to know how to calculate the days of a month and the corresponding day of the week for a given date .
Step breakdown
① First construct an object , And initialize with the current date
LocalDate date = LocalDate.now();`
② Get the current month and date
int month = date.getMonthValue(); int today = date.getDayOfMonth();
③ take date Set to the first day of the month , And get the day of the week
date = date.minusDays(today - 1); // Set to the first day of the month DayOfWeek weekday = date.getDayOfWeek(); int value = weekday.getValue(); // 1 = Monday 7 = Sunday
Variable weekday Set to DayOfWeek Object of type . We call the getValue Method to get a value of the day of the week . We will get an integer . Return on Monday 1, Return on Tuesday 2, By analogy , Return on Sunday 7.
④ Because the first line of the calendar is indented , This makes the first day of the month point to the corresponding day of the week . The following code will print the header and the indent of the first line
System.out.println("Mon Tue Wed Thu Fri Sat Sun");
for (int i = 1; i < value; i++)
System.out.print(" ");⑤ Print the body of the calendar , Into a cycle , among date Traverse every day of the month .
Each iteration , Print date value . If date Is the current date , This date uses a * Mark . Next , hold date Advance to the next day . If you arrive on the first day of the new week , Then line feed printing :
while (date.getMonthValue() == month) {
System.out.printf("%3d", date.getDayOfMonth());
if (date.getDayOfMonth() == today)
System.out.print("*");
else
System.out.print(" ");
date = date.plusDays(1);
if (date.getDayOfWeek().getValue() == 1)
System.out.println();
}⑥ When will it end ? We don't know how many days this month , yes 28、29、30 still 31. actually , as long as date We will continue to iterate in that month
Complete code
import java.time.DayOfWeek;
import java.time.LocalDate;
/**
* @author JKC
* @Description:
* @date 2022/7/1 10:53
*/
public class Making a calendar {
public static void main(String[] args) {
// Create a date object , And initialize
LocalDate date = LocalDate.now();
System.out.println(date);
// Get the current month and date
int month = date.getMonthValue();
int today = date.getDayOfMonth();
// take date Set to the first day of the month , And get the day of the week
date = date.minusDays(today - 1);
// Set to DayOfWeek Object of type . Calling this object getValue Method to get a value of the day of the week
DayOfWeek weekday = date.getDayOfWeek();
int value = weekday.getValue(); // 1 = Monday 7 = Sunday
System.out.println("Mon Tue Wed Thu Fri Sat Sun");
for (int i = 1; i < value; i++)
System.out.print(" ");
while (date.getMonthValue() == month) {
System.out.printf("%3d", date.getDayOfMonth());
if (date.getDayOfMonth() == today)
System.out.print("*");
else
System.out.print(" ");
date = date.plusDays(1);
if (date.getDayOfWeek().getValue() == 1)
System.out.println();
}
if (date.getDayOfWeek().getValue() != 1)
System.out.println();
}
}Conclusion
The focus of the above example program is to show how to use the interface of a class to complete quite complex tasks , Without knowing the implementation details
LocalDate API
static LocalDate now() Construct an object representing the current date static LocalDate of(int year, int month, int day) Construct an object representing a given date int getYear() int getMonthValue() int getDayOfMonth() Get the year of the current date 、 Month and day . DayOfWeek getDayOfWeek Get what day of the week the current date is , As DayOfWeek An instance of class returns . call getValue To get 1~7 A number between , This is the day of the week ,1 For Monday ,7 Means Sunday LocalDate plusDays(int n) LocalDate minusDays(int n) Generated after or before the current date n The date of day
边栏推荐
猜你喜欢

Research progress of data traceability based on the perspective of data element circulation

Redis夺命十二问,你能扛到第几问?

Pikachu target clearance and source code analysis

分布式事务 :可靠消息最终一致性方案

YOLOV3

nodejs启动mqtt服务报错SchemaError: Expected `schema` to be an object or boolean问题解决

Applet page generation link sent by SMS

Tid-mop: a comprehensive framework for security management and control under the scenario of data exchange

基于数据要素流通视角的数据溯源研究进展

IP Core: PLL
随机推荐
融合聚类信息的技术主题图可视化方法研究
Super super super realistic digital people! Keep you on the air 24 hours a day
对“DOF: A Demand-oriented Framework for ImageDenoising“的理解
Markdown writing platform
MySQL - Multi version concurrency control (mvcc)
2022/7/25 考试总结
Solve page refresh without attaching data
Find and locate commands
Nodejs学习资源
Eight common SQL misuses of MySQL, all of which I have learned
【Redis】② Redis通用命令;Redis 为什么这么快?;Redis 的数据类型
[redis] ② redis general command; Why is redis so fast?; Redis data type
基于网络分析和文本挖掘的意见领袖影响力研究
C语言 预处理详解
什么是 Web3 游戏?
白蛋白纳米粒表面修饰低分子量鱼精蛋白LMWP/PEG-1900修饰牛血清白蛋白制备研究
IP Core: PLL
HNOI2012矿场搭建
Wechat applet dynamic style | parameter transfer
After using MQ message oriented middleware, I began to regret