You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Sailpoint与Eclipse中Java空值日期判断逻辑异常问题排查

问题:Sailpoint中日期比较逻辑错误导致结果异常

我在Eclipse中进行基准测试,处理用户登录登出场景的两个日期字符串,其中离职日期可能为空(需赋值为null),该逻辑将用于Sailpoint工程中。

Eclipse测试代码

public static void main(String[] args) {
    // TODO Auto-generated method stub
    
    DateTimeFormatter sdf = DateTimeFormatter.ofPattern("dd/MM/yyyy");
    String tarihEnd= null; 
    String tarihStart = "18/01/2023";
    
   try {
    if  (tarihEnd !=null || tarihStart!=null)
    {
        
        LocalDate jobLeavingDay = LocalDate.parse(tarihEnd,sdf);
        LocalDate jobStartingDay = LocalDate.parse(tarihStart,sdf);
        
             LocalDateTime beginDate = jobLeavingDay.atTime(8, 00);
             LocalDateTime endDate = jobLeavingDay.atTime(16, 30);
             LocalDateTime nowDateTime = LocalDateTime.now();
            
        if(nowDateTime.compareTo(beginDate)==1 && nowDateTime.compareTo(endDate)==-1) 
                  System.out.println("False1");
        
            else if(jobLeavingDay.compareTo(nowDateTime.toLocalDate())<0 && jobStartingDay.compareTo(nowDateTime.toLocalDate())<0) // jobleaving and jobstarting < nowdatetime = true
                System.out.println("True1");

            else if (jobStartingDay.compareTo(nowDateTime.toLocalDate())<0 && jobLeavingDay.compareTo(nowDateTime.toLocalDate()) >0) 
                System.out.println("False2");
        
            else if (jobStartingDay.compareTo(nowDateTime.toLocalDate())>0)
                System.out.println("True");
        
            else if (jobStartingDay.compareTo(nowDateTime.toLocalDate())<0)
                System.out.println("False3"); //jobleaving null, jobStarting_NowDate den once gelirse
    }} catch (Exception e) {
        
          tarihStart=null;
           tarihEnd=null;
        
        }     
}
}

在Sailpoint中,我从CSV文件提取用户信息进行身份识别。例如,入职日期为2022/12/20的用户应返回True,但无论使用compareTo的>符号还是isAfter方法,结果始终为False。

Sailpoint现有代码

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import sailpoint.object.*;

DateTimeFormatter sdf= DateTimeFormatter.ofPattern("dd/MM/yyyy");

Attributes attrs = link.getAttributes();
String tarih = attrs.getString("JobLeaveDate");
String tarih2 = attrs.getString("JobStartDate");

try{
if(tarih!=null || tarih2!=null {

LocalDate jobLeavingDay = LocalDate.parse(tarih, sdf);
LocalDate jobStartingDay = LocalDate.parse(tarih2, sdf);
LocalDate nowDate = LocalDate.now();

         //LocalDateTime beginDate = jobLeavingDay.atTime(8, 00);
        // LocalDateTime endDate = jobLeavingDay.atTime(16, 30);
                // LocalDateTime nowDateTime = LocalDateTime.now();

if (jobStartingDay.compareTo(nowDate)>0)
stat="True";

else if (jobStartingDay.compareTo(nowDate)<0)
stat="False";
} } catch (Exception e) {
  
           tarih=null;
        }   
return (stat)  

需要修改的核心点

1. 日期格式不匹配

CSV中入职日期格式为yyyy/MM/dd(如2022/12/20),但代码中使用的是dd/MM/yyyy格式化器,解析时会直接抛出异常进入catch块,导致stat未被正确赋值。需将格式化器改为对应CSV的格式:

DateTimeFormatter sdf= DateTimeFormatter.ofPattern("yyyy/MM/dd");

2. 空值处理逻辑错误

  • 原条件if(tarih!=null || tarih2!=null)错误:当离职日期为空(tarih=null)但入职日期存在时,仍会进入代码块,此时LocalDate.parse(tarih, sdf)会抛异常。应改为仅判断入职日期非空:
    if(tarih2 != null)
    
  • 离职日期需先判断再解析,避免空指针:
    LocalDate jobLeavingDay = tarih != null ? LocalDate.parse(tarih, sdf) : null;
    

3. Catch块与变量初始化问题

  • 代码未初始化stat变量,若进入catch块会导致返回未定义值,需在开头初始化:
    String stat = "False"; // 默认值根据需求调整
    
  • Catch块无需修改tarih,应专注于异常后的逻辑处理,比如保持stat默认值。

4. 补充相等场景的处理

原代码未覆盖入职日期等于当前日期的情况,需补充分支:

if (jobStartingDay.isAfter(nowDate)) {
    stat = "True";
} else if (jobStartingDay.isBefore(nowDate)) {
    stat = "False";
} else {
    // 入职日期等于当前日期时的逻辑,例如返回True或False
    stat = "True";
}

修改后的Sailpoint代码示例

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import sailpoint.object.*;

// 匹配CSV的日期格式
DateTimeFormatter sdf = DateTimeFormatter.ofPattern("yyyy/MM/dd");
// 初始化stat默认值
String stat = "False";

Attributes attrs = link.getAttributes();
String tarih = attrs.getString("JobLeaveDate");
String tarih2 = attrs.getString("JobStartDate");

try {
    // 仅当入职日期存在时处理
    if (tarih2 != null) {
        LocalDate jobLeavingDay = tarih != null ? LocalDate.parse(tarih, sdf) : null;
        LocalDate jobStartingDay = LocalDate.parse(tarih2, sdf);
        LocalDate nowDate = LocalDate.now();

        if (jobStartingDay.isAfter(nowDate)) {
            stat = "True";
        } else if (jobStartingDay.isBefore(nowDate)) {
            stat = "False";
        } else {
            // 处理入职日期等于当前日期的情况
            stat = "True";
        }
    }
} catch (Exception e) {
    // 异常时保持默认值,或根据需求调整
    stat = "False";
}

return stat;

内容的提问来源于stack exchange,提问作者Yusuf Akar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 23:24:32