java calendar weekend of current week
Asked Answered
J

6

5

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

Judoka answered 15/4, 2011 at 7:48 Comment(3)
I assume you mean in the western Calendar? The weekend is Thursday/Friday, or only Sunday in some places.Pirtle
Current means coming? or the one just passed?Expediency
possible duplicate of java example to get all weekend dates in a given monthAmenra
H
12
Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
c.getTime(); // => Date of this coming Saturday.
Higgle answered 15/4, 2011 at 7:58 Comment(2)
I love it, because here we don't care what day it is today. +1Expediency
Now outdated. The java.time classes supplant the troublesome old Calendar class seen here.Amenra
A
1
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));
Asuncion answered 15/4, 2011 at 7:59 Comment(2)
I love the way you are coming from. And the use of all DAY_OF_XXXXX. Awesome!! +1Expediency
Now outdated. The java.time classes supplant the troublesome old Calendar class seen here.Amenra
Q
0

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) );
Quickman answered 15/4, 2011 at 7:51 Comment(0)
V
0

Joda Time

new DateTime().withDayOfWeek(DateTimeConstants.SATURDAY)
Violet answered 15/4, 2011 at 8:36 Comment(1)
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.Amenra
M
0

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(
Mccarron answered 18/4, 2012 at 11:42 Comment(1)
Now outdated. The java.time classes supplant the troublesome old Calendar class seen here.Amenra
A
0

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.

Amenra answered 9/7, 2017 at 20:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.