Java Date Tool class, Java Time Tool class, Java time format

Source: Internet
Author: User
Tags dateformat

Java Date Tool class, Java Time Tool class, Java time format

>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>

? Copyright Sweet Potato Yiu February 4, 2017 15:03:27 Saturday

http://www.cnblogs.com/fanshuyao/

Annex Download See: Http://fanshuyao.iteye.com/blog/2355386.

Java code
  1. Public static final String Date_time_pattern = "Yyyy-mm-dd HH:mm:ss";
  2. Public static final String Date_pattern = "Yyyy-mm-dd";
  3. /** 
  4. * Date Added minus days
  5. * @param date if NULL, the current time
  6. * @param days plus minus
  7. * @param includetime whether to include a time or seconds, true to include
  8. * @return
  9. * @throws ParseException
  10. */
  11. Public static Date dateAdd (date date, int days, boolean includetime) throws parseexception{
  12. if (date = = null) {
  13. Date = new Date ();
  14. }
  15. if (!includetime) {
  16. SimpleDateFormat SDF = new SimpleDateFormat (Strutils.date_pattern);
  17. Date = Sdf.parse (Sdf.format (date));
  18. }
  19. Calendar cal = Calendar.getinstance ();
  20. Cal.settime (date);
  21. Cal.add (Calendar.date, days);
  22. return Cal.gettime ();
  23. }
  24. /**
  25. * Time formatted into a string
  26. * @param date Date
  27. * @param pattern if empty, then YYYY-MM-DD
  28. * @return
  29. * @throws ParseException
  30. */
  31. Public static string DateFormat (date date, string pattern) throws parseexception{
  32. if (Strutils.isblank (pattern)) {
  33. pattern = Strutils.date_pattern;
  34. }
  35. SimpleDateFormat SDF = new SimpleDateFormat (pattern);
  36. return Sdf.format (date);
  37. }
  38. /**
  39. * String parsed into a time object
  40. * @param datetimestring String
  41. * @param pattern if empty, then YYYY-MM-DD
  42. * @return
  43. * @throws ParseException
  44. */
  45. Public static Date dateparse (string datetimestring, string pattern) throws parseexception{
  46. if (Strutils.isblank (pattern)) {
  47. pattern = Strutils.date_pattern;
  48. }
  49. SimpleDateFormat SDF = new SimpleDateFormat (pattern);
  50. return Sdf.parse (datetimestring);
  51. }
  52. /**
  53. * Format Date time as a string with only date
  54. * @param dateTime Date
  55. * @return
  56. * @throws ParseException
  57. */
  58. Public static String datetimetodatestring (Date dateTime) throws parseexception{
  59. String datetimestring = Strutils.dateformat (DateTime, Strutils.date_time_pattern);
  60. return datetimestring.substring (0, 10);
  61. }
  62. /**
  63. * When, minutes, and seconds are 00:00:00, the datetime is formatted as a date-only string,
  64. * When, minutes and seconds are not 00:00:00, return directly
  65. * @param dateTime Date
  66. * @return
  67. * @throws ParseException
  68. */
  69. Public static String Datetimetodatestringiftimeendzero (Date dateTime) throws parseexception{
  70. String datetimestring = Strutils.dateformat (DateTime, Strutils.date_time_pattern);
  71. if (Datetimestring.endswith ("00:00:00")) {
  72. return datetimestring.substring (0, 10);
  73. }else{
  74. return datetimestring;
  75. }
  76. }
  77. /**
  78. * Format Date time as Date object, and Dateparse
  79. * @param dateTime Date
  80. * @return Date
  81. * @throws ParseException
  82. */
  83. Public static Date datetimetodate (date DateTime) throws parseexception{
  84. Calendar cal = Calendar.getinstance ();
  85. Cal.settime (DateTime);
  86. Cal.set (Calendar.hour_of_day, 0);
  87. Cal.set (Calendar.minute, 0);
  88. Cal.set (Calendar.second, 0);
  89. Cal.set (Calendar.millisecond, 0);
  90. return Cal.gettime ();
  91. }

>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>

? Copyright Sweet Potato Yiu February 4, 2017 15:03:27 Saturday

http://www.cnblogs.com/fanshuyao/

Java Date Tool class, Java Time Tool class, Java time format

Related Article

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.