I want to find the day of the week in java without the use of date and other methods that do it on theirselves.I can find the daydifference between 2 dates but I cant understand how I can find which day of the week that specific date is.
If you can find the day difference between two days, then just use the mod operator.
For instance, if you know that day1 = Monday, and you want to find which day it is after 701 days, it is Monday + 701 % 7 = Monday + 1 = Tuesday.
There are formulas for figuring out what day of the week a particular day is on:
http://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week#Purely_mathematical_methods
The approach you've chosen seems extremely complicated and error prone.
I suggest creating your own class named Date.
A date will contain a month, day, and year and will know how to subtract or add days.
It should also be able to determine if it is equal to, before, or after another date.
You should use an array of ints to represent the number of days in each month.
This array can be a private static field of your Date class.
Then you can create an instance of your class that represents date2 and subtract one day at a time until it equals date1.
You can use a similar approach to determine the day of the week by comparing the input dates to a base date with a known day of week.
I'm certain this is what your instructor intended for this assignment.
This will teach you a valuable lesson:
Take advantage of objects to use encapsulation in stead of using a bunch of if statements.
Related
Is there a way to get number of days in a month using time4j lib?
in android default calendar, we can get it so simple like below
Calendar calendar=Calendar.getInstance();
int numOfDaysInMonth=calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
I mean a standard way, not crazy ways like going to the first Day of next month then come back one day and get day of month.
so can we do that in time4j calendars like "PersianCalendar"
The answer of #محمد علی using the default maximum has a problem: It does not use any calendar context so the maximum in leap years cannot be determined for the last month ESFAND. But the old comment given by #Tunaki is already a good and simple answer:
PersianCalendar today = PersianCalendar.nowInSystemTime();
int lengthOfCurrentMonth = today.lengthOfMonth();
Alternatively, you can also use the element PersianCalendar.DAY_OF_MONTH but then you should determine the contextual maximum, not the default maximum:
PersianCalendar today = PersianCalendar.nowInSystemTime();
int lengthOfCurrentMonth = today.getMaximum(PersianCalendar.DAY_OF_MONTH);
Both expressions will yield the same results in all ways and are completely equivalent.
For standard months (FARVARDIN (1) until BAHMAN (11)) the results will agree with the default maximum. But the last month ESFAND has either 29 days in normal years or 30 days in leap years. Both methods presented here will take this into account (but not the default maximum method).
Apologies in advance if I confuse any ISO date related terms here.
I would like to be able to iterate over every week in a given year (say, 2015). I realize that you can calculate the number of weeks between 1/1/2015 and 12/31/2015, but that doesn't conform to the ISO standard of a week. Rather, it gives the number of 7 day periods between two dates. The first ISO week of the year doesn't necessarily start on 1/1/2015.
If I can get the first date of the first week, I believe I can simply iterate via ZonedDateTime.plusWeeks(1) for 52 weeks. You can get the week number of an arbitrary date via the field accessor:
ZonedDateTime date = ZonedDateTime.now();
WeekFields weekFields = WeekFields.of(Locale.getDefault());
int weekNumber = date.get(weekFields.weekOfWeekBasedYear());
Given this, I think it must be possible to get the date of the first day of the first week of a specific year in the Java8 Time API, but I have not found a way to do it yet.
You can construct a date and adjust it to the first day of week for the first week of the year with the following:
int year = 2016;
WeekFields weekFields = WeekFields.ISO;
LocalDate date = LocalDate.now().with(weekFields.weekBasedYear(), year)
.with(weekFields.weekOfWeekBasedYear(), 1)
.with(ChronoField.DAY_OF_WEEK, 1);
Thanks to JodaStephen's comment, another way to put it would be to use the IsoFields class.
LocalDate date = LocalDate.now().with(IsoFields.WEEK_BASED_YEAR, year)
.with(IsoFields.WEEK_OF_WEEK_BASED_YEAR, 1)
.with(ChronoField.DAY_OF_WEEK, 1);
WeekFields.ISO represents the ISO definition of the week:
The ISO-8601 definition, where a week starts on Monday and the first week has a minimum of 4 days.
The ISO-8601 standard defines a calendar system based on weeks. It uses the week-based-year and week-of-week-based-year concepts to split up the passage of days instead of the standard year/month/day.
Note that the first week may start in the previous calendar year. Note also that the first few days of a calendar year may be in the week-based-year corresponding to the previous calendar year.
From that definition, you can get:
weekBasedYear() represents the week-based-year field:
This represents the concept of the year where weeks start on a fixed day-of-week, such as Monday and each week belongs to exactly one year.
In this case, we want to set it to the wanted year.
weekOfWeekBasedYear() represents the week of week-based-year
This represents the concept of the count of weeks within the year where weeks start on a fixed day-of-week, such as Monday and each week belongs to exactly one year.
In this case, we want the first week of the week-based-year so we set it to 1.
ChronoField.DAY_OF_WEEK which represents the day of the week. In this case, we want the first day of the week so we set to 1.
Then, with such a date, you can indeed iterate over all weeks of the year by calling LocalDate.plusWeeks(1). The question is: how many times do you need to iterate? There can be more than 52 weeks in a year. There are either 52 or 53 weeks in a week-based-year.
You can retrieve the number of weeks with the following. This call rangeRefinedBy(date) to retrieve the valid values of the week of year field for the given date, and get its maximum.
long maxWeekOfYear = weekFields.weekOfWeekBasedYear().rangeRefinedBy(date).getMaximum();
I am trying to determine the sequential ordinal number of a weekday in a month in Java. i.e. if a Friday is the first or 3rd friday of a month.
I can not find a simple way after reading all the things I can find on Java Calendar and posts here. One way I can think of is to determine how many days the first week of this month have in this month and then adjust week_of_month based on what day the day in question is. However, it requires a little complicated calculation. Anyone knows a simple solution?
Just take the day of month, subtract 1, divide by 7, then add 1. The first seven days of the month are always the first (Tuesday, Wednesday, ...) whatever day of the week the actual 1st of the month is.
Personally I'd use Joda Time:
public int getWeekOfWeekDay(LocalDate date) {
return ((date.getDayOfMonth() - 1) / 7) + 1;
}
... but you could do the same using Calendar and fetching the value of the Calendar.DAY_OF_MONTH field.
EDIT: Actually, I've just noticed that for a change, java.util.Calendar is actually simpler than Joda Time - there's a particular field for it! All you need is:
int weekOfWeekDay = calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH);
From the docs for DAY_OF_WEEK_IN_MONTH:
Field number for get and set indicating the ordinal number of the day of the week within the current month. Together with the DAY_OF_WEEK field, this uniquely specifies a day within a month. Unlike WEEK_OF_MONTH and WEEK_OF_YEAR, this field's value does not depend on getFirstDayOfWeek() or getMinimalDaysInFirstWeek(). DAY_OF_MONTH 1 through 7 always correspond to DAY_OF_WEEK_IN_MONTH 1; 8 through 14 correspond to DAY_OF_WEEK_IN_MONTH 2, and so on.
I think I'd probably still use the Joda Time version because it's just a much nicer API all round, but if you're forced to use Calendar, at least you can do this in one shot.
So this issue I believe is simple enough that I can just give you three lines of code and the problem is likely my understanding of the date class.
public DrawCalendar(GregorianCalendar date){
date.setFirstDayOfWeek(GregorianCalendar.THURSDAY);
System.out.print(date.get(GregorianCalendar.DAY_OF_WEEK));
My problem is that I BELIEVE I'm adjusting the starting day of the week, so today, by default is the 6th day of the week. If I change this to a random day or integer like THURSDAY I'm still getting that today is the 6th day of the week even when thursday is the 1st day of the week.
That is what I BELIEVE is happening, why would the print statement not be returning a different number based on the first day of the week which was adjusted just one line before it?
Use getFirstDayOfWeek() method
System.out.println(calObj.getFirstDayOfWeek());
setFirstDayOfWeek typically is SUNDAY or MONDAY and influences the WEEK_OF_YEAR number. The DAY_OF_WEEK is simply MONDAY, TUESDAY, ...
I'm pretty sure it's because:
date.setFirstDayOfWeek(GregorianCalendar.THURSDAY);
Is used to tell the class which day of the week should be used for date calculations involving weeks, while
date.get(GregorianCalendar.DAY_OF_WEEK);
Is getting the day of the week that the current object represents in time (since the current object represents a specific point in time).
I am looking for an external JAR or a method that will supply me the following abilities:
Subtract two dates.
Define holidays as Saturday and Sunday, or Friday and Saturday.
Calculate the difference with holidays or without them.
Can anyone recommend an external JAR before I go into Gregorian calendar calculations?
Take a look at this site, they provide you a java program that helps you with that:
The wdnum() method returns the number of weekdays (excluding weekends) that have passed since Monday, 29 December 1969. It works by calculating the number of days since 1 January, 1970 (getTime() divided by the number of milliseconds in a day), adding 3 and returning the number of week days in full weeks and possibly a partial week that have passed since then.
Have a look at Joda Time it may be helpful with what you're trying to do.
However, more than likely you're going to have to use the methods in the Calendar class such as getTimeInMillis() to subtract the dates and fields in the Calendar class such as Calendar.DAY_OF_WEEK or Calendar.DAY_OF_MONTH to determine the day of the week if you want to exclude certain days of the week from your calculations.