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

功能更强大的格式化工具类 FormatUtils.java的更多相关文章

  1. 发一个比trace功能更强大debug工具,MonterDebugger

    经常看到兄弟说trace不出东西啊,这样给你调试会带来很多不便:加入说我们需要将运行时的debug信息和之前某个版本的进行比对:又加入说我们需要在运行时通过debug动态调整显示对象的属性:查看当前整 ...

  2. 让Docker功能更强大的10个开源工具

    让Docker功能更强大的10个开源工具 更好的管理.Web前端程序.更深入地了解容器应用程序,Docker生态系统正在迅速发展,这还得归功于其充满活力的开源社区. 软件项目的成功常常根据其催生的生态 ...

  3. pandas的筛选功能,跟excel的筛选功能类似,但是功能更强大。

    Select rows from a DataFrame based on values in a column -pandas 筛选 https://stackoverflow.com/questi ...

  4. 比 Navicat 还要好用、功能更强大的工具!

    DBeaver 是一个基于 Java 开发,免费开源的通用数据库管理和开发工具,使用非常友好的 ASL 协议.可以通过官方网站或者 Github 进行下载. 由于 DBeaver 基于 Java 开发 ...

  5. 一个比 Nginx 功能更强大的 Web 服务器

    公众号关注 「开源Linux」 回复「学习」,有我为您特别筛选的学习资料~ Caddy 简介 Caddy 是一个 Go 编写的 Web 服务器,类似于 Nginx,Caddy 提供了更加强大的功能,随 ...

  6. Excel Interactive View - 让页面表格的功能更强大

    MS Office Excel Services提供了一个非常强大的功能,允许你只需要在页面上添加非常少量的JS references,就可以让你在页面上将表格的内容以Excel图表的方式呈现.这里是 ...

  7. 让ecshop编辑器功能更强大

    ecshop后台的商品编辑和文章编辑使用的是FCKEDITOR 编辑器, 这个FCKEDITOR的工具条(toolbar)是可以自定义的,ECSHOP默认使用的是 normal ,属于中档功能, 下面 ...

  8. C++ folly库解读(三)Synchronized —— 比std::lock_guard/std::unique_lock更易用、功能更强大的同步机制

    目录 传统同步方案的缺点 folly/Synchronized.h 简单使用 Synchronized的模板参数 withLock()/withRLock()/withWLock() -- 更易用的加 ...

  9. 更强大的trim功能,过滤汉字等

    第一种方法:通过php自带的函数 <?php /* trim 去除一个字符串两端空格, rtrim 是去除一个字符串右部空格, ltrim 是去除一个字符串左部空格. */ ?> < ...

随机推荐

  1. 电话 SMS 邮件 网页 AppStore

    //调用safar打开网页 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.cnbl ...

  2. LCA(Tarjan)

    program LCA(Tarjan); type arr=record u,v,w,next:longint; end; ; maxm=; ..maxm*] of arr; last,lasq,an ...

  3. 根据终端分配不同的url地址

    <!DOCTYPE HTML> <html lang="zh-CN"> <head> <meta charset="UTF-8& ...

  4. iOS开发环境C语言基础 数组 函数

    1 求数组元素的最大值 1.1 问题 创建程序,实现查询数组中最大值的功能,需求为:创建一个长度为10的数组,数组内放置10个0~99之间(包含0,包含99)的随机数作为数组内容,要求查询出数组中的最 ...

  5. D - 排列

    #include<cstdio> #include<algorithm> #include<string.h> using namespace std; #defi ...

  6. ODBC接口规范

    第1章     ODBC API基础 1.1 ODBC API句柄 ODBC API 实现数据库操作的手段是句柄.在ODBC中,使用不同的句柄(HANDLE)来标志环境(environment).连接 ...

  7. Spring AOP配置文件

    在<aop:config>...</aop:config>报错: Multiple annotations found at this line: - cvc-complex- ...

  8. 元素设置position:fixed属性后IE下宽度无法100%延伸

    元素设置position:fixed属性后IE下宽度无法100%延伸 IE bug 出现条件: 1.div1设置position:fixed属性,并且想要width:100%的效果. 2.div2(下 ...

  9. 自动刷新ALV

    转自http://blog.sina.com.cn/s/blog_701594f40100l8ms.html ABAP:利用SAP定时器自动刷新ALV 曾于无意之中发现,SAP系统中有个名为CL_GU ...

  10. csu 1604 SunnyPig (bfs)

    Description SunnyPig is a pig who is much cleverer than any other pigs in the pigpen. One sunny morn ...