简体   繁体   中英

Compare two strings to Date in java

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...

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

}

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

Then it should be

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

}

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 .

Alternatively:

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

But it is slower.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM