3

I have one date and i have to check whether it was saturday or sunday. Am i proceeding right way ??

Calendar gcal = new GregorianCalendar();
DateFormat dateFormat = new SimpleDateFormat("EEEE");
        Date currentDate = gcal.getTime();
        String strDate = dateFormat.format(currentDate);
        if (!"Saturday".equals(strDate)) {
}

its working fine. but i cant compare two string like,

if (!"Saturday" || "Sunday".equals(strDate)) {}

If a date was Saturday or sunday i have to skip the loop.... Thanks in advance...

Jay
  • 199
  • 1
  • 14
  • 3
    You should be using JDK 8 and the java.time package. GregorianCalendar is Java 1.0 vintage. – duffymo Feb 27 '17 at 15:33
  • Possible duplicate of [String.equals() with multiple conditions (and one action on result)](http://stackoverflow.com/questions/10208052/string-equals-with-multiple-conditions-and-one-action-on-result) – Tom Feb 27 '17 at 15:38
  • The operands for `!` and `||` and the other logical operators must be `boolean`. You can't just negate a `String`! – Lew Bloch Feb 27 '17 at 15:51
  • 1
    See similar Questions: [How to skip weekends while adding days to LocalDate in Java 8?](http://stackoverflow.com/q/33942544/642706), and [How can I add business days to the current date in Java?](http://stackoverflow.com/q/1534804/642706) – Basil Bourque Feb 27 '17 at 19:13

4 Answers4

7

No need to create/format a Date object, use Calendar methods:

Calendar gcal = new GregorianCalendar();

if (gcal.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY && gcal.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {

}
Florent Bayle
  • 11,520
  • 4
  • 34
  • 47
  • 1
    the correct way to check the day of the week +1 for that! – ΦXocę 웃 Пepeúpa ツ Feb 27 '17 at 15:36
  • Gotcha .. Thanks @Florent Bayle – Jay Feb 27 '17 at 15:56
  • FYI, the troublesome old date-time classes such as [`java.util.Calendar`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html) are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes. – Basil Bourque Feb 27 '17 at 18:43
3

If a date was Saturday or sunday i have to skip the loop.

Then it should be

if (!("Saturday".equals(strDate) || "Sunday".equals(strDate)) {

}
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • 2
    Although, this does address the OP's question.. the proper way to check day of week would be to use constants in the GC class. – ninnemannk Feb 27 '17 at 15:40
1

tl;dr

Is today a Saturday?

LocalDate.now( ZoneId.of( "America/Montreal" ) )
         .getDayOfWeek()
         .equals( DayOfWeek.SATURDAY )

Details

Am i proceeding right way ??

No. You are using the troublesome old date-time classes that have been supplanted by the java.time classes.

Another problem is relying implicitly on default time zone. Better to specify your intended time zone explicitly. Time zone determines the date, and date determines the day-of-week, so time zone is crucial.

And another problem is that you are needlessly converting from a Calendar to a Date. But better to avoid these classes entirely.

DayOfWeek

The DayOfWeek enum defines seven objects, one for each day of the week.

You should be passing these objects around your code rather than a string. Notice in the code below that we do not use strings at all.

Be clear that these DayOfWeek objects are not strings. They are real objects, offering several methods. Those methods include toString that generates a hard-coded String in English, all in uppercase. The method getDisplayName generates the name of the day-of-week automatically localized in various human languages.

Enums in Java are much more powerful and practical than conventionally seen in other languages. See Oracle Tutorial.

LocalDate

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

today.toString(): 2017-02-27

Interrogate the LocalDate object for its DayOfWeek.

DayOfWeek dow = today.getDayOfWeek();

dow.toString(): MONDAY

Compare to your target day-of-week.

Boolean isTodaySaturday = dow.equals( DayOfWeek.SATURDAY );

isTodaySaturday.toString(): false

Try this code live at IdeOne.com.

See similar Question: How to skip weekends while adding days to LocalDate in Java 8?


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.

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.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

Alternatively:

if (!strDate.matches("Saturday|Sunday")) {
}

But it is slower.

john16384
  • 7,800
  • 2
  • 30
  • 44