I'm working on a project that is going to require our own custom calendar view. I've been trying to figure out the best way to approach this.
I was considering possibly using a Master xml file that will define the basic layout of the calendar, and then use a secondary xml file, and nest it into each cell of the calendar for each day as an array of objects.
Not exactly sure if this is possible, or if this would be the best way to approach this problem?
Cheers
I recently created a Month layout, using TableLayout.
I took into consideration that you will need 6x7 days for a month to be able to handle every possible situation. (First day of the month being a sunday, last day of the month being a monday etc.)
Based on a given date (lets say 3rd of August) I calculate the first day to be shown
date = 3rd of August
firstDate = first day of month based on date
while( firstDate is not a monday )
firstDate = present date
I then calculate the last day to be shown:
lastDate = last day of month based on date
while( lastDate is not a Sunday )
lastDay = following date
This gives me an interval of dates from firstDate to lastDate
Then I programaticly create 6 TableRow's in which there are 7 days - a TextView or whatever. It could just be declared in an XML-file if you don't want to create too much layout on the fly. One thing to remember is to set the layout_weight for the TextViews so that they are all equally big/small to create a nice grid.
If what you need is more like a Day- or Weeklayout the challenge is a little tougher.
Android has a util for doing all the math to find the days in a month. I guess using a gridview with android.util.MonthDisplayHelper should work.
Related
This question already has answers here:
Get first date of current month in java
(11 answers)
Closed 1 year ago.
Im working on a project that visually displays statistical data from each month and week and i dont know how to elegantly renew the dates of each week and month.
For example, i want to display this months data. I have to make two date variables in Java. The first one being 1.5.2021 and the second one being todays date. I dont know how to elegantly set the first variable to 1.x.xxxx without making a string out of the current date first and then cuting it, doing some numerics with it and merging it back together to 1.5.2021.
Same goes for weekly statistics where i need the Monday date and the Sunday date, for example today being Monday 10.5.2021 and the end on 17.5.2021.
So my idea is to get current date to string format, slice it, convert it to int, calculate the desired dates and the put it back to string(no need to go back to datetype since its gonna be used for querying).
Well, you should definitely take a look at the classes within the java.time package.
In your case, your current date could be a LocalDate instance, for example with the following line:
LocalDate.of(2021, 5, 1);
And then you could just use the withDayOfMonth method to get a new LocalDate instance with the day of the month set to 1:
LocalDate currentDate = LocalDate.now();
LocalDate firstOfMonth = currentDate.withDayOfMonth(1);
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).
So I'm working on an application that needs to display a value, a specific string of text once per day. I have all my strings in an array and now I just need a way to increase the index once per day. The kicker is that if the user downloads the application later in the year I need to have all the other days accounted for. So basically the user will see the same tip as a person who downloaded the app on the first day. Any suggestions?
Would using the Calendar class be my best bet? I just don't want to set an individual switch and case for every day of the year.
Not sure if I understand completely the question. I think you will need to have the same tip shown for everyone in each day, right?
If so, you can use Calendar.DAY_OF YEAR :
Calendar cal = Calendar.getInstance();
int index=cal.get(Calendar.DAY_OF_YEAR);
if I understand you correctly (Bob downloaded the app a hundred days ago should see the same tip as Alice who downloaded the application today), you can use Calendar's DAY_OF_YEAR value to display the same date
Calendar ca1 = Calendar.getInstance(); //get today's date
int DAY_OF_YEAR=ca1.get(Calendar.DAY_OF_YEAR) -1; //DAY_OF_YEAR starts at one
//avoid IndexOutOfBoundsExceptions
String tip = tiparray[DAY_OF_YEAR % tiparray.length];
I have a datepicker where the user selects a date and then a checkbox on what type of period he wants to get the date from. For example:
User selects the 1. of November and selects the checkbox "Month" in this case the end date will be increased by 1 and even if this sound simple enough its slowly starting to annoy me alot!
The problem is that Java doesnt have a great date object that works for this kind of thing so i thought that i would use Calendar but it isnt easy to increment a calendar date take for instance the following example:
endDate.set(startDate.YEAR, startDate.MONTH+1, startDate.DATE);
in theory this would increment the month by one being one larger than the start date. This works in about 90 % of the months EXECPT from December if you increase the month by 1 in December then the integer month return 13 same thing happens for startDate.DATE; and startDate.Year;
My question is isnt there an easier way to do this? i could make a ton of If sentences but i really think that it is kinda redundant.
Use add method of java.util.Calendar.
endDate.set(startDate.YEAR, startDate.MONTH, startDate.DATE);
if(some_condition) {
endDate.add(Calendar.MONTH, 1);
}
You can use Calendar.add() to add values to the calendar value, e.g. Calendar.add(Calendar.MONTH, 1) this adds one month and takes into account that January is after December.
The standard recomendation here is to look at Joda-Time (see here for more info). It's a much more consistent/capable API with none of the threading issues that plague the standard Java date/formatting APIs and as such is widely used and accepted.
In terms of what you want above, I would suggest something like:
LocalDate d = ...
LocalDate nd = d.plusMonths(1);
The above will correctly handle month/year rollovers.
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.