在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

java中常用的时间处理类TimeUtil

2016-12-20 13:17| 发布者: zhangjf| 查看: 479| 评论: 0

摘要: import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * 时间处理类 * @author Alan * @versio ...
  1. import java.text.ParseException;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Calendar;
  4. import java.util.Date;
  5. /**
  6. * 时间处理类
  7. * @author Alan
  8. * @version 2013-7-26
  9. */
  10. public class TimeUtil {
  11. private Calendar calendar=Calendar.getInstance();
  12. /**
  13. * 得到当前的时间,时间格式yyyy-MM-dd
  14. * @return
  15. */
  16. public String getCurrentDate(){
  17. SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
  18. return sdf.format(new Date());
  19. }
  20. /**
  21. * 得到当前的时间,自定义时间格式
  22. * y 年 M 月 d 日 H 时 m 分 s 秒
  23. * @param dateFormat 输出显示的时间格式
  24. * @return
  25. */
  26. public String getCurrentDate(String dateFormat){
  27. SimpleDateFormat sdf=new SimpleDateFormat(dateFormat);
  28. return sdf.format(new Date());
  29. }
  30. /**
  31. * 日期格式化,默认日期格式yyyy-MM-dd
  32. * @param date
  33. * @return
  34. */
  35. public String getFormatDate(Date date){
  36. SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
  37. return sdf.format(date);
  38. }
  39. /**
  40. * 日期格式化,自定义输出日期格式
  41. * @param date
  42. * @return
  43. */
  44. public String getFormatDate(Date date,String dateFormat){
  45. SimpleDateFormat sdf=new SimpleDateFormat(dateFormat);
  46. return sdf.format(date);
  47. }
  48. /**
  49. * 返回当前日期的前一个时间日期,amount为正数 当前时间后的时间 为负数 当前时间前的时间
  50. * 默认日期格式yyyy-MM-dd
  51. * @param field 日历字段
  52. * y 年 M 月 d 日 H 时 m 分 s 秒
  53. * @param amount 数量
  54. * @return 一个日期
  55. */
  56. public String getPreDate(String field,int amount){
  57. calendar.setTime(new Date());
  58. if(field!=null&&!field.equals("")){
  59. if(field.equals("y")){
  60. calendar.add(calendar.YEAR, amount);
  61. }else if(field.equals("M")){
  62. calendar.add(calendar.MONTH, amount);
  63. }else if(field.equals("d")){
  64. calendar.add(calendar.DAY_OF_MONTH, amount);
  65. }else if(field.equals("H")){
  66. calendar.add(calendar.HOUR, amount);
  67. }
  68. }else{
  69. return null;
  70. }
  71. return getFormatDate(calendar.getTime());
  72. }
  73. /**
  74. * 某一个日期的前一个日期
  75. * @param d,某一个日期
  76. * @param field 日历字段
  77. * y 年 M 月 d 日 H 时 m 分 s 秒
  78. * @param amount 数量
  79. * @return 一个日期
  80. */
  81. public String getPreDate(Date date,String field,int amount){
  82. calendar.setTime(date);
  83. if(field!=null&&!field.equals("")){
  84. if(field.equals("y")){
  85. calendar.add(calendar.YEAR, amount);
  86. }else if(field.equals("M")){
  87. calendar.add(calendar.MONTH, amount);
  88. }else if(field.equals("d")){
  89. calendar.add(calendar.DAY_OF_MONTH, amount);
  90. }else if(field.equals("H")){
  91. calendar.add(calendar.HOUR, amount);
  92. }
  93. }else{
  94. return null;
  95. }
  96. return getFormatDate(calendar.getTime());
  97. }
  98. /**
  99. * 某一个时间的前一个时间
  100. * @param date
  101. * @return
  102. * @throws ParseException
  103. */
  104. public String getPreDate(String date) throws ParseException{
  105. Date d=new SimpleDateFormat().parse(date);
  106. String preD=getPreDate(d,"d",1);
  107. Date preDate=new SimpleDateFormat().parse(preD);
  108. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  109. return sdf.format(preDate);
  110. }
  111. }
复制代码

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-7-8 17:09

Copyright 2015-2025 djqfx

返回顶部