salesforce如果简单的说可以大概分成两个部分:Apex,VisualForce Page.
其中Apex语言和java很多的语法类似,今天总结的是一些简单的Apex的变量等知识。
有如下几种常用的基本变量Integer,String,Decimal,Double,Long,Boolean,ID。
集合常用的对象:List<T>,Set<T>,Map<T>。
时间日期常用对象:Datetime,Time,Date。
其他:Object,sObject(与数据库相关,以后篇会讲)
与JAVA一个最大的区别是:Apex中基本对象的初始值均为null。
eg:
Integer i;
i += 1;
System.debug(i);
在java中此种写法是可以的,因为int类型初始值为0,i+=1以后则i变成1.但是在Apex中因为i初始值为null。 所以i+=1在运行时会抛出NullPointerException 当然,比较有意思的事情是这样,直接上代码:
Integer i;
System.debug(i+'1');
此种方法输出的结果则为null1。起始这也不奇怪,因为Apex也是基于java拓展的,如果看java编程思想了解底层的null的toString()方法处理也就知道了,当执行Print操作时,一个变量为null时,他的toString方法则返回'null'字符串。当然,这个只是一个拓展,不多展开,如果感兴趣,可以查看一下java的api或者看一下java编程思想一书。
一)基本变量:
1)Integer
Integer表示一个32位整数的对象,取值范围为-2^31 -- 2^31.
Integer主要有两个方法:
/*
public String format()
//译:将Integer值转换成String类型的值
*/
Integer goodsCount = 12;
System.debug('将Integer值转成String: ' + goodsCount.format());
/*
public static Integer valueOf(String stringToObject)
//译:将String类型转成Integer类型
*/
Integer goodsCountI = Integer.valueOf('12');
2)Long
Long类型表示一个64位整数的对象,取值范围为-2^63--2^63-1.
Integer类型可以直接转换成Long类型,Long类型在不超过范围情况下可以通过intValue()方法转成Integer类型。
以下为Long类型部分主要方法:
Integer transferSource = 12345;Long code = transferSource;//Integer类型可以直接转成Long类型
/*
public String format()
//译:将Long类型转换成String类型
*/
System.debug('Long类型转成String类型:' + code.format());
/*
public Integer intValue()
//译:将Long类型转成Integer类型
*/
System.debug('将Long类型转成Integer类型:' + code.intValue());
/*
public static Long valueOf(String stringToLong)
//译:将String类型转成Long类型
*/
Long codeLong = Long.valueOf('123');
3)ID
ID类型可以用任何一个符合规则的18位字符表示,如果你设置ID字符为15位,则将字符自动扩展成18位。不符合规则的ID字符在运行时则运行时异常。
以下为ID的主要方法:
/*
public static ID valueOf(String toID)
//译:将toId转换成ID
*/
String idStr = '111111111111111111';
ID id = ID.valueOf(idStr);
/*
public Boolean equals(String id)
//译:判断两个ID是否相同
*/
Boolean isEquals = id.equals(idStr);
4)Decimal
简单的来说,Decimal变量的意思为包含小数点的32位数就是Decimal,很像java中的float类型变量。
以下为Decimal的部分主要方法用来了解Decimal的功能:
1 Decimal priceDecimal = -4.50;
2 /*
3 public Decimal abs()
4 //译:返回小数点的绝对值
5 */
6 System.debug('小数的绝对值为:' + priceDecimal.abs());
7
8 /*
9 public Decimal divide(Decimal divisor, Integer scale)
10 //译:通过divisor作为除数,并设置结果为特定的scale的位数
11 */
12 System.debug('priceDecimal除以10小数点保留两位小数: ' + priceDecimal.divide(10,2));//-0.45
13
14 /*
15 public Double doubleValue()
16 //译:将Decimal类型转换成Double类型
17 */
18 System.debug('将priceDecimal转换成Double类型' + priceDecimal.doubleValue());
19
20 /*
21 public String format()
22 //译:将Decimal转成String类型
23 */
24 System.debug('Decimal转成String类型' + priceDecimal.format());
25
26 /*
27 public Integer intValue()
28 //译:将Decimal转成Integer
29 */
30 System.debug('将Decimal转成Integer类型' + priceDecimal.intValue());
31
32 /*
33 public Long longValue()
34 //译:将Decimal转成Long类型
35 */
36 System.debug('将Decimal转成Long类型' + priceDecimal.longValue());
37
38 /*
39 public Decimal pow(int exponent)
40 译:返回Decimal对应的指数次幂值.如果Decimal值为0,则返回1
41 */
42 System.debug('priceDecimal平方值为:' + priceDecimal.pow(2));
43
44 /*
45 public Integer precision()
46 //译:返回Decimal值得数字的总数
47 */
48 System.debug('priceDecimal数字总数为:' + priceDecimal.precision());//2 -4.5 有4和5
49
50
51 /*
52 public Long round()
53 //译:将Decimal值转换成最接近Long类型的值,四舍五入
54 */
55 System.debug('priceDecimal四舍五入Long类型值为:' + priceDecimal.round());
56
57 /*
58 public Integer scale()
59 //译:返回小数点后的位数个数
60 */
61 System.debug('priceDecimal小数点后的位数为:' + priceDecimal.scale());
62
63 /*
64 public Decimal setScale(Integer scale)
65 //译:设置小数的小数点后的位数
66 */
67 System.debug('设置priceDecimal的小数为2位' + priceDecimal.setScale(2));
68
69 /*
70 public Decimal stripTrailingZeros()
71 //译:返回移除0以后的小数
72 */
73 System.debug('移除priceDecimal小数点后的0以后的值为:' + priceDecimal.stripTrailingZeros());
74
75 /*
76 public String toPlainString()
77 //译:返回Decimal转换后的String,Decimal值不使用科学记数法
78 */
79 System.debug('不使用科学记数法转换成String类型' + priceDecimal.toPlainString());
80 /*
81 Decimal.valueOf(Object objectToDecimal)
82 //译:将Object转成Decimal。其中Object可以为Double,Long, String
83 */
84 Long priceL = 12345;
85 Double priceD = 123.456;
86 String priceS = '12345';
87 Decimal d1 = Decimal.valueOf(priceL);
88 Decimal d2 = Decimal.valueOf(priceD);
89 Decimal d3 = Decimal.valueOf(priceS);
5)Double
Double变量为包含小数点的64位数,很像 java中的Double类型变量。
Decimal类型变量可以直接转换成Double类型变量,Double类型在不超过范围情况下可以通过
以下为Double的部分主要方法:
Double price = 34.5678;
/*
public static Double valueOf(String stringToDouble)
//译:将String类型转换成Double
*/
String doubleString = '3.89';
System.debug('将字符串转换成Double' + Double.valueOf(doubleString));
/*
public Long round()
//译:返回double最接近Long的值,四舍五入
*/
Long priceLong = price.round();
System.debug('通过round方法将double转换成Long类型值为:' + priceLong);
/*
public Integer intValue()
//译:将double值转换成int类型值
*/
Integer priceInteger = price.intValue();
System.debug('将double转换成Integer类型值为:' + priceInteger);
Long priceLongByLongValue = price.longValue();
System.debug('将double转换成Long类型值为:' + priceLongByLongValue);
6)String
String类型和Java中的String类型很类似,在这里不做过多解释,代码中主要需要看一下String类型对象和上述变量如何相互转换,这在项目中是经常用到的,也是必须需要知道的。以下为String类型主要方法:
1 String goodsName = 'abcd123汉字显示';//测试文本
2 /*
3 public String abbreviate(Integer maxWidth)
4 //译:返回简化字符串,maxWidth>自身长度?自身:maxWidth长度的字符串加上省略号,省略号占3个字符
5 //注意:maxWidth如果小于4,则抛出Runtime Exception
6 */
7
8 System.debug('简化后的字符串名称为: '+goodsName.abbreviate(5));//结果:ab...
9
10 /*
11 public String abbreviate(Integer maxWidth,Integer offset)
12 //译:返回简化字符串,maxWidth为需要简化长度,offset为字符串简化起点
13 //如果max太小,则抛出Runtime Exception
14 */
15 //返回简化字符串,参数1:最大长度;参数2:偏移量offset
16 System.debug('简化并添加偏移量的字符串名称为:'+goodsName.abbreviate(5,2));
17
18 /*
19 public String capitalize()
20 //译:返回当前字符串,其中第一个字母改为标题(大写)。
21 */
22
23 System.debug('将首字母大写显示'+goodsName.capitalize());
24
25 /*
26 public String center(Integer size)
27 //译:返回指定大小字符串使原字符串位于中间位置(空格填充左右),如果size<字符串长度,则不起作用
28 */
29
30 System.debug('设置指定字符串长度为20的显示为:' + goodsName.center(20));
31
32 /*
33 public String center(Integer size,String paddingString)
34 //译:返回指定大小字符串使原字符串处于中间位置。参数1:字符串显示长度;参数2:填充的字符样式
35 */
36 //返回值:----abcd123汉字显示-----
37 System.debug('使用-填充字符串显示为:'+goodsName.center(20,'-'));
38
39 /*
40 public Integer charAt(Integer index)
41 //译:返回对应值得ASC码值
42 */
43 System.debug('goodsName.charAt(5)' + goodsName.charAt(5));
44
45 /*
46 public Integer codePointAt(Integer index)
47 //译:返回指定位置的值对应的Unicode编码
48 */
49
50 System.debug('goodsName.codePoint(5)' + goodsName.codePointAt(5));
51
52 /*
53 public Integer codePointBefore(Integer index)
54 //译:返回指定位置的值前一个对应的Unicode编码
55 */
56 System.debug('goodsName.codePointBefore(5)' + goodsName.codePointBefore(5));
57
58 /*
59 public Integer codePointCount(Integer beginIndex,Integer endIndex)
60 //译:返回起始位置到截至位置字符串的Unicode编码值
61 */
62
63 System.debug('goodsName.codePointCount(5,7)' + goodsName.codePointCount(5,7));
64
65 /*
66 public Integer compareTo(String secondString)
67 //译:基于Unicode比较两个字符串大小,如果小于比较值返回负整数,大于返回正整数,等于返回0
68 */
69 System.debug('两个字符串比较的情况为 : ' + goodsName.compareTo('compareString'));
70
71 /*
72 public Boolean contains(String substring)
73 //译:判断是否包含某个字符串,包含返回true,不包含返回false
74 */
75
76 System.debug('商品名称是否包含abcd : ' + goodsName.contains('abcd'));
77
78 /*
79 public Boolean containsAny(String inputString)
80 //译:判断是否包含inputString任意一个字符,包含返回true,不包含返回false
81 */
82 System.debug('商品名称是否包含abcd任意一个字符:' + goodsName.containsAny('abcd'));
83
84 /*
85 public Boolean containsIgnoreCase(String inputString)
86 //译:判断是否包含inputString(不区分大小写),包含返回true,不包含返回false
87 */
88
89 System.debug('商品名称是否包含AbCd(不区分大小写:)' + goodsName.containsIgnoreCase('AbCd'));
90 /*
91 public Boolean containsNone(String inputString)
92 //译:判断是否不包含inputString,不包含返回true,包含返回false
93 */
94 System.debug('商品名称是否不包含abcd'+goodsName.containsNone('abcd'));
95
96 /*
97 public Boolean containsOnly(String inputString)
98 //译:当前字符串从指定序列只包括inputString返回true,否则返回false
99 */
100 System.debug('商品名称是否只包含abcd:'+ goodsName.containsOnly('abcd'));
101
102 /*
103 public Boolean containsWhitespace()
104 //译:判断字符串是否包含空格,包含返回true,不包含返回false
105 */
106 System.debug('商品名称是否包含空格 : ' + goodsName.containsWhitespace());
107
108 /*
109 public Integer countMatches(String substring)
110 //译:判断子字符串在字符串中出现的次数
111 */
112 System.debug('商品名称出现abcd的次数:' + goodsName.countMatches('abcd'));
113
114 /*
115 public String deleteWhitespace()
116 //译:移除字符串中所有的空格
117 */
118 String removeWhitespaceString = ' a b c d ';
119 System.debug('原 a b c d ,移除空格的字符串显示为:' + removeWhitespaceString.deleteWhitespace());
120
121 /*
122 public String difference(String anotherString)
123 //译:返回两个字符串之间不同,如果anotherString为空字符串,则返回空字符串,如果anotherString为null,则抛异常
124 //比较结果以anotherString为基准,从第一个字符比较,不相同则返回anotherString与源字符串不同之处
125 */
126 //返回值:bcd啦啦啦
127 System.debug('商品名称和abcd啦啦啦的不同返回值为:' + goodsName.difference('bcd啦啦啦'));
128
129 /*
130 public Boolean endsWith(String substring)
131 //译:判断字符串是否已substring截止,如果是返回true,否则返回false
132 */
133 System.debug('商品名称是否已 显示 截止 :' + goodsName.endsWith('显示'));
134
135 /*
136 public Boolean endsWithIgnoreCase(String substring)
137 //译:判断字符串是否已substring截止(不区分大小写),如果是返回true,否则返回false
138 */
139 System.debug('商品名称是否已 显示 截止(不区分大小写) :' + goodsName.endsWithIgnoreCase('显示'));
140
141 /*
142 public Boolean equals(Object anotherString)
143 //译:判断字符串是否和其他字符串相同
144 */
145
146 /*
147 public Boolean equalsIgnoreCase(String anotherString)
148 //译:判断字符串是否和其他字符串相同(不区分大小写)
149 */
150
151 String testEquals = 'AbCd123汉字显示';
152
153 System.debug('商品名称是否和testEquals字符串相同:' + goodsName.equals(testEquals));
154
155 System.debug('商品名称是否和testEquals字符串相同:(不区分大小写)'+goodsName.equalsIgnoreCase(testEquals));
156
157 /*
158 public static String format(String stringToFormat, List<String> formattingArguments)
159 //译:将转换的参数替换成相同方式的字符串中
160 */
161 String sourceString = 'Hello {0} ,{1} is good';
162 List<String> formattingArguments = new String[]{'Zero','Apex'};
163 System.debug('替换sourceString内容以后显示为:' + String.format(sourceString,formattingArguments));
164
165 /*
166 public static String fromCharArray(List<Integer> charArray)
167 //译:将char类型数组转换成String类型
168 */
169 List<Integer> charArray = new Integer[] {55,57};
170 String destinatioin = String.fromCharArray(charArray);
171 System.debug('通过fromCharArray方法转换的字符串为:' + destinatioin);
172
173
174 /*
175 public List<Integer> getChars()
176 //译:返回字符串的字符列表
177 */
178 List<Integer> goodsNameChars = goodsName.getChars();
179
180 /*
181 public static String getCommonPrefix(List<String> strings)
182 //译:获取列表共有前缀
183 */
184 List<String> strings = new String[]{'abcdf','abe'};
185 String commonString = String.getCommonPrefix(strings);
186 System.debug('共有前缀:' + commonString);
187
188 //goodsName.getLevenshteinDistance();//待学习
189
190 /*
191 public Integer hashCode()
192 //译:返回字符串的哈希值
193 */
194 Integer hashCode = goodsName.hashCode();
195 System.debug('商品名称的哈希值为:'+hashCode);
196 /*
197 public Integer indexOf(String substring)
198 //译:返回substring第一次在字符串中出现的位置,如果不存在返回-1
199 */
200 System.debug('cd 在商品名称中出现的位置:' + goodsName.indexOf('cd'));
201
202 /*
203 public Integer indexOf(String substring,Integer index)
204 //译:返回substring第一次在字符串中出现的位置,起始查询字符串的位置为index处
205 */
206 System.debug('cd 在商品名称中出现的位置:' + goodsName.indexOf('cd',2));
207
208 /*
209 public Integer indexOfAny(String substring)
210 //译:substring任意一个字符第一次在字符串中出现的位置
211 */
212 System.debug('商品信息中select任意字符最先出现位置:' + goodsName.indexOfAny('select'));
213
214 /*
215 public Integer indexOfAnyBut(String substring)
216 //译:返回substring任意一个字符不被包含的第一个位置,无则返回-1
217 */
218 System.debug('商品信息中select任意一个字符最先不被包含的位置'+goodsName.indexOfAnyBut('select'));
219
220 /*
221 public Integer indexOfChar(int char)
222 //译:返回字符串中char字符最先出现的位置
223 */
224 Integer firstChar = goodsName.indexOfChar(55);
225
226 /*
227 public Integer indexOfDifference(String compareTo)
228 //译:返回两个字符串第一个不同位置的坐标
229 */
230 System.debug('商品名称与abce字符串第一个不同的位置为:' + goodsName.indexOfDifference('abce'));
231
232 /*
233 public Integer indexOfIgnoreCase(String substring)
234 //译:返回substring在字符串中第一个出现的位置(不考虑大小写)
235 */
236 System.debug('商品名称中第一个出现CD位置的为(不分大小写)' + goodsName.indexOfIgnoreCase('CD'));
237
238 /*
239 public Boolean isAllLowerCase()
240 //译:字符串是否均为小写,如果是返回true,否则返回false
241 */
242 System.debug('商品名称中是否全是小写: ' + goodsName.isAllLowerCase());
243
244 /*
245 public Boolean isAllUpperCase()
246 //译:字符串是否均为大写,如果是返回true,否则返回false
247 */
248 System.debug('商品名称中是否全是大写:' + goodsName.isAllUpperCase());
249
250 /*
251 public Boolean isAlpha()
252 //译:如果当前所有字符均为Unicode编码,则返回true,否则返回false
253 */
254 System.debug('商品名称是否均为Unicode编码:' + goodsName.isAlpha());
255
256 /*
257 public Boolean isAlphanumeric()
258 //译:如果当前所有字符均为Unicode编码或者Number类型编码,则返回true,否则返回false
259 */
260 System.debug('商品名称是否均为Unicode或者Number类型编码:' + goodsName.isAlphanumeric());
261
262 /*
263 public Boolean isAlphanumericSpace()
264 //译:如果当前所有字符均为Unicode编码或者Number类型或者空格,则返回true,否则返回false
265 */
266 System.debug('商品名称是否均为Unicode,Number或者空格' + goodsName.isAlphanumericSpace());
267
268 /*
269 public Boolean isAlphaSpace()
270 //译:如果当前所有字符均为Unicode或者空格,则返回true,否则返回false
271 */
272 System.debug('商品名称是否均为Unicode,或者空格' +goodsName.isAlphaSpace());
273
274 /*
275 public Boolean isAsciiPrintable()
276 //译:如果当前所有字符均为可打印的Asc码,则返回true,否则返回false
277 */
278 System.debug('商品名称所有字符是否均为可打印的Asc码:' + goodsName.isAsciiPrintable());
279
280 /*
281 public Boolean isNumeric()
282 //译:如果当前字符串只包含Unicode的位数,则返回true,否则返回false
283 */
284 System.debug('商品名称所有字符是否均为Unicode的位数:' + goodsName.isNumeric());
285
286 /*
287 public Boolean isWhitespace()
288 //译:如果当前字符只包括空字符或者空,则返回true,否则返回false
289 */
290 System.debug('商品名称所有字符只包括空字符或者空:' + goodsName.isWhitespace());
291
292 /*
293 public static String join(Object iterableObj, String separator)
294 //译:通过separator连接对象,通用于数组,列表等
295 */
296 List<Integer> intLists = new Integer[] {1,2,3};
297 String s = String.join(intLists,'/');//s = 1/2/3
298
299
300 /*
301 public Boolean lastIndexOf(String substring)
302 //译:substring在字符串中最后出现的位置,不存在则返回-1
303 */
304 System.debug('cd最后一次出现的位置:' + goodsName.lastIndexOf('cd'));
305 /*
306 public Boolean lastIndexOfIgnoreCase(String substring)
307 //译:substring在字符串中最后出现的位置(忽略大小写),不存在则返回-1
308 */
309 System.debug('Cd最后一次出现的位置(不考虑大小写):' + goodsName.lastIndexOfIgnoreCase('Cd'));
310
311 /*
312 public String left(Integer index)
313 //译:获取从零开始到index处的字符串
314
315 */
316 System.debug('商品名称前三个字符 : abc' + goodsName.left(3));
317
318 /*
319 public String leftPad(Integer length)
320 //译:返回当前字符串填充的空间左边和指定的长度
321 */
322 String s1 = 'ab';
323 String s2 = s1.leftPad(4);//s2 = ' ab';
324 /*
325 public Integer length()
326 //译:返回字符串长度
327 */
328 System.debug('商品名称长度:' + goodsName.length());
329 /*
330 public String mid(Integer startIndex,Integer length);
331 //译:返回新的字符串,第一个参数为起始位,第二个字符为字符的长度//类似于substring
332 */
333 System.debug('商品名称截取字符串' + goodsName.mid(2,3));
334
335 /*
336 public String normalizeSpace()
337 //译:前后删除空白字符
338 */
339 String testNormalizeSpace = 'abc\t\n de';
340 System.debug('通过normalizeSpace处理后的字符串为:' + testNormalizeSpace.normalizeSpace());
341
342 /*
343 public String remove(String substring)
344 //译:移除所有特定的子字符串,并返回新字符串
345 public String removeIgnorecase(String substring)
346 //译:移除所有特定的子字符串(忽略大小写),并返回新字符串
347 */
348 System.debug('商品名称移除12后的字符串为:' + goodsName.remove('12'));
349
350 /*
351 public String removeEnd(String substring)
352 //译:当且仅当子字符串在后面移除子字符串
353 */
354 System.debug('当显示在商品名称最后时移除:' + goodsName.removeEnd('显示'));
355
356 /*
357 public String removeStatrt(String substring)
358 //译:当且仅当子字符串在后面移除子字符串
359 */
360 System.debug('当ab在商品名称最前面时移除' + goodsName.removeStart('ab'));
361
362 /*
363 public String repeat(Integer numberOfTimes)
364 //译:重复字符串numberOfTimes次数
365 */
366 System.debug('重复商品名称两次的显示为:' + goodsName.repeat(2));
367
368 /*
369 public String repeat(String separator, Integer numberOfTimes)
370 //译:重复字符串numberOfTimes次,通过separator作为分隔符
371 */
372 System.debug('通过separator分隔符重复字符串两次:' + goodsName.repeat('-',2));
373
374 /*
375 public String replace(String target, String replacement)
376 //译:将字符串中的target转换成replacement
377 */
378 System.debug('将商品名称中ab替换成AB' + goodsName.replace('ab','AB'));
379
380 /*
381 public String replaceAll(String target,String replacement)
382 */
383 System.debug('将商品名称中ab全部替换成AB' + goodsName.replaceAll('ab','AB'));
384
385 /*
386 public String reverse()
387 //译:字符串倒序排列
388 */
389 System.debug('商品名称倒序:' + goodsName.reverse());
390
391 /*
392 public String right(Integer length)
393 //译:从右查询返回length的个数的字符串
394 */
395 System.debug('返回商品名称后三位字符:' + goodsName.right(3));
396
397 /*
398 public String[] split(String regExp)
399 //译:通过regExp作为分隔符将字符串分割成数组
400 */
401 String[] sonSplitString = goodsName.split('1');//通过1分割字符串
402
403 /*
404 public String[] split(String regExp, Integer limit)
405 //译:通过regExp作为分隔符将字符串分割成数组,limit显示数组个数
406 */
407 String [] sonSplitString1 = goodsName.split('1',2);
408
409 /*
410 public Boolean startsWith(string substring)
411 //译:判断字符串是否以substring开头,如果是返回true,不是返回false
412 */
413 System.debug('商品名称是否以abcd开始:' + goodsName.startsWith('abcd'));
414
415 /*
416 public String substring(Integer length)
417 //译:截取字符串固定长度
418 */
419 System.debug('截取商品名称前四个字符: ' + goodsName.substring(4));
420
421 /*
422 public String toLowerCase()
423 //译:将字符串转换成小写
424 */
425 System.debug('商品名称转换成小写:' + goodsName.toLowerCase());
426
427 /*
428 public String toUpperCase()
429 //译:将字符串转换成大写
430 */
431 System.debug('商品名称转换成大写:' + goodsName.toUpperCase());
432
433 /*
434 public String trim()
435 //译:去字符串左右空格
436 */
437 System.debug('去空格后商品名称:' + goodsName.trim());
438
439 /*
440 public String uncapitalize()
441 //译:将字符串第一个转换成小写
442 */
443 System.debug('商品名称第一个单词转换成小写:' + goodsName.uncapitalize());
444
445 /*
446 public static String valueOf(Object objectToConvert)
447 //译:将Object类型转换成String类型,其中Object类型包括以下:
448 // Date,DateTime,Decimal,Double,Integer,Long,Object
449 */
450 System.debug('将Double类型转换成String类型' + String.valueOf(3.45));
7)Boolean
Boolean类型声明一个布尔类型,和java区别为:Boolean类型变量有三个取值:true,false,null(default),所以使用Boolean类型声明的时候必须赋予初始值,否则初始值为null
二)时间日期类型
1)Datetime
Datetime类型声明一个日期时间的对象,包含两部分:日期,时间。因为salesforce一般制作global项目,所以日期时间一般取格林时间。Datetime无构造函数,如果实例化只能通过其静态方法初始化。以下为Datetime的部分主要方法:
1 Datetime nowDatetime = Datetime.now();
2 Datetime datetime1 = Datetime.newInstance(2015,3,1,13,26,0);
3 String datetimeString = '2016-3-1 PM14:38';
4 /*
5 Datetime datetime2 = Datetime.parse(datetimeString);
6 Datetime datetime3 = Datetime.valueOf(datetimeString);
7 */
8 System.debug('通过初始化年月日时分秒得到的Datetime,并转换格式值:'+datetime1.format('yyyy-MM-dd HH:mm:ss'));
9 System.debug('当前日期时间:' + nowDatetime.format());
10 /*
11 System.debug('通过parse方法初始化的datetime:' + datetime2.format());
12 System.debug('通过valueOf方法初始化的datetime'+datetime3.format());
13 */
14 datetime1 = datetime1.addDays(1);
15 datetime1 = datetime1.addMonths(1);
16 datetime1 = datetime1.addYears(1);
17 datetime1 = datetime1.addHours(1);
18 datetime1 = datetime1.addMinutes(1);
19 datetime1 = datetime1.addSeconds(1);
20 System.debug('更改后的日期时间:' + datetime1.format('yyyy-MM-dd HH:mm:ss'));
21 Date date1 = datetime1.date();
22 System.debug('datetime1对应的Date为:'+date1.format());
23
24 Date dateGmt = datetime1.dateGmt();
25 System.debug('datetime1对应的DateGmt值为:'+dateGmt.format());
26 Integer year = datetime1.year();
27 Integer yearGmt = datetime1.yearGmt();
28 Integer month = datetime1.month();
29 Integer monthGmt = datetime1.monthGmt();
30 Integer day = datetime1.day();
31 Integer dayGmt = datetime1.dayGmt();
32 Integer dayOfYear = datetime1.dayOfYear();
33 Integer dayOfYearGmt = datetime1.dayOfYearGmt();
34 Integer hour = datetime1.hour();
35 Integer hourGmt = datetime1.hourGmt();
36 Integer minute = datetime1.minute();
37 Integer minuteGmt = datetime1.minuteGmt();
38 Integer second = datetime1.second();
39 Integer secondGmt = datetime1.secondGmt();
40 System.debug('year : '+ year + '\tyearGmt : ' + yearGmt);
41 System.debug('month : ' + month + '\tmonthGmt : '+ monthGmt);
42 System.debug('day : ' + day + '\tdayGmt : ' + dayGmt);
43 System.debug('hour : ' + hour + '\thourGmt : ' + hourGmt);//两者不同 一个为14 Gmt为6
44 System.debug('minute : ' + minute + '\tminuteGmt : ' + minuteGmt);
45 System.debug('second : ' + second + '\tsecondGmt : ' + secondGmt);
46 System.debug('dayOfYear : ' + dayOfYear + '\tdayOfYearGmt : ' + dayOfYearGmt);
47 System.debug('转成本地日期并以长日期类型显示:'+ datetime1.formatLong());
48 Long timeL = datetime1.getTime();
49 System.debug('转成time类型的Long类型显示为:'+timeL.format());
50 Datetime datetime5 = Datetime.newInstance(2016,4,2);
51 System.debug('datetime1与datetime2是否同一天:' + datetime1.isSameDay(datetime5));//true
2)Date
Date类型声明一个日期的对象,Date可以和Datetime相互转换,主要需要掌握二者关系以及相互转换。
以下为Date部分主要方法:
1 Date date2 = Date.today();
2 System.debug('当前日期:' + date2.format());
3 Date date3 = Date.newInstance(2016,3,1);
4 String dateString = '2016-3-1';
5 Date date4 = Date.parse(dateString);
6 Date date5 = Date.valueOf(dateString);
7 System.debug('通过newInstance实例化:' + date3.format());
8 System.debug('通过parse实例化:' + date4.format());
9 System.debug('通过valueOf实例化:' + date5.format());
10
11 date3 = date3.addMonths(1);
12 date3 = date3.addDays(1);
13 System.debug('date3的日期为:' + date3.format());
14 Integer year1 = date3.year();
15 Integer month1 = date3.month();
16 Integer day1 = date3.day();
17 System.debug('year : ' + year1);
18 System.debug('month : ' + month1);
19 System.debug('day : ' + day1);
20 Integer dayOfYear1 = date3.dayOfYear();
21 System.debug('dayOfYear : ' + dayOfYear1);
22
23
24 Integer daysBetween = date3.daysBetween(date4);//date4-date3
25 System.debug('date3和date4相差天数:' + daysBetween);
26
27 System.debug('date4和date5是否相同日期:'+date4.isSameDay(date5));
28
29 System.debug('date3和date4相差月数:' + date3.monthsBetween(date4));
30
31 System.debug('调用toStartOfMonth执行值:' + date3.toStartOfMonth().format());//返回本月第一天
32 /*
33 public Date toStartOfWeek()
34 //译:返回本月第一个周日,如果本月1日非周日,则返回上月最晚的周日
35 */
36 System.debug('调用toStartOfWeek执行值: ' + date3.toStartOfWeek().format());
3)Time
Time类型声明一个时间的对象,对于时间需要考虑的是:因为中国时间和格林时间相差8小时,所以具体项目时如果是global项目需要考虑使用格林时间,即GMT时间。
三)集合类型
集合类型主要有三种,List,Set以及Map。其中三种均为泛型方式,所以声明变量时,直接带上泛型。
1)List<T>
List代表一类的有序数据列表。数据序号从0开始。与JAVA不同的是:List是一个类,并且不存在ArrayList等子类。即实例化
eg:List<String> list1 = new List<String>();
List可以通过自身构造函数实例化,也可以通过数组进行实例化。
以下为List主要方法:
注:set()方法在设置插入位置以前应确保长度大于需要插入的位置,否则将抛出异常。
1 List<String> lists = new String[]{'1','3'};
2 List<String> list1 = new String[] {'5','4'};
3 lists.set(0,'a');
4 lists.add(0,'b');
5 lists.add('2');
6 lists.addAll(list1);
7 //lists.sort();
8 for(String item : lists) {
9 System.debug('item : ' + item);
10 }
11 Iterator<String> iterator = lists.iterator();
12 while(iterator.hasNext()) {
13 String item = iterator.next();
14 System.debug('通过iterator遍历:'+ item);
15 }
16 if(lists.size() > 0) {
17 Integer listSize = lists.size();
18 lists.remove(listSize-1);
19 }
20 List<String> cloneList = lists.clone();
21 for(Integer i=0;i<cloneList.size();i++) {
22 System.debug('cloneListItem : ' + cloneList.get(i));
23 }
24 lists.clear();
25
26 if(lists.size() > 0) {
27 for(String item : lists) {
28 System.debug('set item : ' + item);
29 }
30 } else {
31 System.debug('lists has already clear');
32 }
2)Set<T>
Set代表一类数据的无序列表。与JAVA不同的是:Set是一个类,不存在HashSet等子类。即实例化
eg:Set<String> set1 = new Set<String>();
Set主要方法如下:
1 Set<String> set1 = new Set<String>();
2 set1.add('1');
3 set1.add('2');
4 Set<String> set2 = set1.clone();
5 Boolean isEquals = set1.equals(set2);
6 Boolean isContains = set1.contains('1');
7 Integer setSize = set1.size();
8 Iterator<String> iterator2 = set1.iterator();
9 while(iterator2.hasNext()) {
10 System.debug('set item:' + iterator2.next());
11 }
12 Boolean isEmpty = set1.isEmpty();
13 //set1.remove('1');
14 List<String> anotherList = new String[] {'1','3'};
15 /*
16 public Boolean retainAll(List<Object> list)
17 //译:set值为list中和set重复的内容,如果没有重复的内容,则set为空
18 */
19 set1.retainAll(anotherList);
20 Iterator<String> iterator3 = set1.iterator();
21 while(iterator3.hasNext()) {
22 System.debug('set item via retainAll:' + iterator3.next());//1,List与Set公共部分
23 }
3)Map<K,V>
Map代表着键值对,与JAVA用法类似,区别为Map是一个类,不是接口,不存在HashMap<K,V>等子类
Map主要方法如下:
1 Map<String,Object> map1 = new Map<String,Object>();
2 map1.put('key1','value1');
3 map1.put('key2','value2');
4 map1.put('key3','value3');
5 Boolean isContainsKey = map1.containsKey('key1');
6 Map<String,Object> map2 = map1.clone();
7 Boolean isMapEquals = map1.equals(map2);
8 Set<String> keySet = map1.keySet();
9 String value1 = (String)map1.get('key1');
10 List<String> valuesList = (List<String>)map1.values();
11 Integer mapSize = map1.size();
四)运算及控制语句
运算与控制语句和JAVA基本类似,所以在这里只是简单介绍一下增强for循环
List<String> goodsList = new String[] {'衣服','裤子'};
for(String goods : goodsList) {
System.debug(goods);
}
详细了解这些基本类型变量,集合以及相关方法请自己查看API。其实掌握一些主要的方法就可以,其他方法不用掌握大概清楚实现功能就好,具体需要的时候查看API看一下用法就可以。如果有什么问题,欢迎留言,共同探讨。内容如果有写的不正确地方,欢迎指正。下一篇将主要介绍sObject与简单的DML操作。