Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare two strings to Date in java

Tags:

java

date

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

like image 559
Jay Avatar asked Jun 20 '26 01:06

Jay


2 Answers

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

}
like image 141
Florent Bayle Avatar answered Jun 21 '26 16:06

Florent Bayle


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

Then it should be

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

}
like image 37
Suresh Atta Avatar answered Jun 21 '26 15:06

Suresh Atta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!