简体   繁体   中英

java calendar weekend of current week

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

Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
c.getTime(); // => Date of this coming Saturday.
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));

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(

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 .

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) );

乔达时间

new DateTime().withDayOfWeek(DateTimeConstants.SATURDAY)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM