功能更强大的格式化工具类 FormatUtils.java

  1. package com.util;  
  2.   
  3. import java.text.DecimalFormat;  
  4. import java.text.ParseException;  
  5. import java.text.SimpleDateFormat;  
  6. import java.util.Date;  
  7.   
  8. /** 
  9.  * 功能更强大的格式化工具类 
  10.  */  
  11. public class FormatUtils {  
  12.     private static SimpleDateFormat second = new SimpleDateFormat(  
  13.             "yy-MM-dd hh:mm:ss");  
  14.   
  15.     private static SimpleDateFormat day = new SimpleDateFormat("yyyy-MM-dd");  
  16.     private static SimpleDateFormat detailDay = new SimpleDateFormat("yyyy年MM月dd日");  
  17.     private static SimpleDateFormat fileName = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");  
  18.     private static SimpleDateFormat tempTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  19.     private static SimpleDateFormat excelDate = new SimpleDateFormat("yyyy/MM/dd");  
  20.       
  21.     /** 
  22.      * 格式化excel中的时间 
  23.      * @param date 
  24.      * @return 
  25.      */  
  26.     public static String formatDateForExcelDate(Date date) {  
  27.         return excelDate.format(date);  
  28.     }  
  29.       
  30.     /** 
  31.      * 将日期格式化作为文件名 
  32.      * @param date 
  33.      * @return 
  34.      */  
  35.     public static String formatDateForFileName(Date date) {  
  36.         return fileName.format(date);  
  37.     }  
  38.   
  39.     /** 
  40.      * 格式化日期(精确到秒) 
  41.      *  
  42.      * @param date 
  43.      * @return 
  44.      */  
  45.     public static String formatDateSecond(Date date) {  
  46.         return second.format(date);  
  47.     }  
  48.       
  49.     /** 
  50.      * 格式化日期(精确到秒) 
  51.      *  
  52.      * @param date 
  53.      * @return 
  54.      */  
  55.     public static String tempDateSecond(Date date) {  
  56.         return tempTime.format(date);  
  57.     }  
  58.   
  59.     public static Date tempDateSecond(String str) {  
  60.         try {  
  61.             return tempTime.parse(str);  
  62.         } catch (ParseException e) {  
  63.             e.printStackTrace();  
  64.         }  
  65.         return new Date();  
  66.     }  
  67.     /** 
  68.      * 格式化日期(精确到天) 
  69.      *  
  70.      * @param date 
  71.      * @return 
  72.      */  
  73.     public static String formatDateDay(Date date) {  
  74.         return day.format(date);  
  75.     }  
  76.       
  77.     /** 
  78.      * 格式化日期(精确到天) 
  79.      *  
  80.      * @param date 
  81.      * @return 
  82.      */  
  83.     public static String formatDateDetailDay(Date date) {  
  84.         return detailDay.format(date);  
  85.     }  
  86.   
  87.     /** 
  88.      * 将double类型的数字保留两位小数(四舍五入) 
  89.      *  
  90.      * @param number 
  91.      * @return 
  92.      */  
  93.     public static String formatNumber(double number) {  
  94.         DecimalFormat df = new DecimalFormat();  
  95.         df.applyPattern("#0.00");  
  96.         return df.format(number);  
  97.     }  
  98.   
  99.     /** 
  100.      * 将字符串转换成日期 
  101.      *  
  102.      * @param date 
  103.      * @return 
  104.      * @throws Exception 
  105.      */  
  106.     public static Date formateDate(String date) throws Exception {  
  107.         return day.parse(date);  
  108.     }  
  109.       
  110.     /** 
  111.      * 将字符日期转换成Date 
  112.      * @param date 
  113.      * @return 
  114.      * @throws Exception 
  115.      */  
  116.     public static Date parseStringToDate(String date) throws Exception {  
  117.         return day.parse(date);  
  118.     }  
  119.       
  120.     public static String formatDoubleNumber(double number) {  
  121.         DecimalFormat df = new DecimalFormat("#");  
  122.         return df.format(number);  
  123.     }  
原文地址:https://www.cnblogs.com/swite/p/5168688.html