日期工具类DateUtils

  1. package date.util;
  2. import java.text.ParseException;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Calendar;
  5. import java.util.Date;
  6. public class DateUtils {
  7. static final String formatPattern = "yyyy-MM-dd";
  8. static final String formatPattern_Short = "yyyyMMdd";
  9. /**
  10. * 获取当前日期
  11. * @return
  12. */
  13. public static String getCurrentDate(){
  14. SimpleDateFormat format = new SimpleDateFormat(formatPattern);
  15. return format.format(new Date());
  16. }
  17. /**
  18. * 获取制定毫秒数之前的日期
  19. * @param timeDiff
  20. * @return
  21. */
  22. public static String getDesignatedDate(long timeDiff){
  23. SimpleDateFormat format = new SimpleDateFormat(formatPattern);
  24. long nowTime = System.currentTimeMillis();
  25. long designTime = nowTime - timeDiff;
  26. return format.format(designTime);
  27. }
  28. /**
  29. *
  30. * 获取前几天的日期
  31. */
  32. public static String getPrefixDate(String count){
  33. Calendar cal = Calendar.getInstance();
  34. int day = 0-Integer.parseInt(count);
  35. cal.add(Calendar.DATE,day); // int amount 代表天数
  36. Date datNew = cal.getTime();
  37. SimpleDateFormat format = new SimpleDateFormat(formatPattern);
  38. return format.format(datNew);
  39. }
  40. /**
  41. * 日期转换成字符串
  42. * @param date
  43. * @return
  44. */
  45. public static String dateToString(Date date){
  46. SimpleDateFormat format = new SimpleDateFormat(formatPattern);
  47. return format.format(date);
  48. }
  49. /**
  50. * 字符串转换日期
  51. * @param str
  52. * @return
  53. */
  54. public static Date stringToDate(String str){
  55. //str = " 2008-07-10 19:20:00 " 格式
  56. SimpleDateFormat format = new SimpleDateFormat(formatPattern);
  57. if(!str.equals("")&&str!=null){
  58. try {
  59. return format.parse(str);
  60. } catch (ParseException e) {
  61. // TODO Auto-generated catch block
  62. e.printStackTrace();
  63. }
  64. }
  65. return null;
  66. }
  67. //java中怎样计算两个时间如:“21:57”和“08:20”相差的分钟数、小时数 java计算两个时间差小时 分钟 秒 .
  68. public void timeSubtract(){
  69. SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  70. Date begin = null;
  71. Date end = null;
  72. try {
  73. begin = dfs.parse("2004-01-02 11:30:24");
  74. end = dfs.parse("2004-03-26 13:31:40");
  75. } catch (ParseException e) {
  76. e.printStackTrace();
  77. }
  78. long between = (end.getTime() - begin.getTime()) / 1000;// 除以1000是为了转换成秒
  79. long day1 = between / (24 * 3600);
  80. long hour1 = between % (24 * 3600) / 3600;
  81. long minute1 = between % 3600 / 60;
  82. long second1 = between % 60;
  83. System.out.println("" + day1 + "天" + hour1 + "小时" + minute1 + "分"
  84. + second1 + "秒");
  85. }
  86. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值