zoukankan      html  css  js  c++  java
  • 各种类型转为Date

    最近遇到个问题,就是Java的Excel导入时,对Excel中的日期做处理,常规模式用下面这种方法处理

    HSSFCell birthDateCell = row.getCell(col++);

    Date date = birthDateCell.getDateCellValue(); 

    今天推荐下面这种做法:

    首先定一个方法,做返回date类型,需要传object值类型

     public static Date getDate(Object o)
      {
        if (o == null)
          return null;
        if (o instanceof Date)
          return ((Date)o);
        if (o instanceof String)
          return getDate(String.valueOf(o));
        if (o instanceof Timestamp)
          return new Date(((Timestamp)o).getTime());
        if (o instanceof Date)
          return new Date(((Date)o).getTime());

        return null; 

      } 

    或者这样

     public static Date getDate(String dateStr)

      {
        Date temp1 = null;
        if (dateStr == null)
          return null;
        if (dateStr.equals(""))
          return null;
        SimpleDateFormat formatter = null;
        try {
          if (dateStr.indexOf(" ") != -1) {
            String[] aa = StringUtils.split(dateStr, ":");
            if (aa.length == 3)
              formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            else if (aa.length == 2)
              formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            else
              formatter = new SimpleDateFormat("yyyy-MM-dd HH");

          }
          else if (dateStr.indexOf("-") == -1) {
            formatter = new SimpleDateFormat("yyyyMMdd");
          } else {
            formatter = new SimpleDateFormat("yyyy-MM-dd");
          }

          temp1 = formatter.parse(dateStr);
        } catch (Exception e) {
          e.printStackTrace();
        }
        return temp1;
      }

    然后做Java的Excel导入时,就可以操作时间了

     HSSFCell registerTimeCell = row.getCell(col++);

     Date date = DateUtil.getDate(getCellValue(registerTimeCell));

    把上面的2个 getDate方法放到你定义的类里面去,方便使用

  • 相关阅读:
    ps -aux --sort -rss |head 列出进程拿物理内存占用排序 使用ps aux 查看系统进程时,第六列即 RSS列显示的就是进程使用的物理内存。
    13 memcache服务检查
    shell 颜色
    expr判断整数是相加的值,返回命令的返回值$? 是0,但是少数情况是1,例如1 + -1 ,$? 的结果是1 ,判断要大于1最准确
    ZABBIX监控原理
    ansible分发密钥
    再来一个expect脚本
    11:菜单自动化软件部署经典案例
    19:批量检查多个网站地址是否正常
    数组迭代
  • 原文地址:https://www.cnblogs.com/nxblog/p/4615500.html
Copyright © 2011-2022 走看看