时间工具类

public class DateUtils {
   /** 
     * 两个时间相差距离多少天多少小时多少分多少秒 
     * @param available_time 时间参数 1 格式:1990-01-01 12:00:00
     * @param type 时间参数 2 格式:2009-01-01 12:00:00
     * @return long[] 返回值为:{, , , } 
     */  
   public static long[] getDistanceTimes(String available_time,String type) {
      DateFormat df;
      if(type=="time"){
          df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      } else {
          df = new SimpleDateFormat("yyyy-MM-dd");
      }
      Date one;
      Date two;
      long day = 0;
      long hour = 0;
      long min = 0;
      long sec = 0;
      long diff = 0;
      try {
         one = new Date();
         two = df.parse(available_time);
         long time1 = one.getTime();
         long time2 = two.getTime();
         
         if (time1 < time2) {
            diff = time2 - time1;
         } else {
//          diff = time1 - time2;
            long[] times = {0 };
            return times;
         }
         day = diff / (24 * 60 * 60 * 1000);
         hour = (diff / (60 * 60 * 1000) - day * 24);
         min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
         sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
      } catch (Exception e) {
//       e.printStackTrace();
      }
      long interval = diff;
      long[] times = {interval , day, hour, min, sec };
      return times;
   }
   
   /**
    * 根据time获取 时分秒
    * @param time
    * @return
    */
   public static long[] getHourMinSecTimes(Long time) {
      long day = 0;
      long hour = 0;
      long min = 0;
      long sec = 0;
      try {
         day = time / (24 * 60 * 60 * 1000);
         hour = (time / (60 * 60 * 1000) - day * 24);
         min = ((time / (60 * 1000)) - day * 24 * 60 - hour * 60);
         sec = (time / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
      } catch (Exception e) {
      }
      long[] times = { day, hour, min, sec };
      return times;
   }
   
   public static String getCurrentTime(String format) {
      Date date = new Date();
      SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.getDefault());
      String currentTime = sdf.format(date);
      return currentTime;
   }

   public static String getCurrentTime() {
      return getCurrentTime("yyyyMMdd HH:mm");
   }
   public static String getCurrentTime1(){
      return getCurrentTime("yyyy-MM-dd");
   }
   
   /**
    * 将日期转换成星期
    * @param pTime 日期格式 yyyy-MM-dd
    * @return 转换后字符串 格式:""+"..."
    */
    public static String getWeek(String pTime) {
        String Week = "";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        try {
         c.setTime(format.parse(pTime));
        } catch (ParseException e) {
         e.printStackTrace();
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 1) {
           Week += "";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 2) {
           Week += "";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 3) {
           Week += "";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 4) {
           Week += "";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 5) {
           Week += "";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 6) {
           Week += "";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 7) {
           Week += "";
        }
        return Week;
       }


   /**
    * 根据String时间获取年份
    * @param startTime
    * @return
    */
   public static int getYear(String startTime){
      SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
      Date date = null;
      try {
         date = sdf.parse(startTime);
      } catch (ParseException e) {
         e.printStackTrace();
      }
      Calendar calendar = Calendar.getInstance();
      calendar.setTime(date);
      return calendar.get(Calendar.YEAR);
   }

   /**
    * 根据String时间获取月份
    * @param startTime
    * @return
    */
   public static int getMonth(String startTime){
      SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
      Date date = null;
      try {
         date = sdf.parse(startTime);
      } catch (ParseException e) {
         e.printStackTrace();
      }
      Calendar calendar = Calendar.getInstance();
      calendar.setTime(date);
      return calendar.get(Calendar.MONTH)+1;
   }

   /**
    * 根据String时间获取日
    * @param startTime
    * @return
    */
   public static int getDay(String startTime){
      SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
      Date date = null;
      try {
         date = sdf.parse(startTime);
      } catch (ParseException e) {
         e.printStackTrace();
      }
      Calendar calendar = Calendar.getInstance();
      calendar.setTime(date);
      return calendar.get(Calendar.DAY_OF_MONTH);
   }
   /**
    * 根据String时间获取周
    * @param startTime
    * @return
    */
   public static String getWeekByDay(String startTime){
      SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
      Date date = null;
      try {
         date = sdf.parse(startTime);
      } catch (ParseException e) {
         e.printStackTrace();
      }
      Calendar calendar = Calendar.getInstance();
      calendar.setTime(date);
      String weekStr = "";
      switch (calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH)){
         case 1:
            weekStr = "";
            break;
         case 2:
            weekStr = "";
            break;
         case 3:
            weekStr = "";
            break;
         case 4:
            weekStr = "";
            break;
      }
      return weekStr;
   }

   /**
    *将时间戳转换成时间
    * @param beginDate
    * @return
    */
   public static String timestampToDate(String beginDate) {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
      String sd = sdf.format(new Date(Long.parseLong(beginDate)));
      return sd;
   }
   /**
    *将时间戳转换成时间
    * @param beginDate
    * @return
    */
   public static String timestampToDate1(String beginDate) {
      SimpleDateFormat sdf = new SimpleDateFormat("HH:mm yyyy-MM-dd");
      String sd = sdf.format(new Date(Long.parseLong(beginDate)));
      return sd;
   }
   /**
    *将时间戳转换成时间
    * @param beginDate
    * @return
    */
   public static String timestampToDate2(String beginDate) {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM.dd-HH:mm");
      String sd = sdf.format(new Date(Long.parseLong(beginDate)));
      return sd;
   }
   //获取当前时间
   public static String getDate() {
      SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
      return formatter.format(new Date());
   }


   /*
    * 毫秒转化时分秒毫秒
    */
   public static String formatTime(Long ms) {
      Integer ss = 1000;
      Integer mi = ss * 60;
      Integer hh = mi * 60;
      Integer dd = hh * 24;

      Long day = ms / dd;
      Long hour = (ms - day * dd) / hh;
      Long minute = (ms - day * dd - hour * hh) / mi;
      Long second = (ms - day * dd - hour * hh - minute * mi) / ss;
      Long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;

      StringBuffer sb = new StringBuffer();
      if(day > 0) {
         sb.append(day+"");
      }
      if(hour > 0) {
         sb.append(hour+"小时");
      }
      if(minute > 0) {
         sb.append(minute+"");
      }
      if(second > 0) {

         if(milliSecond > 0) {
            second++;
         }
         sb.append((second)+"");
      }else {
         if(milliSecond > 0) {
            second++;
         }
         sb.append((second)+"");
      }
//    if(milliSecond > 0) {
//       sb.append(milliSecond+"毫秒");
//    }
      return sb.toString();
   }
   //-------视频播放时间转换 begin-----------
   /**
    * 将毫秒转换成时间
    * @param millisecond
    * @return
    */
   public static String millConVertDate(int millisecond){
      Date d = new Date(millisecond);
      SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
//    System.out.println(sdf.format(d));
      return sdf.format(d);
   }

   public static String transtimetostr(int time) {
      int i = time / 1000;
      int hour = i / (60 * 60);
      int minute = i / 60 % 60;
      int second = i % 60;

      StringBuilder sb = new StringBuilder();
      sb.append(hour >= 10 ? hour : "0" + hour);
      sb.append(":");
      sb.append(minute >= 10 ? minute : "0" + minute);
      sb.append(":");
      sb.append(second >= 10 ? second : "0" + second);

      return sb.toString();
   }
   
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值