Java using calendar i want to get date of current weekend, any quick idea
Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
c.getTime(); // => Date of this coming Saturday.
Calendar
class seen here. –
Amenra 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));
DAY_OF_XXXXX
. Awesome!! +1 –
Expediency Calendar
class seen here. –
Amenra 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) );
Joda Time
new DateTime().withDayOfWeek(DateTimeConstants.SATURDAY)
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?
- 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.
© 2022 - 2024 — McMap. All rights reserved.