现在有Opportunity表中,最近的更新日时计算与系统日付之间的时间如何差
1.系统日付取得
代码语言:javascript复制Datetime sysDateTime = System.now();
String strSysDateTime = sysDateTime.format('YYYY-MM-dd HH:mm:ss');
system.debug('strSysDateTime>>>' strSysDateTime);
2.Opportunity表中,最近的更新日时取得
代码语言:javascript复制List<Opportunity> oppList = [SELECT LastModifiedDate
FROM Opportunity
ORDER BY LastModifiedDate DESC];
Datetime lastModifiedDate = oppList.get(0).LastModifiedDate;
String strConvertedDate = lastModifiedDate.format('YYYY-MM-dd HH:mm:ss');
system.debug('strConvertedDate>>>' strConvertedDate);
3.计算时间差
代码语言:javascript复制long fromDateTime = Datetime.ValueOf(strConvertedDate).getTime();
long toDateTime = Datetime.ValueOf(strSysDateTime).getTime();
Integer days = (Integer)((toDateTime - fromDateTime) / (1000 * 60 * 60 * 24));
system.debug('最终更新日和系统日付的天数差为>>>' days);
Integer hours = (Integer)((toDateTime - fromDateTime) / (1000 * 60 * 60));
system.debug('最终更新日和系统日付的小时差为>>>' hours);
Integer minutes = (Integer)((toDateTime - fromDateTime) / (1000 * 60));
system.debug('最终更新日和系统日付的分钟差为>>>' minutes);