ITeye博客

一个关于日期,数字,字符格式化的常用工具类

BlueCore · 2009-11-01 23:03 · Java SE · Markdown

  1. import java.text.SimpleDateFormat;
    2. import java.text.ParseException;
    3. import java.util.Calendar;
    4. import java.util.Date;
    5. import java.text.DecimalFormat;
    6.
    7. import org.apache.commons.lang.StringUtils;
    8.
    9. public class StrTool {
  2. /** 
    
  3.  * 日期格式化 
    
  4.  * 
    
  5.  * @param date 
    
  6.  *            Date 
    
  7.  * @param formatstr 
    
  8.  *            String 
    
  9.  * @return String 
    
  10.  */  
    
  11. public static String formatDate(java.util.Date date, String formatstr) {  
    
  12.     if (date == null || formatstr == null)  
    
  13.         return null;  
    
  14.     java.text.SimpleDateFormat dateFormat = new SimpleDateFormat(formatstr);  
    
  15.     return dateFormat.format(date);  
    
  16. }  
    
  17. public static String formatDate(java.util.Date date) {  
    
  18.     return formatDate(date, "yyyy-MM-dd");  
    
  19. }  
    
  20. /** 
    
  21.  * 格式当前日期 
    
  22.  * 
    
  23.  * @return String 
    
  24.  */  
    
  25. public static String formatDateByNow() {  
    
  26.     return formatDate(new java.util.Date());  
    
  27. }  
    
  28. public static String formatTime(java.util.Date date) {  
    
  29.     return formatDate(date, "kk:mm:ss");  
    
  30. }  
    
  31. public static String formatDateTime(java.util.Date date) {  
    
  32.     return formatDate(date, "yyyy-MM-dd kk:mm");  
    
  33. }  
    
  34. /** 
    
  35.  * 解释日期yyyy-MM-dd kk:mm 
    
  36.  * 
    
  37.  * @param strdate 
    
  38.  *            String 
    
  39.  * @return Date 
    
  40.  * @throws ParseException 
    
  41.  */  
    
  42. public static java.util.Date parseDateTime(String strdate)  
    
  43.         throws ParseException {  
    
  44.     return parseDateTimeByFormatstr(strdate, "yyyy-MM-dd kk:mm");  
    
  45. }  
    
  46. /** 
    
  47.  * 解释日期yyyy-MM-dd 
    
  48.  * 
    
  49.  * @param strdate 
    
  50.  *            String 
    
  51.  * @return Date 
    
  52.  * @throws ParseException 
    
  53.  */  
    
  54. public static java.util.Date parseDate(String strdate)  
    
  55.         throws ParseException {  
    
  56.     return parseDateTimeByFormatstr(strdate, "yyyy-MM-dd");  
    
  57. }  
    
  58. /** 
    
  59.  * 根据传入的格式化字串来解释日期 
    
  60.  * 
    
  61.  * @param strdate 
    
  62.  *            String 
    
  63.  * @param formatstr 
    
  64.  *            String 
    
  65.  * @return Date 
    
  66.  * @throws ParseException 
    
  67.  */  
    
  68. public static java.util.Date parseDateTimeByFormatstr(String strdate,  
    
  69.         String formatstr) throws ParseException {  
    
  70.     java.text.SimpleDateFormat dateFormat = new SimpleDateFormat(formatstr);  
    
  71.     return dateFormat.parse(strdate);  
    
  72. }  
    
  73. /** 
    
  74.  * 格式数字类型 
    
  75.  * 
    
  76.  * @param f 
    
  77.  *            float 
    
  78.  * @param sacl 
    
  79.  *            int 
    
  80.  * @return String 
    
  81.  */  
    
  82. public static String formatNumber(double f, String pattern) {  
    
  83.     java.text.DecimalFormat num = new DecimalFormat(pattern);  
    
  84.     return num.format(f);  
    
  85. }  
    
  86. /** 
    
  87.  * 格式化为钱格式显示如:1,234,555.00 
    
  88.  * 
    
  89.  * @param money 
    
  90.  *            double 
    
  91.  * @return String 
    
  92.  */  
    
  93. public static String formatMoney(double money) {  
    
  94.     return formatNumber(money, "#,##0.00");  
    
  95. }  
    
  96. public static String indexUpStr(String str) {  
    
  97.     if (str == null || str.length() < 1) {  
    
  98.         return str;  
    
  99.     }  
    
  100.     String tmstr = "";  
    
  101.     tmstr = str.substring(0, 1);  
    
  102.     tmstr = tmstr.toUpperCase();  
    
  103.     tmstr += str.substring(1);  
    
  104.     return tmstr;  
    
  105. }  
    
  106. /** 
    
  107.  * 根据分隔符,将数字字符转换成整型数组 
    
  108.  * 
    
  109.  * @param str 
    
  110.  *            String 
    
  111.  * @param regex 
    
  112.  *            String 
    
  113.  * @return int 
    
  114.  */  
    
  115. public static int toIntsByRegex(String str, String regex) {  
    
  116.     if (str == null || "".equals(str.trim()))  
    
  117.         return null;  
    
  118.     String tms = str.split(regex);  
    
  119.     int tmi = new int;  
    
  120.     for (int i = 0; i < tms.length; i++) {  
    
  121.         tmi* = Integer.parseInt(tms*);  
    
  122.     }  
    
  123.     return tmi;  
    
  124. }  
    
  125. public static int toInts(String str) {  
    
  126.     return toIntsByRegex(str, ",");  
    
  127. }  
    
  128. /** 
    
  129.  * 将格式化的数字还原,如:1,234,567.00——>1234567.00 
    
  130.  * 
    
  131.  * @param formattedNumber 
    
  132.  * @return parseString 
    
  133.  */  
    
  134. public static String parseFormattedNumber(String formattedNumber) {  
    
  135.     java.text.DecimalFormat num = new DecimalFormat();  
    
  136.     String parseString = "";  
    
  137.     try {  
    
  138.         parseString = formatNumber(Double.parseDouble(num.parse(  
    
  139.                 formattedNumber).toString()), "#0.00");  
    
  140.     } catch (ParseException e) {  
    
  141.         e.printStackTrace();  
    
  142.     }  
    
  143.     return parseString;  
    
  144. }  
    
  145. public static void main(String args) throws ParseException {  
    
  146.     // System.out.println(indexUpStr("")+";");  
    
  147.     // toIntsByRegex(" ",",");  
    
  148.     // Date date = parseDateTime("2004-04-12 13:24");  
    
  149.     // System.out.println(formatDateTime(date));  
    
  150.     java.text.DecimalFormat num = new DecimalFormat();  
    
  151.     System.out.println(formatNumber(5234556, "#,##0.00%"));  
    
  152.     System.out.println(parseFormattedNumber("99999999"));  
    
  153.     System.out.println("interval: "  
    
  154.             + getIntervalOfDate("2005-05-01", "2006-06-12"));  
    
  155. }  
    
  156. /** 
    
  157.  * 将字符串数组转化为字符串,并用","连接 
    
  158.  * 
    
  159.  * @author 李奉学 
    
  160.  * @param arrayString 
    
  161.  * @return 带","分割的字符串 
    
  162.  */  
    
  163. public static String convertArrayToString(String arrayString) {  
    
  164.     String returnString = "";  
    
  165.     if (arrayString != null) {  
    
  166.         for (int i = 0; i < arrayString.length; i++) {  
    
  167.             // 第一个元素前不能加","  
    
  168.             if (StringUtils.isEmpty(returnString)) {  
    
  169.                 returnString = returnString + arrayString*;  
    
  170.             } else {  
    
  171.                 returnString = returnString + "," + arrayString*;  
    
  172.             }  
    
  173.         }  
    
  174.     }  
    
  175.     return returnString;  
    
  176. }  
    
  177. public static String getFileExt(String filename) {  
    
  178.     String fileext = "";  
    
  179.     String tmfilename = filename;  
    
  180.     if (tmfilename != null) {  
    
  181.         int l = tmfilename.indexOf(".") + 1;  
    
  182.         if (l == 0) {  
    
  183.             return "";  
    
  184.         }  
    
  185.         int tml = l;  
    
  186.         while (tml != 0) {  
    
  187.             try {  
    
  188.                 l = tml;  
    
  189.                 tml = tmfilename.indexOf(".", tml) + 1;  
    
  190.             } catch (Exception e) {  
    
  191.                 tml = 0;  
    
  192.             }  
    
  193.         }  
    
  194.         int length = tmfilename.length();  
    
  195.         if (l <= length && l != 1) {  
    
  196.             fileext = tmfilename.substring(l);  
    
  197.         } else {  
    
  198.             fileext = "";  
    
  199.         }  
    
  200.     }  
    
  201.     return fileext;  
    
  202. }  
    
  203. /** 
    
  204.  * 补齐字符长度 
    
  205.  * 
    
  206.  * @param str 
    
  207.  * @param length 
    
  208.  * @param repair 
    
  209.  * @return 
    
  210.  */  
    
  211. public static String fillStrLength(String str, int length, String repair) {  
    
  212.     for (int i = str.length(); i < length; i++) {  
    
  213.         str = repair + str;  
    
  214.     }  
    
  215.     return str;  
    
  216. }  
    
  217. /** 
    
  218.  * 比较字符串大小 
    
  219.  * 
    
  220.  * @param str1 
    
  221.  * @param str2 
    
  222.  * @return 
    
  223.  */  
    
  224. public static int strComp(String str1, String str2) {  
    
  225.     if (str1 == null || str2 == null) {  
    
  226.         return 0;  
    
  227.     }  
    
  228.     int str1Length = str1.length();  
    
  229.     int str2Length = str2.length();  
    
  230.     if (str2Length > str1Length) {  
    
  231.         str1 = fillStrLength(str1, str2Length, "0");  
    
  232.     } else if (str2Length < str1Length) {  
    
  233.         str1 = fillStrLength(str2, str1Length, "0");  
    
  234.     }  
    
  235.     int result = str1.compareTo(str2);  
    
  236.     if (result >= 1)  
    
  237.         result = 1;  
    
  238.     if (result <= -1)  
    
  239.         result = -1;  
    
  240.     return result;  
    
  241. }  
    
  242. /** 
    
  243.  * xuxu 返回毫秒 
    
  244.  * 
    
  245.  * @param date 
    
  246.  * @return 
    
  247.  */  
    
  248. public static long getMillis(Date date) {  
    
  249.     Calendar c = Calendar.getInstance();  
    
  250.     c.setTime(date);  
    
  251.     return c.getTimeInMillis();  
    
  252. }  
    
  253. /** 
    
  254.  * 日期相减 
    
  255.  * 
    
  256.  * @param date 
    
  257.  *            日期 
    
  258.  * @param day 
    
  259.  *            天数 
    
  260.  * @return 
    
  261.  */  
    
  262. public static Date subDate(Date date, int day) {  
    
  263.     Calendar c = Calendar.getInstance();  
    
  264.     c.setTimeInMillis(getMillis(date) - ((long) day) * 24 * 3600 * 1000);  
    
  265.     return c.getTime();  
    
  266. }  
    
  267. /** 
    
  268.  * 返回同一年内两个日期相差的天数 
    
  269.  * 
    
  270.  * @author 李奉学 
    
  271.  * @param date1 
    
  272.  * @param date2 
    
  273.  * @return 相差的天数 
    
  274.  * @throws ParseException 
    
  275.  */  
    
  276. public static int getIntervalOfDate(String date1, String date2)  
    
  277.         throws ParseException {  
    
  278.     int interval = 0;  
    
  279.     // 设置日期  
    
  280.     Calendar cal1 = Calendar.getInstance();  
    
  281.     cal1.setTime(parseDate(date1));  
    
  282.     Calendar cal2 = Calendar.getInstance();  
    
  283.     cal2.setTime(parseDate(date2));  
    
  284.     // 相差的天数  
    
  285.     interval = Math.abs(cal1.get(Calendar.DAY_OF_YEAR)  
    
  286.             - cal2.get(Calendar.DAY_OF_YEAR));  
    
  287.     return interval;  
    
  288. }  
    
  289. /** 
    
  290.  * 当前日期在一年中的周序号 
    
  291.  * 
    
  292.  * @param date 
    
  293.  * @return 周序号 
    
  294.  * @throws ParseException 
    
  295.  */  
    
  296. public static int getWeekIndexOfYear(String date) throws ParseException {  
    
  297.     int index = -1;  
    
  298.     Calendar cal = Calendar.getInstance();  
    
  299.     cal.setTime(parseDate(date));  
    
  300.     index=cal.get(Calendar.WEEK_OF_YEAR);  
    
  301.     return index;  
    
  302. }  
    
  303. }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。