Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: getting current Day of the Week value

At the moment, I'm creating a java schedule app and I was wondering how to find the current day of the week while using the Calendar class to get the first day of the week.

Here is the method I'm using in a class in order to set up a week schedule

public static String getSunday() {
    SimpleDateFormat d = new SimpleDateFormat(dayDate);
    Calendar specific = cal;
    specific.add(Calendar.DAY_OF_YEAR, (cal.getFirstDayOfWeek() - ??));
    return d.format(specific.getTime());
}
like image 636
TLM Avatar asked Mar 11 '10 17:03

TLM


People also ask

How do I get the day of the week in java?

To get the day of the week, use Calendar. DAY_OF_WEEK.

How do I get today's date in java?

The now() method of LocalDate class The now() method of the Localdate class returns the Date object representing the current time.

What is now () in java?

The now() method of the LocalTime class in Java is used to get the current time from the system clock in the default time-zone.


1 Answers

You can get the current day of the week by calling get() with Calendar.DAY_OF_WEEK

public int getTodaysDayOfWeek() {
  final Calendar c = Calendar.getInstance();
  return c.get(Calendar.DAY_OF_WEEK);
}

Also, while I'm not sure I understand exactly what you're trying to do, it looks fishy to me. (What's cal for example?)

like image 74
Pointy Avatar answered Oct 20 '22 01:10

Pointy