您的位置:首页 > 编程语言 > Java开发

Java 常用方法整理

2010-07-05 14:17 344 查看

1、时间对比处理

 

/**
  * 时间 差对比
  *
  * @param now1
  *            当前时间
  * @param date1
  *            对比时间
  * @return
  */
 public static Map<String, Long> getTime(Date now1, Date date1) {
  // SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  java.util.Date now = now1;
  java.util.Date date = date1;

  Map<String
9453
, Long> diffTime = new HashMap<String, Long>();
  // 获取 毫秒 差
  long l = now.getTime() - date.getTime();
  // 获取 天数 差
  long day = l / (24 * 60 * 60 * 1000);
  // 获取 小时 差(只对比时:分:秒)
  long hour = (l / (60 * 60 * 1000) - day * 24);
  // 获取 总小时差
  long hour1 = l / (60 * 60 * 1000);
  // 获取 分钟 差(只对比时:分:秒)
  long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
  // 获取总分钟差
  long min1 = l / (60 * 1000);
  // 获取 秒数 差(只对比时:分:秒)
  long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
  // 获取总秒数差
  long s1 = l / 1000;

  if (l > 0) {
   diffTime.put("AllMill", l);
  }
  if (day > 0) {
   diffTime.put("AllDay", day);
  }
  if (hour > 0) {
   diffTime.put("Hour", hour);
  }
  if (hour1 > 0) {
   diffTime.put("AllHour", hour1);
  }
  if (min > 0) {
   diffTime.put("Min", min);
  }
  if (min1 > 0) {
   diffTime.put("AllMin", min1);
  }
  if (s > 0) {
   diffTime.put("Second", s);
  }
  if (s1 > 0) {
   diffTime.put("AllSecond", s1);
  }
  return diffTime;
 }

 

2、字符串处理

 

/**
  * 字符串处理
  *
  * @param str
  *            要处理的字符串
  * @param conMap
  *            处理条件(自己需要自己添加) 1、(integer)Count 为字符串截取个数处理 2、AddDot 可以不设置
  *            添加省略号 默认为 3个 如果要添加多个省略号 根据第三个key 3、(Integer)DotCount 要添加省略号个数
  *            4、需要处理条件自己添加
  * @return
  */
 public String doString(String str, Map<Object, Object> conMap) {
  StringBuffer sBuffer = new StringBuffer();
  if (str != null && !("").equals(str)) {
   if (conMap.containsKey("Count")) {
    if (conMap.containsKey("AddDot")) {
     sBuffer.append(str.substring(0, Integer.valueOf(conMap.get(
       "Count").toString()) - 3));
     sBuffer.append("...");
    } else {
     if (conMap.containsKey("DotCount")) {
      sBuffer.append(str.substring(0, Integer.valueOf(conMap
        .get("Count").toString())
        - Integer.valueOf(conMap.get("DotCount")
          .toString())));
      for (int i = 0; i < Integer.valueOf(conMap.get(
        "DotCount").toString()); i++) {
       sBuffer.append(".");
      }
     }
    }
   }
  } else {
   System.out.print("需要处理的字符串是空的!");
   return "";
  }
  return sBuffer.toString();
 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息