how do we get the first and last day of the current month - salesforce

How do we get the first day of the month and last day of the month. i am using it on a apex class.
i can get the todays date by using
date.today()
but how would i get the first and last day of the month.
Thanks

You should get familiar with the Apex Code Developers Guide as it has very good documentation for questions like this. Here's the Date methods page that you would find helpful.
With respect to your specific question, you get the first date of the month (I'll use today's month in my example) using the following
Date firstDayOfMonth = System.today().toStartOfMonth();
To get the last day of the month, use the following:
Date lastDayOfMonth = firstDayOfMonth.addDays(Date.daysInMonth(firstDayOfMonth.year(), firstDayOfMonth.month()) - 1);

I think there is a better way to solve the second problem, that is, accept the last day of the month.
firstDayOfMonth.addMonths(1).addDays(-1);

Related

RRule Repeating monthly calendar event on weekday only

I have an recurring event on the 7th of each month but am struggling to find an RRule that I can put into a text editor and import into google calendar. I need the rule to put the event on the last week day if the 7th falls on a weekend. Thanks in advance.
Due to the fallback condition, I don't think you can achieve this only defining a RRULE, unfortunately.
RRULE in Google Calendar follows RFC 5545 specification.
With RFC 5545, regarding monthly recurrences, you can either set the recurring rule for a specific day of the month (e.g. always on the 7th of the month would be RRULE:FREQ=MONTHLY;BYMONTHDAY=7) or a specific offset of day of the week within a month (e.g. second to last weekday of the month would be RRULE:FREQ=MONTHLY;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=-2).
Only using this specification, I don't believe there is a way to select the previous (or next) weekday in reference to the 7th day of the month.
However you can always achieve this by using a script to generate the dates or interpret an existing list you already have before insert them on Google Calendar.

SSRS Date Expression stopped working

I hope someone can share their experience with me. I've used the following ssrs expression to default a SSRS report parameter to the last day of the current month for months without issue:
=DateAdd(ā€œdā€, -1, DateSerial(Year(Now()), Month(Now()), 1))
This morning instead of returnng 3/31/2016, the expression is stuck on 3/29/2016. Can anyone help me understand why this happened?
You are substracting one day to the first day of the current month.
You have to get the first day of the next month and substract one day.
=DateAdd("d", -1, DateSerial(2016, Month(Now.AddMonths(1)), 1))
It returns 3/31/2016
Let me know if this helps.
The expression you show will give the last day of the previous month, not the last day of this month.
You want to do date arithmetic rather than build dates from scratch. Think about what will happen in December if you are using the current year but adding a month to find the month you need.
The safest way to get the last day of the current month would be to calculate the first day of this month, add one month then subtract a day:
=DateAdd(DateInterval.Day, -1, DateAdd(DateInterval.Month, 1, DateAdd(DateInterval.Day, 1-Day(Today), Today)))
If what you actually want is the last day of the previous month, then that is simple:
=DateAdd(DateInterval.Day, 0-Day(Today), Today)

Codename One - days of month

I am trying to build a week view of the month with customized cells, so the Calendar class won't work for me.
I am having trouble knowing on which day the month starts. For example, January 1st was a Friday. Also, I need to know how many days a given month has.
Without telling me the exact code, can you tell me the variables or methods I could use for this?
Look at the actual code for the Calendar class here: http://github.com/codenameone/CodenameOne/
Just set java.util.Calendar to the right date then use the get method to get the day of the week.

How to get first day of week in c

I am coding little calendar program in c.
I have issue to get first day of week in current locale, i need it to format calendar. For example Sunday - is first day of week in US, but in Europe first day of week is Monday. How can i get this information for current locale? Thank you.
You can create a config file containing a map of locale and first day of the week. Read this config file and create a look up table at the beginning of the program. Refer to this table everytime you are trying to get the first day of the week for a particular locale.

how do i display the current week dates in salesforce?

I am trying to display dates of current week. How can i do it? What i am trying to do is display the timesheet for the week.
So if i view the page today then it should display me the current week dates. Any pointers to approach this problem would be very helpful
Thanks
Prady
I would think you could use the Date.toStartOfWeek function and then use addDays to that Date 7 times to determine the rest of the days of the week.
The reference is here:
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_date.htm

Resources