⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stringhandler.java

📁 东软短信网关接入程序
💻 JAVA
字号:
package com.zznode.dp.ismg.util;

import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.Vector;
import java.util.Date;

public class StringHandler {
  private StringHandler _instance = null;
  public StringHandler() {
  }

  /**
   * 本类的实例对象
   * @return Log
   */
  public StringHandler getStringHandler() {
    if (_instance == null) {
      _instance = new StringHandler();
    }
    return _instance;
  }

  public Calendar getCalendar(String time) {
    if (time == null || (time != null && time.equals(""))) {
      System.out.println("time is not null!");
      return null;
    }
    String pattern = "yyyy-MM-dd HH:mm:ss"; ;
    SimpleDateFormat bartDateFormat;
    if (isCompriseAppointCharacter(time, "[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}")) {
      pattern = "yyyy-MM-dd";
    }
    bartDateFormat = new SimpleDateFormat(pattern);
    Calendar rightNow = Calendar.getInstance();
    try {
      Date date = bartDateFormat.parse(time);
      rightNow.setTimeInMillis(date.getTime());
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
    return rightNow;
  }

  /**
   * 返回当前日期的字符串
   * @return String
   */
  public String getCurrentDate() {
    Calendar rightNow = Calendar.getInstance();
    String date = rightNow.get(rightNow.YEAR) + "-" +
        Integer.toString(rightNow.get(rightNow.MONTH) + 1) + "-" +
        rightNow.get(rightNow.DATE);
    return date;
  }

  /**
   * 返回当前时间的字符串
   * @return String
   */
  public String getCurrentTime() {
    Calendar rightNow = Calendar.getInstance();
    String time = rightNow.get(rightNow.YEAR) + "-" +
        Integer.toString(rightNow.get(rightNow.MONTH) + 1) + "-" +
        rightNow.get(rightNow.DATE) + " " +
        rightNow.get(rightNow.HOUR) + ":" + rightNow.get(rightNow.MINUTE) + ":" +
        rightNow.get(rightNow.SECOND);
    return time;

  }

  /**
   * 返回时间字符串
   * @param rightNow Calendar
   * @return String
   */
  public String getCurrentTime(Calendar rightNow) {
    if (rightNow == null) {
      System.out.println("Calendar is not null !");
      return null;
    }

    String time = rightNow.get(rightNow.YEAR) + "-" +
        Integer.toString(rightNow.get(rightNow.MONTH) + 1) + "-" +
        rightNow.get(rightNow.DATE) + " " + rightNow.get(rightNow.HOUR) + ":" +
        rightNow.get(rightNow.MINUTE) + ":" + rightNow.get(rightNow.SECOND);
    return time;

  }

  /**
   * 返回当前日期的字符串
   * @return String
   */
  public String getCurrentDate(Calendar rightNow) {
    if (rightNow == null) {
      System.out.println("Calendar is not null !");
      return null;
    }
    String date = rightNow.get(rightNow.YEAR) + "-" +
        Integer.toString(rightNow.get(rightNow.MONTH) + 1) + "-" +
        rightNow.get(rightNow.DATE);
    return date;
  }

  /**
   * 查看当前字符中是否含有指定的字符
   * @param i int
   * @param eventlist String[]
   * @return String
   */
  public boolean isCompriseAppointCharacter(String linkstr, String pststr) {
    Pattern p = null; //正则表达式
    Matcher m = null; //操作的字符串

    //字符串匹配
    p = Pattern.compile(pststr); //查找
    m = p.matcher(linkstr);
    if (m.find()) {
      return true;
    }
    else {
      return false;
    }
  }

  public static void main(String[] args) {

    StringHandler stringhandler = new StringHandler();

  }

  /**
   * 将Vector中的数据以符号隔开
   * @param ID Vector
   * @return String
   */
  public String dealVectorBySign(Vector ID, String sign) {
    String IDStr = "";
    if (ID != null) {
      for (int i = 0; i < ID.size(); i++) {
        if (i == 0) {
          IDStr += ID.get(i);
        }
        else {
          IDStr += sign + ID.get(i);
        }
      }
    }
    return IDStr;
  }

  /**
   * 用指定的模板替换字符串
   * @param _line String
   * @param patstr String
   * @param repstr String
   * @return String
   */
  public String replaceAppointCharacter(String _line, String patstr,
                                        String repstr) {
    String line = _line;
    if (line == null || (line != null && line.equals(""))) {
      line = "";
    }
    else {
      line = _line.trim();
      Pattern p = null; //正则表达式
      Matcher m = null; //操作的字符串
      StringBuffer sb = null;
      //字符串匹配
      p = Pattern.compile(patstr); //查找patstr
      m = p.matcher(line);
      sb = new StringBuffer();
      while (m.find()) {
        m.appendReplacement(sb, repstr);
      }
      m.appendTail(sb);

      line = sb.toString();
    }
    return line; //返回值
  }

  /* 提取附和模板的部分
   * @param _line String
   * @return String
   */
  public Vector distillAccordTemplatePart(String line, String patstr) {
    Vector values = new Vector(); ;
    Pattern p = Pattern.compile(patstr);
    Matcher m = p.matcher(line);
    boolean rs = m.find();

    for (int i = 1; i <= m.groupCount(); i++) {
      values.add(m.group(i));
    }
    return values;
  }

  public byte[] strToUcs2(String str) {
    byte[] b = null;
    try {
      b = str.getBytes("UnicodeBigUnmarked");
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
    return b;
  }

  public byte[] strToGbk(String str) {
    byte[] b = null;
    try {
      b = str.getBytes("GBK");
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
    return b;
  }

  public byte[] strToAscii(String str) {
    byte[] b = null;
    try {
      b = str.getBytes("ASCII");
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
    return b;
  }

  private String bytesToHexStr(byte[] b) {
    if (b == null)return "";
    StringBuffer strBuffer = new StringBuffer(b.length * 3);
    for (int i = 0; i < b.length; i++) {
      strBuffer.append(Integer.toHexString(b[i] & 0xff));
      strBuffer.append(" ");
    }
    return strBuffer.toString();
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -