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];
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).
I am developing a java system that will keep track of a certain company's fixed assets, calculate the depreciating value using the straight line accounting method so the depreciating value will always be constant. after calculating all this it should divide the figure by 12 to get a monthly depreciating value and then alert the user (finance) of the depreciating after every financial year. how can I make the java based app get the time from the local machine to trigger these alerts.
First set the financial year of your app to, for instance, (31/03/2015) and then compare with the current date from your local machine. Check out the code for clarity:
if(getCurrentDate().equals((31/03/2015))||getCurrentDate().after((31/03/2015))){
//then do something here
}
And here is the method to get current date:
public static java.util.Date getCurrentDate() {
int month = GregorianCalendar.getInstance().get(Calendar.MONTH);
int year = GregorianCalendar.getInstance().get(Calendar.YEAR);
int day = GregorianCalendar.getInstance().get(Calendar.DATE);
Calendar calender = Calendar.getInstance();
calender.set(year, month, day);
return calender.getTime();
}
I have this code in my project
Calendar subval = Calendar.getInstance();
final int WOY= subval.WEEK_OF_YEAR;
and when I check it for value of WOY it outputs 3 now it is currently Feb 25 2013 and I know the week number is not three. I am storing this value to help set automatic refresh times so I am able to force a refresh to make sure the device has the most current data. In between refresh periods some crucial data is stored locally. Now I need a reliable fixed time slot and I chose once a week basically if the WEEK OF YEAR is not the same as the stored value for WEEK OF YEAR set data to be refreshed at next opportunity and then store current WEEK OF YEAR on device. I started coding this within 1 week so I have not transitioned to the new week so I am not sure if it working correctly but the value of three scares me.
WEEK_OF_YEAR is a flag used to the get() method. It's value never changes.
You use it like this:
Calendar subval = Calendar.getInstance();
final int WOY= subval.get(Calendar.WEEK_OF_YEAR);
Calendar.WEEK_OF_YEAR is a constant to be used to specify which field to return from your Calendar instance. Instead of assigning the value to the fixed value, you need to call Calendar#get:
int WOY = subval.get(Calendar.WEEK_OF_YEAR);
You need to get it like this
Calendar subval = Calendar.getInstance();
int year=subval.get(Calender.Calendar.WEEK_OF_YEAR);
Calendar.WEEK_OF_YEAR is a static constant.
You need to call calendar.get(Calendar.WEEK_OF_YEAR)
Subclasses define WEEK_OF_YEAR for days before the first week of year according to the java oracle documentation
http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Calendar.html
see: week_of_year
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'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.