More powerful formatting tool class Formatutils.java

Source: Internet
Author: User

  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. * More powerful formatting tool class
  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 year mm DD day");
  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. * Formatting time in Excel
  19. * @param date
  20. * @return
  21. */
  22. public static String formatdateforexceldate (date date) {
  23. return Exceldate.format (date);
  24. }
  25. /** 
  26. * Format the date as the name of the file
  27. * @param date
  28. * @return
  29. */
  30. public static String formatdateforfilename (date date) {
  31. return Filename.format (date);
  32. }
  33. /** 
  34. * Formatted date (accurate to seconds)
  35. *
  36. * @param date
  37. * @return
  38. */
  39. public static String Formatdatesecond (date date) {
  40. return Second.format (date);
  41. }
  42. /** 
  43. * Formatted date (accurate to seconds)
  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. * Formatted date (exact to day)
  61. *
  62. * @param date
  63. * @return
  64. */
  65. public static String formatdateday (date date) {
  66. return Day.format (date);
  67. }
  68. /** 
  69. * Formatted date (exact to day)
  70. *
  71. * @param date
  72. * @return
  73. */
  74. public static String formatdatedetailday (date date) {
  75. return Detailday.format (date);
  76. }
  77. /** 
  78. * Keep numbers of type Double two decimal places (rounded)
  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. * Convert String to date
  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. * Convert character date to 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. }

More powerful formatting tool class Formatutils.java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.