0

I have tried a lot to search the relevant code on net but what I get so far is

    Calendar cal = Calendar.getInstance();
    cal.setTime(new Date(1996,2,2));
    int d = cal.get(Calendar.DAY_OF_MONTH);
    System.out.println("day: "+d);

Please help!

  • Can you give an example out put? I am quite sure what are you trying.. – Ruchira Gayan Ranaweera Nov 05 '20 at 08:38
  • It's like printing the day of each year since you were born – Waseem Khan Nov 05 '20 at 08:39
  • What you got so far is wrong, since you should be using the [**`LocalDate`**](https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html) class, not `Calendar` and not `Date`. – Andreas Nov 05 '20 at 08:39
  • For example if my date is 1999-2-2 so the day of my birth day in 1999 is Tuesday and in 2000 the birth day is wednesday and so on.... – Waseem Khan Nov 05 '20 at 08:41
  • @Andreas will you please provide the code ? – Waseem Khan Nov 05 '20 at 08:42
  • Seems like university homework/task to do ;) Try to 'divide' every aspect of a 'problem'/requirement that You have, in order to gain better understanding what kind of operations/things You need to do ( computer in next step ) to get You those names ;) – Fincio Nov 05 '20 at 08:43
  • @WaseemKhan Ruchira asked you to show **example** output. Explaining output isn't a display of example output. E.g. should output be just `TUESDAY, WEDNESDAY, FRIDAY, SATURDAY, SUNDAY, MONDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, ...`, or what? Please **edit** the question and show example output. – Andreas Nov 05 '20 at 08:43
  • @WaseemKhan No, I will not just provide the code. Writing the code is an assignment given to *you*, not me. See: [Open letter to students with homework problems](https://softwareengineering.meta.stackexchange.com/questions/6166/open-letter-to-students-with-homework-problems) – Andreas Nov 05 '20 at 08:44
  • You can also have a look at different 'date formatting' options - who knows, it might help ;) – Fincio Nov 05 '20 at 08:46
  • @Andreas I have written the code but the output is not what i exactly need. I need the output like this Year Day 1999 Tuesday 2000 Wednesday and so on.... – Waseem Khan Nov 05 '20 at 08:47
  • @WaseemKhan Since you still haven't shown us **in the question** what output you want, and you haven't shown us any output you actually get, and you haven't shown any code that actually iterates through the years, how did you expect us to help you get it right? – Andreas Nov 05 '20 at 08:50
  • Why do you believe that `get(Calendar.DAY_OF_MONTH)` has anything to do with printing e.g. `Tuesday`, which is a day-of-week, not a day-of-month? – Andreas Nov 05 '20 at 08:51
  • @Andreas I got the answer thanks :) – Waseem Khan Nov 05 '20 at 08:55
  • 1
    Does this answer your question? [Calculating the difference between two Java date instances](https://stackoverflow.com/questions/1555262/calculating-the-difference-between-two-java-date-instances) – tania Nov 05 '20 at 12:38

2 Answers2

1

You should not be using the old out-dated Calendar and Date classes. Use LocalDate instead.

Since the question still doesn't show the desired output, we can only guess, but something like this should do it.

LocalDate birthday = LocalDate.of(1999, 2, 2);
System.out.println("Birthday: " + birthday.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG)));

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu: EEEE");
int thisYear = Year.now().getValue();
for (LocalDate date = birthday; date.getYear() <= thisYear; date = date.plusYears(1))
    System.out.println(date.format(formatter));

Output

Birthday: February 2, 1999
1999: Tuesday
2000: Wednesday
2001: Friday
2002: Saturday
2003: Sunday
2004: Monday
2005: Wednesday
2006: Thursday
2007: Friday
2008: Saturday
2009: Monday
2010: Tuesday
2011: Wednesday
2012: Thursday
2013: Saturday
2014: Sunday
2015: Monday
2016: Tuesday
2017: Thursday
2018: Friday
2019: Saturday
2020: Sunday

To make it correctly handle someone who is born on the leap day (Feb 29), you might do it like this instead:

LocalDate birthday = LocalDate.of(2000, 2, 29);

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM d, uuuu: EEEE");
int thisYear = Year.now().getValue();
for (int year = birthday.getYear(); year <= thisYear; year++)
    System.out.println(birthday.withYear(year).format(formatter));

Output

Feb 29, 2000: Tuesday
Feb 28, 2001: Wednesday
Feb 28, 2002: Thursday
Feb 28, 2003: Friday
Feb 29, 2004: Sunday
Feb 28, 2005: Monday
Feb 28, 2006: Tuesday
Feb 28, 2007: Wednesday
Feb 29, 2008: Friday
Feb 28, 2009: Saturday
Feb 28, 2010: Sunday
Feb 28, 2011: Monday
Feb 29, 2012: Wednesday
Feb 28, 2013: Thursday
Feb 28, 2014: Friday
Feb 28, 2015: Saturday
Feb 29, 2016: Monday
Feb 28, 2017: Tuesday
Feb 28, 2018: Wednesday
Feb 28, 2019: Thursday
Feb 29, 2020: Saturday
Andreas
  • 154,647
  • 11
  • 152
  • 247
0

You can get an idea from below.

You need to refer getDisplayName().

    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.YEAR,1996);
    cal.set(Calendar.MONTH,2);
    cal.set(Calendar.DATE,2);
    String d = cal.getDisplayName(Calendar.DAY_OF_WEEK,Calendar.LONG, Locale.US);
    System.out.println("day: "+d);

Output:

day: Saturday
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115