Apex Date如何使用?
作者:互联网
在 Salesforce Apex 中,Date
类用于表示日期(年、月、日),不包含时间信息。以下是 Date
类的常见用法及其相关操作:
创建 Date 对象
-
使用 today() 方法获取当前日期:
Date currentDate = Date.today(); System.debug(currentDate); // 输出当前日期,例如:2023-10-05
Apex -
使用 new Date(year, month, day) 构造函数创建特定日期: 注意:月份是从 1 开始计数的。
Date specificDate = Date.newInstance(2023, 10, 5); System.debug(specificDate); // 输出:2023-10-05
Apex -
从字符串解析日期: 使用
Date.valueOf(String)
方法将格式为 "yyyy-MM-dd" 的字符串转换为 Date 对象。String dateString = '2023-10-05'; Date parsedDate = Date.valueOf(dateString); System.debug(parsedDate); // 输出:2023-10-05
Apex
日期运算
-
添加或减去天数: 使用
addDays(integer)
方法。Date currentDate = Date.today(); Date futureDate = currentDate.addDays(7); // 一周后的日期 Date pastDate = currentDate.addDays(-3); // 三天前的日期 System.debug(futureDate); // 输出:当前日期 + 7 天 System.debug(pastDate); // 输出:当前日期 - 3 天
Apex -
添加或减去月份: 使用
addMonths(integer)
方法。Date currentDate = Date.today(); Date futureDate = currentDate.addMonths(1); // 一个月后的日期 Date pastDate = currentDate.addMonths(-2); // 两个月前的日期 System.debug(futureDate); // 输出:当前日期 + 1 个月 System.debug(pastDate); // 输出:当前日期 - 2 个月
Apex -
添加或减去年份: 使用
addYears(integer)
方法。Date currentDate = Date.today(); Date futureDate = currentDate.addYears(1); // 一年后的日期 Date pastDate = currentDate.addYears(-5); // 五年前的日期 System.debug(futureDate); // 输出:当前日期 + 1 年 System.debug(pastDate); // 输出:当前日期 - 5 年
Apex
获取日期组件
-
获取年份: 使用
year()
方法。Date currentDate = Date.today(); Integer year = currentDate.year(); System.debug(year); // 输出当前年份,例如:2023
Apex -
获取月份: 使用
month()
方法。Date currentDate = Date.today(); Integer month = currentDate.month(); System.debug(month); // 输出当前月份,例如:10
Apex -
获取日: 使用
day()
方法。Date currentDate = Date.today(); Integer day = currentDate.day(); System.debug(day); // 输出当前日,例如:5
Apex
比较日期
-
比较两个日期是否相等: 使用
equals(Date)
方法。Date date1 = Date.newInstance(2023, 10, 5); Date date2 = Date.newInstance(2023, 10, 5); Boolean areEqual = date1.equals(date2); System.debug(areEqual); // 输出:true
Apex -
比较两个日期的先后顺序: 使用
before(Date)
和after(Date)
方法。Date date1 = Date.newInstance(2023, 10, 5); Date date2 = Date.newInstance(2023, 10, 10); Boolean isBefore = date1.before(date2); Boolean isAfter = date1.after(date2); System.debug(isBefore); // 输出:true System.debug(isAfter); // 输出:false
Apex
格式化日期输出
- 将日期转换为字符串: 使用
format()
方法可以将日期格式化为特定区域设置的字符串。Date currentDate = Date.today(); String formattedDate = currentDate.format(); // 默认区域设置 String formattedDateWithLocale = currentDate.format('yyyy-MM-dd', 'en_US'); // 指定区域设置 System.debug(formattedDate); // 输出:根据默认区域设置格式化的日期字符串 System.debug(formattedDateWithLocale); // 输出:2023-10-05
Apex
示例:计算两个日期之间的天数差
public class DateDifference {
public static Integer getDaysBetweenDates(Date startDate, Date endDate) {
if (startDate == null || endDate == null) {
return null;
}
return Math.abs(startDate.daysBetween(endDate));
}
}
// 使用示例
Date date1 = Date.newInstance(2023, 10, 1);
Date date2 = Date.newInstance(2023, 10, 10);
Integer daysBetween = DateDifference.getDaysBetweenDates(date1, date2);
System.debug(daysBetween); // 输出:9
标签: 来源: