java 日历 本周的周末

发布于 2024-11-01 20:28:46 字数 34 浏览 7 评论 0 原文

Java使用日历我想获取当前周末的日期,任何快速的想法

Java using calendar i want to get date of current weekend, any quick idea

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

南汐寒笙箫 2024-11-08 20:28:46
Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
c.getTime(); // => Date of this coming Saturday.
Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
c.getTime(); // => Date of this coming Saturday.
能怎样 2024-11-08 20:28:46
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));
戏舞 2024-11-08 20:28:46

试试这个:

String dayNames[]={"lundi","mardi","mercredi","jeudi","vendredi","samedi","dimanche"};
        String nomthNames[]={"janvier","février","mars","avril","mai","juin","juillet","Août","septembre","octobre","novembre","decembre"};
        Calendar date = Calendar.getInstance();
        String dayName = dayNames[date.get(Calendar.DAY_OF_WEEK)];
        String dayMonth = nomthNames[date.get(Calendar.MONTH)];

        lblBonjour.setText("<html><b>Bonjour, "+new Functions().getNomPrenom(myID)+"</b><br>"+
                "Ajourd'hui "+dayName+" "+date.get(Calendar.DATE)+" "+dayMonth+" "+date.get(Calendar.YEAR)+"<br>"+
                "Heure locale: "+date.get(Calendar.HOUR_OF_DAY)+":"+date.get(Calendar.MINUTE) );

Try this:

String dayNames[]={"lundi","mardi","mercredi","jeudi","vendredi","samedi","dimanche"};
        String nomthNames[]={"janvier","février","mars","avril","mai","juin","juillet","Août","septembre","octobre","novembre","decembre"};
        Calendar date = Calendar.getInstance();
        String dayName = dayNames[date.get(Calendar.DAY_OF_WEEK)];
        String dayMonth = nomthNames[date.get(Calendar.MONTH)];

        lblBonjour.setText("<html><b>Bonjour, "+new Functions().getNomPrenom(myID)+"</b><br>"+
                "Ajourd'hui "+dayName+" "+date.get(Calendar.DATE)+" "+dayMonth+" "+date.get(Calendar.YEAR)+"<br>"+
                "Heure locale: "+date.get(Calendar.HOUR_OF_DAY)+":"+date.get(Calendar.MINUTE) );
一抹淡然 2024-11-08 20:28:46

乔达时间

new DateTime().withDayOfWeek(DateTimeConstants.SATURDAY)

Joda Time

new DateTime().withDayOfWeek(DateTimeConstants.SATURDAY)
心作怪 2024-11-08 20:28:46

试试这个。它将给出工作日的开始时间和结束时间。

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(
红尘作伴 2024-11-08 20:28:46

tl;dr

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

现代方法使用 java.time 类来取代麻烦的、设计不良的 Date & 。 日历 类。

当前日期

LocalDate< /a> 类表示仅日期值,没有时间和时区。

时区对于确定日期至关重要。对于任何特定时刻,全球各地的日期都会因地区而异。例如,在法国巴黎午夜过后几分钟,又是新的一天“昨天”在魁北克蒙特利尔

大陆/地区格式指定正确的时区名称 ,例如 America/Montreal非洲/卡萨布兰卡,或太平洋/奥克兰 。切勿使用 3-4 个字母的缩写,例如 ESTIST,因为它们不是真正的时区,不是标准化的,甚至不是唯一的( !)。

ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( z );

转到下一个周末

您可以使用 TemporalAdjuster 接口。 中提供了一些方便的实现TemporalAdjusters 类,例如 nextOrSame。使用 指定星期几DayOfWeek enum 对象。请注意,DayOfWeek 是一个实际对象,而不仅仅是数字或字符串,提供 类型安全并确保有效值。

LocalDate saturday = today.with( TemporalAdjusters.nextOrSame( DayOfWeek.SATURDAY ) ) ;
LocalDate sunday = saturday.plusDays( 1 ) ;

如果您想要同一个或上一个周末,请致电 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

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 DayOfWeek enum object. Note that a DayOfWeek is an actual object rather than a mere number or string, providing type-safety and ensuring valid values.

LocalDate saturday = today.with( TemporalAdjusters.nextOrSame( DayOfWeek.SATURDAY ) ) ;
LocalDate sunday = saturday.plusDays( 1 ) ;

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文