Summary of String Conversion into time format in Java

Source: Internet
Author: User
Tags time and date

There is such a string: "20070911121547 ",

Converted to time format: 12:15:47

Java Code
  1. Public Class Bb {
  2. Public Static Void Main (string [] ARGs ){
  3. // Todo auto-generated method stub
  4. Simpledateformat df =New Simpledateformat ("Yyyymmddhhmmss");
  5. String datestring ="20071128175545";
  6. Try {
  7. Date = DF. parse (datestring );
  8. System. Out. println (DF. Format (date ));
  9. }Catch (Exception ex ){
  10. System. Out. println (ex. getmessage ());
  11. }
  12. }
  13. }
 
Public Class BB {public static void main (string [] ARGs) {// todo auto-generated method stub simpledateformat df = new simpledateformat ("yyyymmddhhmmss "); string datestring = "20071128175545"; try {date = DF. parse (datestring); system. out. println (DF. format (date);} catch (exception ex) {system. out. println (ex. getmessage ());}}}

Time is nothing more than the string type to the time type, or the time type to the string type, there is the processing of the previous time, the next time, and so on.

I made an example myself:

Java Code
  1. Package Com. Observe. monitoralarm. util;
  2. Import Java. Text. parseexception;
  3. Import Java. Text. simpledateformat;
  4. Import Java. Util. calendar;
  5. Import Java. Util. date;
  6. /**
  7. * Time help class
  8. * @ Version $ ID: dateutil.Java, V 1.1 04:29:52 Linan exp $
  9. * @ Author Linan
  10. */
  11. Public Class Dateutil {
  12. Private Calendar calendar = calendar. getinstance ();
  13. /**
  14. * Obtain the current time, in the format of yyyy-mm-dd.
  15. * @ Return
  16. */
  17. Public String getcurrentdate (){
  18. Simpledateformat SDF =New Simpledateformat ("Yyyy-mm-dd");
  19. Return SDF. Format (New Date ());
  20. }
  21. /**
  22. * Get the current time, custom time format
  23. * Y, M, D, H, M, minutes, seconds
  24. * @ Param dateformat the time format displayed by the output
  25. * @ Return
  26. */
  27. Public String getcurrentdate (string dateformat ){
  28. Simpledateformat SDF =New Simpledateformat (dateformat );
  29. Return SDF. Format (New Date ());
  30. }
  31. /**
  32. * Format the date. The default date format is yyyy-mm-dd.
  33. * @ Param date
  34. * @ Return
  35. */
  36. Public String getformatdate (date ){
  37. Simpledateformat SDF =New Simpledateformat ("Yyyy-mm-dd");
  38. Return SDF. Format (date );
  39. }
  40. /**
  41. * Date formatting: Custom output Date Format
  42. * @ Param date
  43. * @ Return
  44. */
  45. Public String getformatdate (date, string dateformat ){
  46. Simpledateformat SDF =New Simpledateformat (dateformat );
  47. Return SDF. Format (date );
  48. }
  49. /**
  50. * Returns the previous time and date of the current date. amount is the time after the current time as a negative value before the current time.
  51. * Default date format: yyyy-mm-dd
  52. * @ Param field calendar Field
  53. * Y, M, D, H, M, minutes, seconds
  54. * @ Param amount quantity
  55. * @ Return a date
  56. */
  57. Public String getpredate (string field,Int Amount ){
  58. Calendar. settime (New Date ());
  59. If(Field! =Null&&! Field. Equals ("")){
  60. If(Field. Equals ("Y")){
  61. Calendar. Add (calendar. Year, amount );
  62. }Else If(Field. Equals ("M")){
  63. Calendar. Add (calendar. Month, amount );
  64. }Else If(Field. Equals ("D")){
  65. Calendar. Add (calendar. day_of_month, amount );
  66. }Else If(Field. Equals ("H")){
  67. Calendar. Add (calendar. Hour, amount );
  68. }
  69. }Else{
  70. Return Null;
  71. }
  72. Return Getformatdate (calendar. gettime ());
  73. }
  74. /**
  75. * The previous date of a certain date
  76. * @ Param D, a date
  77. * @ Param field calendar Field
  78. * Y, M, D, H, M, minutes, seconds
  79. * @ Param amount quantity
  80. * @ Return a date
  81. */
  82. Public String getpredate (date D, string field,Int Amount ){
  83. Calendar. settime (d );
  84. If(Field! =Null&&! Field. Equals ("")){
  85. If(Field. Equals ("Y")){
  86. Calendar. Add (calendar. Year, amount );
  87. }Else If(Field. Equals ("M")){
  88. Calendar. Add (calendar. Month, amount );
  89. }Else If(Field. Equals ("D")){
  90. Calendar. Add (calendar. day_of_month, amount );
  91. }Else If(Field. Equals ("H")){
  92. Calendar. Add (calendar. Hour, amount );
  93. }
  94. }Else{
  95. Return Null;
  96. }
  97. Return Getformatdate (calendar. gettime ());
  98. }
  99. /**
  100. * The previous time of a certain time
  101. * @ Param date
  102. * @ Return
  103. * @ Throws parseexception
  104. */
  105. Public String getpredate (string date) Throws Parseexception {
  106. Date d =New Simpledateformat (). parse (date );
  107. String Pred = getpredate (D,"D",1);
  108. Date predate =New Simpledateformat (). parse (Pred );
  109. Simpledateformat SDF =New Simpledateformat ("Yyyy-mm-dd");
  110. Return SDF. Format (predate );
  111. }
  112. }

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.