I see someone has already mentioned Java8 before...but this way of writing is a bit awkward...Everyone has basically the same idea...
The start time given should be changed to the first day of the current month
The end time given should be changed to the last day of the current month
The key is how to change... There are ready-made APIs that can be called in Java 8... There is no need for parse... It will be okay if it becomes the first day, but there will be many judgments if it becomes the last day. ..
The code is as follows. According to the given conditions, an isBetween method is written
A brief explanation... yyyy-mm This form of year and month has been processed by a new class in Java8. This is the with method of the interface. This involves the design of the new time API of Java8 and the signature of the with method. As follows YearMonth(它是一个TemporalAdjuster的实现类),根据LocalDate(它是一个Temporal的实现类)的with方法,其实是Temporal
means: an object to adjust Temporal对象可以根据一个TemporalAdjuster
So combined with the code written above, the idea is as follows
I randomly took the current time
ALocalDate
Modify A according to the incoming
(call the with method), that is, adjust the year and month of the time to get time BYearMonth
Put B according to an adjuster
), adjust the day of the time, and get the final timeTemporalAdjuster(TemporalAdjusters.firstDayOfMonth
In fact, many commonly used time adjustments have been encapsulated in
... It is also simple, straightforward and easy to use. You can pay attention to this classTemporalAdjusters
Finally, I provide some relationships between the Java 8 new time API that I simply connected before. Following these relationships, you can look at the specific classes and relationships. You will find that the new time API is not only easy to use but also very powerful.
end should be converted to the last day of this month, for example 2017-06-30;
If the format is determined to be yyyy-mm或yyyy-mm-dd,那么用date.compareTo(start) >= 0 && date.compareTo(end) < 0, that’s it;
If there is a non-standard format, such as 2017-4-6, either convert it to a standard format, or convert it to Date or long (Date is essentially long), and then compare.
If you are not using Java8:
I see someone has already mentioned Java8 before...but this way of writing is a bit awkward...Everyone has basically the same idea...
The start time given should be changed to the first day of the current month
The end time given should be changed to the last day of the current month
The key is how to change... There are ready-made APIs that can be called in Java 8... There is no need for parse... It will be okay if it becomes the first day, but there will be many judgments if it becomes the last day. ..
The code is as follows. According to the given conditions, an isBetween method is written
A brief explanation...
yyyy-mm This form of year and month has been processed by a new class in Java8. This is the with method of the interface. This involves the design of the new time API of Java8 and the signature of the with method. As follows
YearMonth
(它是一个TemporalAdjuster
的实现类),根据LocalDate
(它是一个Temporal
的实现类)的with方法,其实是Temporal
means: an object to adjust
So combined with the code written above, the idea is as followsTemporal
对象可以根据一个TemporalAdjuster
A
LocalDate
(call the with method), that is, adjust the year and month of the time to get time B
YearMonth
), adjust the day of the time, and get the final time
TemporalAdjuster
(TemporalAdjusters.firstDayOfMonth
... It is also simple, straightforward and easy to use. You can pay attention to this class
Finally, I provide some relationships between the Java 8 new time API that I simply connected before. Following these relationships, you can look at the specific classes and relationships. You will find that the new time API is not only easy to use but also very powerful.TemporalAdjusters
If you are using Java8:
If you are not using Java8 but are using Joda-time:
end should be converted to the last day of this month, for example
2017-06-30
;If the format is determined to be
yyyy-mm
或yyyy-mm-dd
,那么用date.compareTo(start) >= 0 && date.compareTo(end) < 0
, that’s it;If there is a non-standard format, such as
2017-4-6
, either convert it to a standard format, or convert it to Date or long (Date is essentially long), and then compare.