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("yyyy年MM月dd日 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(); } }
时间工具类
最新推荐文章于 2022-03-11 15:21:45 发布