找出第一个周日/周一每月的日期等 [英] find out the date for first Sunday/Monday etc of the month
本文介绍了找出第一个周日/周一每月的日期等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要检测第一个星期日/第一/第二周的星期一的日期在java中每月?
我怎样才能实现呢?
I want to detect date of First Sunday/Monday of first/second week in every month in java? How can I achieve it?
我已经检查Calendar类以及Java中日期类,但没能得到该解决方案,请帮我出这一点。
I have checked Calendar class as well as Date class in java but not able to get solution for that so please help me out for this.
推荐答案
使用内置到Java 8 java.time
库:
Java.time
Using java.time
library built into Java 8:
import java.time.{DayOfWeek, LocalDate}
import java.time.temporal.TemporalAdjusters.firstInMonth
val now = LocalDate.now() # 2015-11-23
val firstMonday = now.`with`(firstInMonth(DayOfWeek.MONDAY)) # 2015-11-02 (Monday)
您可以选择从 java.time.DayOfWeek任何一天。{周一,周二,周三,周四,周五,周六,周日}
。
如果您需要添加时间信息,你可以使用任何可用的 LOCALDATE
到 LocalDateTime
转换像
If you need to add time information, you may use any available LocalDate
to LocalDateTime
conversion like
firstMonday.atStartOfDay() # 2015-11-02T00:00
这篇关于找出第一个周日/周一每月的日期等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文