[英]Get the next date by specifying the day of week
I need to use calendar to do this approach, basicly i get the specific day of week (1,2,3) each int represents a day of week(Monday,Tuesday) not in this order, but the logic is this. 我需要使用日历来执行此方法,基本上我得到了星期几(1、2、3),每个int代表星期几(星期一,星期二)不是按此顺序,但是逻辑是这样。
What i need is to get the date of the next day of week, imagine today is Monday, and the user select Wednsesday, i need to get the date of the next Wednesday. 我需要获取星期几的日期,假设今天是星期一,而用户选择Wednsesday,我需要获取下星期三的日期。
My logic is this at the moment: 我目前的逻辑是:
calendar.set(Calendar.DAY_OF_WEEK, dayOfWeek);
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
dateMatch = calendar.getTime();
day of week is passed from a slidePicker, and represents the specific day of week, this DAY_OF_WEEK doesn't work, if i put Wednseday he gives me 6 星期几是从slidePicker传递过来的,代表星期几,如果我把Wednseday放给我6,这DAY_OF_WEEK不起作用
Although your question text only says to find next dayOfWeek
, your code also includes a time of day, in the form of hour
and minute
. 尽管您的问题文本仅说要找到
dayOfWeek
,但您的代码还包括一天中的时间,以hour
和minute
的形式。
Assuming you want the first future occurrence of that combination, ie dayOfWeek
, hour
, and minute
, that means that if today is that dayOfWeek
, you either want today if time of day is later than now, or next week if time of day is earlier than now. 假设你想要的组合的第一个未来发生,即
dayOfWeek
, hour
和minute
,这意味着,如果今天是dayOfWeek
,你要么想如果今天一天的时间晚了,或者下周,如果一天的时间早比现在。
You can do that like this: 您可以这样做:
int dayOfWeek = Calendar.WEDNESDAY;
int hour = 10; // 10 AM
int minute = 0;
Calendar cal = Calendar.getInstance(); // Today, now
if (cal.get(Calendar.DAY_OF_WEEK) != dayOfWeek) {
cal.add(Calendar.DAY_OF_MONTH, (dayOfWeek + 7 - cal.get(Calendar.DAY_OF_WEEK)) % 7);
} else {
int minOfDay = cal.get(Calendar.HOUR_OF_DAY) * 60 + cal.get(Calendar.MINUTE);
if (minOfDay >= hour * 60 + minute)
cal.add(Calendar.DAY_OF_MONTH, 7); // Bump to next week
}
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, minute);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
System.out.println(cal.getTime()); // Prints: Wed May 10 10:00:00 EDT 2017
To get the next Wednesday after today, or stick with today's date if already a Wednesday. 要获得今天之后的下一个星期三,或者如果已经是星期三,则坚持今天的日期。
LocalDate.now( ZoneId.of( "Africa/Tunis" ) )
.with( TemporalAdjusters.nextOrSame( DayOfWeek.WEDNESDAY ) )
Use modern java.time classes that supplanted the troublesome old legacy date-time classes such as Calendar
. 使用现代的java.time类来代替麻烦的旧旧日期时间类,例如
Calendar
。
Use DayOfWeek
enum objects to represent Monday-Sunday. 使用
DayOfWeek
枚举对象表示星期一至星期日。 Use smart objects rather than dumb integers to represent your day-of-week intention. 使用智能对象而不是哑整数来表示您的星期几意图。 Makes your code more self-documenting, ensures valid values, and provides type-safety.
使您的代码更具自记录性,确保有效值,并提供类型安全性。
The LocalDate
class represents a date-only value without time-of-day and without time zone. LocalDate
类表示没有日期和时区的仅日期值。
A time zone is crucial in determining a date. 时区对于确定日期至关重要。 For any given moment, the date varies around the globe by zone.
在任何给定时刻,日期都会在全球范围内变化。 For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec .
例如, 法国巴黎午夜过后几分钟是新的一天,而在魁北克蒙特利尔仍然是“昨天”。
If no time zone is specified, the JVM implicitly applies its current default time zone. 如果未指定时区,则JVM隐式应用其当前的默认时区。 That default may change at any moment, so your results may vary.
该默认值可能随时更改,因此您的结果可能会有所不同。 Better to specify your desired/expected time zone explicitly as an argument.
最好将您的期望/期望时区明确指定为参数。
Specify a proper time zone name in the format of continent/region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
. 以
continent/region
的格式指定正确的时区名称 ,例如America/Montreal
, Africa/Casablanca
或Pacific/Auckland
。 Never use the 3-4 letter abbreviation such as EST
or IST
as they are not true time zones, not standardized, and not even unique(!). 切勿使用
EST
或IST
等3-4个字母的缩写,因为它们不是真实的时区,不是标准化的,甚至不是唯一的(!)。
ZoneId z = ZoneId.of( "America/Montreal" ) ;
LocalDate today = LocalDate.now( z ) ;
Use a TemporalAdjuster
implementation such as TemporalAdjusters.nextOrSame
to move to another date. 使用
TemporalAdjuster
实现(例如TemporalAdjusters.nextOrSame
移动到另一个日期。
TemporalAdjuster ta = TemporalAdjusters.nextOrSame( DayOfWeek.WEDNESDAY ) ;
LocalDate nextOrSameWednesday = today.with( ta ) ;
If working with moments, use ZonedDateTime
class rather than the awful Calendar
class. 如果需要处理瞬间,请使用
ZonedDateTime
类而不是糟糕的Calendar
类。 Some idea as above, let the TemporalAdjuster
do the heavy-lifting. 如上所述,让
TemporalAdjuster
做繁重的工作。 But keep in mind that the time-of-day may be altered if that time-of-day is invalid for that new date such as during a Daylight Saving Time (DST) cut-over. 但是请记住,如果该日对新日期无效,例如在夏令时(DST)转换期间,则该日可能会更改 。
ZoneId z = ZoneId.of( "Pacific/Auckland" );
ZonedDateTime zdt = ZonedDateTime.now( z ) ;
TemporalAdjuster ta = TemporalAdjusters.nextOrSame( DayOfWeek.WEDNESDAY ) ;
ZonedDateTime zdtSameOrNextWednesday = zdt.with( ta ) ;
The java.time framework is built into Java 8 and later. java.time框架内置于Java 8及更高版本中。 These classes supplant the troublesome old legacy date-time classes such as
java.util.Date
, Calendar
, & SimpleDateFormat
. 这些类取代了麻烦的旧的旧式日期时间类,例如
java.util.Date
, Calendar
和SimpleDateFormat
。
The Joda-Time project, now in maintenance mode , advises migration to the java.time classes. 现在处于维护模式的Joda-Time项目建议迁移到java.time类。
To learn more, see the Oracle Tutorial . 要了解更多信息,请参见Oracle教程 。 And search Stack Overflow for many examples and explanations.
并在Stack Overflow中搜索许多示例和说明。 Specification is JSR 310 .
规格为JSR 310 。
You may exchange java.time objects directly with your database. 您可以直接与数据库交换java.time对象。 Use a JDBC driver compliant with JDBC 4.2 or later.
使用与JDBC 4.2或更高版本兼容的JDBC驱动程序 。 No need for strings, no need for
java.sql.*
classes. 不需要字符串,不需要
java.sql.*
类。
Where to obtain the java.time classes? 在哪里获取java.time类?
The ThreeTen-Extra project extends java.time with additional classes. ThreeTen-Extra项目使用其他类扩展了java.time。 This project is a proving ground for possible future additions to java.time.
该项目为将来可能在java.time中添加内容提供了一个试验场。 You may find some useful classes here such as
Interval
, YearWeek
, YearQuarter
, and more . 您可以在这里找到一些有用的类,比如
Interval
, YearWeek
, YearQuarter
,和更多 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.