jai jai -4 years ago 99
Java Question

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

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download