0

I need to set some days in method set. I try to use:

c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
c.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);

but with this way set only Wednesday.

Thank you and sorry for my english :)

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Maxim
  • 123
  • 2
  • 7
  • 1
    Could you provide more info on what you intend to do? `java.util.Calendar` is nothing like a Google Calendar or similar, it just represents a given datetime (maybe that is the origin of the confussion). – SJuan76 Aug 17 '13 at 22:50

2 Answers2

1

The Calendar does not function as you expect it to. From the JavaDoc:

The Calendar class is an abstract class that provides methods for converting between a specific instant in time and a set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, and so on, and for manipulating the calendar fields, such as getting the date of the next week. An instant in time can be represented by a millisecond value that is an offset from the Epoch, January 1, 1970 00:00:00.000 GMT (Gregorian).

Notice that the documentation states a specific instant in time. This implies the Calendar can only be based off of one point in time from epoch.

When you use the set method you are adjusting the specific instant in time through each call. So first it gets set to Monday then Wednesday.

You could use a List<Calendar> to store multiple Calendar instances set to your desired days.

public class CalendarTest {
    public static void main(String[] args) {
        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        cal1.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        cal2.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);

        List<Calendar> calendars = Arrays.asList(cal1, cal2);
    }
} 
Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
0
public static String getDay(String day,String month,String year){

    int mm = Integer.parseInt(month);
    int dd = Integer.parseInt(day);
    int yy = Integer.parseInt(year);
    LocalDate dt = LocalDate.of(yy, mm, dd);
    return dt.getDayOfWeek().toString().toUpperCase();
}
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Thank you for demonstrating the use of the modern `LocalDate` class instead of the outdated and poorly designed `Calendar`. As I read the question, you didn’t quite hit what was asked about, though. – Ole V.V. Sep 15 '18 at 09:17