日期工具类DateUtils

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值