Java trim isBlank isDataTime的实现

本文介绍了一种用于验证日期字符串格式(yyyyMMdd_HHmmss)的方法,并提供了一个用于去除字符串中空白字符(包括全角和半角空格)的实用函数。这些工具能够帮助开发者在Java应用程序中更高效地进行数据验证和处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.去除空白(包括全角和半角空格)
public static String trim(String target) {
   if (target == null) {
       return target;
   } else {
       return target.trim().replaceAll("^[\\s ]+", "").replaceAll("[\\s ]+$","");
   }
}
2. null 或空白判断
public static boolean isBlank(String target) {
       return (target == null) || (trim(target).length() == 0);
}
3.判断某日期字符串是否是 yyyyMMdd_HHmmss格式
  public static boolean isDateTime(String dataTime) {
      boolean result = true;
     
     if (dataTime == null || "yyyyMMdd_HHmmss".length() != dataTime.length()) {
        result = false;
     } else {
       DateFormat format = new SimpleDateFormat("yyyyMMdd_HHmmss");
       format.setLenient(false);
       try {
             format.parse(dataTime);
        } catch (ParseException e) {
            result = false;
       }
    }
    return result;  
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值