d-man
d-man

Reputation: 58073

java calendar weekend of current week

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

Upvotes: 5

Views: 6198

Answers (6)

Basil Bourque
Basil Bourque

Reputation: 338476

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.

Upvotes: 0

ULLAS K
ULLAS K

Reputation: 901

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(

Upvotes: 0

lschin
lschin

Reputation: 6811

Joda Time

new DateTime().withDayOfWeek(DateTimeConstants.SATURDAY)

Upvotes: 0

MByD
MByD

Reputation: 137312

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

Upvotes: 1

maerics
maerics

Reputation: 156424

Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
c.getTime(); // => Date of this coming Saturday.

Upvotes: 12

Ali Ben Messaoud
Ali Ben Messaoud

Reputation: 11920

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

Upvotes: 0

Related Questions