Java using calendar i want to get date of current weekend, any quick idea
-
2I assume you mean in the western Calendar? The weekend is Thursday/Friday, or only Sunday in some places. – Peter Lawrey Apr 15 '11 at 07:51
-
Current means coming? or the one just passed? – Adeel Ansari Apr 15 '11 at 07:56
-
possible duplicate of [java example to get all weekend dates in a given month](http://stackoverflow.com/questions/3272454/java-example-to-get-all-weekend-dates-in-a-given-month) – Basil Bourque Sep 27 '14 at 23:51
6 Answers
Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
c.getTime(); // => Date of this coming Saturday.

- 151,642
- 46
- 269
- 291
-
-
Now outdated. The java.time classes supplant the troublesome old `Calendar` class seen here. – Basil Bourque Jul 09 '17 at 16:19
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));

- 135,866
- 28
- 264
- 277
-
I love the way you are coming from. And the use of all `DAY_OF_XXXXX`. Awesome!! +1 – Adeel Ansari Apr 15 '11 at 08:06
-
Now outdated. The java.time classes supplant the troublesome old `Calendar` class seen here. – Basil Bourque Jul 09 '17 at 20:47
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(
-
Now outdated. The java.time classes supplant the troublesome old Calendar class seen here. – Basil Bourque Jul 09 '17 at 20:47
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?
- Java SE 8, Java SE 9, and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and Java SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
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.

- 303,325
- 100
- 852
- 1,154
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) );

- 11,690
- 8
- 54
- 87
Joda Time
new DateTime().withDayOfWeek(DateTimeConstants.SATURDAY)

- 6,745
- 2
- 38
- 52
-
FYI, the Joda-Time project is now in maintenance mode. Its team advises magrating to the java.time classes built into Java 8 and later. For Java 6 & 7, see the ThreeTen-Backport project. – Basil Bourque Jul 09 '17 at 16:22