56、salesforce学习笔记(三)
Date类型 Datetime nowDatetime = Datetime.now(); Datetime datetime1 = Datetime.newInstance(2015,3,1,13,26,0); String datetimeString = '2016-3-1 PM14:38'; System.debug('通过初始化年月日时分秒得到的Datetime,并转换格式到:'+datetime1.format('yyyy-MM-dd HH:mm:ss')); System.debug('当前日期时间:' + nowDatetime.format()); datetime1 = datetime1.addDays(1); datetime1 = datetime1.addMonths(1); datetime1 = datetime1.addYears(1); datetime1 = datetime1.addHours(1); datetime1 = datetime1.addMinutes(1); datetime1 = datetime1.addSeconds(1); System.debug('更改后的日期时间:'+datetime1.format('yyyy-MM-dd HH:mm:ss')); Date date1 = datetime1.date(); System.debug('datetime1对应的Date为:'+date1.format()); Date dateGmt = datetime1.dateGmt(); System.debug('datetime1对应的DateGmt值为:'+dateGmt.format()); Integer year = datetime1.year(); Integer yearGmt = datetime1.yearGmt(); Integer month = datetime1.month(); Integer monthGmt = datetime1.monthGmt(); Integer day = datetime1.day(); Integer dayGmt = datetime1.dayGmt(); Integer dayOfYear = datetime1.dayOfYear(); Integer dayOfYearGmt = datetime1.dayOfYearGmt(); Integer hour = datetime1.hour(); Integer hourGmt = datetime1.hourGmt(); Integer minute = datetime1.minute(); Integer minuteGmt = datetime1.minuteGmt(); Integer second = datetime1.second(); Integer secondGmt = datetime1.secondGmt(); System.debug('year:'+year+'\tyearGmt:'+yearGmt); System.debug('month:'+month+'\tmonthGmt:'+monthGmt); System.debug('day:'+day+'\tdayGmt:'+dayGmt); System.debug('hour:'+hour+'\thourGmt:'+hourGmt); System.debug('minute:'+minute+'\tminuteGmt:'+minuteGmt); System.debug('second:'+second+'\tsecondGmt:'+secondGmt); System.debug('dayOfYear:'+dayOfYear+'\tdayOfYearGmt:'+dayOfYearGmt); System.debug('转成本地日期并以长日期类型显示:'+datetime1.formatLong()); Long timeL = datetime1.getTime(); System.debug('转成time类型的Long类型显示为:'+timeL.format()); Datetime datetime5 = Datetime.newInstance(2016, 4 ,2); System.debug('datetime1与datetime2是否同一天:'+datetime1.isSameDay(datetime5));
结果如下所示
Date类型 Date date2 = Date.today(); System.debug('当前日期:'+date2.format()); Date date3 = Date.newInstance(2016, 3, 1); String dateString = '2016-3-1'; Date date4 = Date.parse(dateString); Date date5 = Date.valueOf(dateString); System.debug('通过newInstance实例化:'+date3.format()); System.debug('通过parse实例化:'+ date4.format()); System.debug('通过valueOf实例化' + date5.format()); date3 = date3.addMonths(1); date3 = date3.addDays(1); System.debug('date3的日期为:' + date3.format()); Integer year1 = date3.year(); Integer month1 = date3.month(); Integer day1 = date3.day(); System.debug('year:'+year1); System.debug('month:'+ month1); System.debug('day:'+ day1); Integer dayOfYear1 = date3.dayOfYear(); System.debug('dayOfYear :' + dayOfYear1); Integer daysBetween = date3.daysBetween(date4);//date4-date3 System.debug('date3和date4相差天数:'+ daysBetween); System.debug('date4和date5是否相同日期:'+date4.isSameday(date5)); System.debug('date3和date4相差月数:'+date3.monthsBetween(date4)); System.debug('调用toStartOfMonth执行值:'+date3.toStartOfMonth().format()); System.debug('调用toStartOfWeek执行值:' + date3.toStartOfWeek().format());
结果如下所示