Calendar currDate = Calendar.getInstance();;
currDate.add(Calendar.DAY_OF_YEAR, (Calendar.SATURDAY - currDate.get(Calendar.DAY_OF_WEEK) ));
System.out.println("weekend date is in the " + curreDate.get(Calendar.DAY_OF_MONTH));
Calendar currDate = Calendar.getInstance();;
currDate.add(Calendar.DAY_OF_YEAR, (Calendar.SATURDAY - currDate.get(Calendar.DAY_OF_WEEK) ));
System.out.println("weekend date is in the " + curreDate.get(Calendar.DAY_OF_MONTH));
Calendar cal = Calendar.getInstance();
cal.set(Calendar.MONTH, cal.MONTH);
cal.set(Calendar.WEEK_OF_MONTH, cal.WEEK_OF_MONTH);
int weekStart = cal.getFirstDayOfWeek();
cal.set(Calendar.DAY_OF_WEEK,weekStart);
Date WeekStartDate=cal.getTime();
cal.set(Calendar.DAY_OF_WEEK,weekStart+7);
Date WeekEndDate=cal.getTime(
Try this. It will give both the start and end of week days.
Calendar cal = Calendar.getInstance();
cal.set(Calendar.MONTH, cal.MONTH);
cal.set(Calendar.WEEK_OF_MONTH, cal.WEEK_OF_MONTH);
int weekStart = cal.getFirstDayOfWeek();
cal.set(Calendar.DAY_OF_WEEK,weekStart);
Date WeekStartDate=cal.getTime();
cal.set(Calendar.DAY_OF_WEEK,weekStart+7);
Date WeekEndDate=cal.getTime(
LocalDate.now( ZoneId.of( "Pacific/Auckland" ) ) // Today, in specific time zone.
.with( TemporalAdjusters.nextOrSame( DayOfWeek.SATURDAY ) ) // Next Saturday, or today if already Saturday.
.plusDays( 1 ) // Sunday
LocalDate.now( ZoneId.of( "Pacific/Auckland" ) ) // Today, in specific time zone.
.with( TemporalAdjusters.nextOrSame( DayOfWeek.SATURDAY ) ) // Next Saturday, or today if already Saturday.
.plusDays( 1 ) // Sunday
java.time
The modern approach uses the java.time classes that supplant the troublesome poorly-designed Date & Calendar classes.
Current date
The LocalDate class represents a date-only value without time-of-day and without time zone.
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.
Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or 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(!).
ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( z );
Moving to the next weekend
You can adjust from one date to another using an implementation of the TemporalAdjuster interface. Some handy implementations are provided in the TemporalAdjusters class, such as nextOrSame. Specify a day-of-week using a DayOfWeekenum object. Note that a DayOfWeek is an actual object rather than a mere number or string, providing type-safety and ensuring valid values.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.
发布评论
评论(6)
试试这个:
Try this:
乔达时间
Joda Time
试试这个。它将给出工作日的开始时间和结束时间。
Try this. It will give both the start and end of week days.
tl;dr
java.time
现代方法使用 java.time 类来取代麻烦的、设计不良的
Date
& 。日历
类。当前日期
LocalDate
< /a> 类表示仅日期值,没有时间和时区。时区对于确定日期至关重要。对于任何特定时刻,全球各地的日期都会因地区而异。例如,在法国巴黎午夜过后几分钟,又是新的一天“昨天”在魁北克蒙特利尔。
以
大陆/地区
格式指定正确的时区名称 ,例如America/Montreal
、非洲/卡萨布兰卡
,或太平洋/奥克兰
。切勿使用 3-4 个字母的缩写,例如EST
或IST
,因为它们不是真正的时区,不是标准化的,甚至不是唯一的( !)。转到下一个周末
您可以使用
TemporalAdjuster
接口。中提供了一些方便的实现TemporalAdjusters
类,例如nextOrSame
。使用指定星期几DayOfWeek
enum 对象。请注意,DayOfWeek
是一个实际对象,而不仅仅是数字或字符串,提供 类型安全并确保有效值。如果您想要同一个或上一个周末,请致电
previousOrSame
调整器。关于 java.time
java.time 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧遗留日期时间类,例如
java.util.Date
,日历
, & ;SimpleDateFormat
。Joda-Time 项目,现已在 维护模式,建议迁移到 java.time 类。
要了解更多信息,请参阅 Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范为 JSR 310。
您可以直接与数据库交换java.time对象。使用符合 JDBC 驱动程序 /jeps/170" rel="nofollow noreferrer">JDBC 4.2 或更高版本。不需要字符串,不需要 java.sql.* 类。
从哪里获取 java.time 类?
ThreeTen-Extra 项目通过附加类扩展了 java.time 。该项目是 java.time 未来可能添加的内容的试验场。您可能会在这里找到一些有用的类,例如
间隔
,YearWeek
,<代码>YearQuarter,以及更多 。tl;dr
java.time
The modern approach uses the java.time classes that supplant the troublesome poorly-designed
Date
&Calendar
classes.Current date
The
LocalDate
class represents a date-only value without time-of-day and without time zone.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.
Specify a proper time zone name in the format of
continent/region
, such asAmerica/Montreal
,Africa/Casablanca
, orPacific/Auckland
. Never use the 3-4 letter abbreviation such asEST
orIST
as they are not true time zones, not standardized, and not even unique(!).Moving to the next weekend
You can adjust from one date to another using an implementation of the
TemporalAdjuster
interface. Some handy implementations are provided in theTemporalAdjusters
class, such asnextOrSame
. Specify a day-of-week using aDayOfWeek
enum object. Note that aDayOfWeek
is an actual object rather than a mere number or string, providing type-safety and ensuring valid values.If you want to the same or previous weekend, call the
previousOrSame
adjuster.About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as
java.util.Date
,Calendar
, &SimpleDateFormat
.The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for
java.sql.*
classes.Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as
Interval
,YearWeek
,YearQuarter
, and more.