Salesforce Apex Study Note

Salesforce Apex Tips like

How to get certain days month end day?
How to change the string to date ?
How to Change the date to String with formatting?

How to get certain days month end day?

1
2
3
Date selectedDate = Date.today().addDays(4);
Date firstDate = selectedDate.toStartOfMonth();
p.Date__c =firstDate.addDays(date.daysInMonth(selectedDate.year() , selectedDate.month()) - 1);

Change the string to date

1
2
3
Date x = Date.valueOf('2015-8-11'); 

system.debug('Now '+ x );

Change the date to String with formatting

1
2
3
4
5
6
7
8
9
10
11
date InputDate = date.newInstance(2017, 3, 21); 

Integer d = InputDate.day();

Integer mo = InputDate.month();

Integer yr = InputDate.year();

DateTime DT = DateTime.newInstance(yr, mo, d);

system.debug('Now '+ DT.format('yyyy-MM-dd') );

How can we implement stack concepts in salesforce apex?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public class MyStack {

private List < sObject > al;
public MyStack() {
al = new List < sObject > ();
}
public void push(sObject item) {
al.add(item);
}
public sObject pop() {
if (!isEmpty())
return al.remove(size() - 1);
else
return null;
}
public boolean isEmpty() {
return (al.size() == 0);
}
public sObject peek() {
if (!isEmpty())
return al.get(size() - 1);
else
null;
}
public int size() {
return al.size();
}

}

SQL –>List–> Map

map<id, User> userMap = new map<id, User>([select ID,ManagerId,Manager.Email from User where id in : OwnerIdSet]);