前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java常用工具类之时间转换

Java常用工具类之时间转换

作者头像
二十三年蝉
发布2018-02-28 11:52:57
7940
发布2018-02-28 11:52:57
举报
文章被收录于专栏:闻道于事闻道于事
代码语言:javascript
复制
import java.text.DecimalFormat;  
import java.text.ParseException;  
import java.text.SimpleDateFormat;  
import java.util.Date;  
  
/** 
 * 功能更强大的格式化工具类 
 */  
public class FormatUtils {  
    private static SimpleDateFormat second = new SimpleDateFormat(  
            "yy-MM-dd hh:mm:ss");  
  
    private static SimpleDateFormat day = new SimpleDateFormat("yyyy-MM-dd");  
    private static SimpleDateFormat detailDay = new SimpleDateFormat("yyyy年MM月dd日");  
    private static SimpleDateFormat fileName = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");  
    private static SimpleDateFormat tempTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
    private static SimpleDateFormat excelDate = new SimpleDateFormat("yyyy/MM/dd");  
      
    /** 
     * 格式化excel中的时间 
     * @param date 
     * @return 
     */  
    public static String formatDateForExcelDate(Date date) {  
        return excelDate.format(date);  
    }  
      
    /** 
     * 将日期格式化作为文件名 
     * @param date 
     * @return 
     */  
    public static String formatDateForFileName(Date date) {  
        return fileName.format(date);  
    }  
  
    /** 
     * 格式化日期(精确到秒) 
     *  
     * @param date 
     * @return 
     */  
    public static String formatDateSecond(Date date) {  
        return second.format(date);  
    }  
      
    /** 
     * 格式化日期(精确到秒) 
     *  
     * @param date 
     * @return 
     */  
    public static String tempDateSecond(Date date) {  
        return tempTime.format(date);  
    }  
  
    public static Date tempDateSecond(String str) {  
        try {  
            return tempTime.parse(str);  
        } catch (ParseException e) {  
            e.printStackTrace();  
        }  
        return new Date();  
    }  
    /** 
     * 格式化日期(精确到天) 
     *  
     * @param date 
     * @return 
     */  
    public static String formatDateDay(Date date) {  
        return day.format(date);  
    }  
      
    /** 
     * 格式化日期(精确到天) 
     *  
     * @param date 
     * @return 
     */  
    public static String formatDateDetailDay(Date date) {  
        return detailDay.format(date);  
    }  
  
    /** 
     * 将double类型的数字保留两位小数(四舍五入) 
     *  
     * @param number 
     * @return 
     */  
    public static String formatNumber(double number) {  
        DecimalFormat df = new DecimalFormat();  
        df.applyPattern("#0.00");  
        return df.format(number);  
    }  
  
    /** 
     * 将字符串转换成日期 
     *  
     * @param date 
     * @return 
     * @throws Exception 
     */  
    public static Date formateDate(String date) throws Exception {  
        return day.parse(date);  
    }  
      
    /** 
     * 将字符日期转换成Date 
     * @param date 
     * @return 
     * @throws Exception 
     */  
    public static Date parseStringToDate(String date) throws Exception {  
        return day.parse(date);  
    }  
      
    public static String formatDoubleNumber(double number) {  
        DecimalFormat df = new DecimalFormat("#");  
        return df.format(number);  
    }  
}  
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-01-18 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档