时间处理的工具类
来源:互联网 发布:淘宝身份证开过店 编辑:程序博客网 时间:2024/05/08 04:37
package com.huaiye.ecs.common.utils;import java.text.DecimalFormat;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.Locale;public class TimeUtil { private final static String TIME_FORMAT = "HH:mm"; private final static String TIME_FORMAT_YMDHMS = "yyyy-MM-dd HH:mm:ss"; private final static String TIME_FORMAT_YMDHM = "yyyy-MM-dd HH:mm"; private final static String TIME_FORMAT_MDHMS = "MM-dd HH:mm:ss"; private final static String TIME_FORMAT_YMD = "yyyy-MM-dd"; private final static String TIME_FORMAT_YMD_2 = "yyyyMMdd"; /** * 将时间显示为HH:mm:SS,注意HH不是24小时制也不是12小时制,是累计的小时数, * 可能是大于24,此方法主要用于播放器界面的时间显示 * * @param millisecond 时间毫秒 * @return HH:mm:SS */ public static String formatForPlayer(long millisecond) { DecimalFormat df = new DecimalFormat("00"); StringBuilder timeString = new StringBuilder(); long hour = millisecond / (1000 * 60 * 60); long minute = millisecond % (1000 * 60 * 60) / (1000 * 60); long second = millisecond % (1000 * 60) / 1000; timeString.append(df.format(hour)).append(":"); timeString.append(df.format(minute)).append(":") .append(df.format(second)); return timeString.toString(); } /** * 获取当前时间的小时和分钟 * * @return HH:mm */ public static String getCurFormatTime() { SimpleDateFormat df = new SimpleDateFormat(TIME_FORMAT, Locale.CHINA); return df.format(new Date()); } /** * 获取当前时间的yyyy-MM-dd HH:mm:ss格式 * * @return yyyy-MM-dd HH:mm:ss */ public static String getCurYMDTime() { SimpleDateFormat df = new SimpleDateFormat(TIME_FORMAT_YMDHMS, Locale.CHINA); return df.format(new Date()); } public static String getCurYMDFTime() { SimpleDateFormat df = new SimpleDateFormat(TIME_FORMAT_YMDHM, Locale.CHINA); return df.format(new Date()); } //获取上一天的时间 public static String getYesTody(Date date) { SimpleDateFormat df = new SimpleDateFormat(TIME_FORMAT_YMDHMS, Locale.CHINA); return df.format(date); } /** * 将yyyy-MM-dd HH:mm:ss格式的时间转变为MM-dd HH:mm:ss * * @param dateString 时间字符串 * @return 转变后的值 */ public static String formatToMDHms(String dateString) { String result = ""; Date date = getDateFromString(dateString); if (date != null) { result = getDateStringMdHms(date); } return result; } /** * 将yyyy-MM-dd HH:mm:ss格式的时间转变为Date类型 * * @param dateString 时间字符串 * @return 转变后的值 */ public static Date getDateFromString(String dateString) { SimpleDateFormat df = new SimpleDateFormat(TIME_FORMAT_YMDHMS, Locale.CHINA); Date date = null; try { date = df.parse(dateString); } catch (ParseException e) { e.printStackTrace(); } return date; } /** * 将"yyyy-MM-dd HH:mm:ss"格式的日期转为long类型的time * * @param timeString 时间字符串 * @return long类型的time */ public static long getCommonTime(String timeString) { SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT_YMDHMS, Locale.CHINA); try { Date d = sdf.parse(timeString); return d.getTime(); } catch (ParseException e) { e.printStackTrace(); } return 0; } /** * 将"yyyy-MM-dd HH:mm:ss"格式的日期转为"yyyy-MM-dd"格式 * * @param timeString 时间字符串 * @return long类型的time */ public static String formatDateStr(String timeString) { SimpleDateFormat sdfBefore = new SimpleDateFormat(TIME_FORMAT_YMDHMS, Locale.CHINA); SimpleDateFormat sdfAfter = new SimpleDateFormat(TIME_FORMAT_YMD, Locale.CHINA); try { return sdfAfter.format(sdfBefore.parse(timeString)); } catch (ParseException e) { e.printStackTrace(); } return ""; } /** * 将"yyyy-MM-dd格式的日期转为long类型的time * * @param timeString 时间字符串 * @return long类型的time */ public static long getYMDCommonTime(String timeString) { SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT_YMD, Locale.CHINA); try { Date d = sdf.parse(timeString); return d.getTime(); } catch (ParseException e) { e.printStackTrace(); } return 0; } public static long getHoursLong() { String curFormatTime = getCurFormatTime(); SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT, Locale.CHINA); try { Date d = sdf.parse(curFormatTime); return d.getTime(); } catch (ParseException e) { e.printStackTrace(); } return 0; } /** * 将日期转换为yyyy-MM-dd HH:mm:ss格式字符串 * * @param date 日期 * @return yyyy-MM-dd HH:mm:ss */ public static String getDateString(Date date) { SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT_YMDHMS, Locale.CHINA); return sdf.format(date); } /** * 将日期转换为MM-dd HH:mm:ss格式字符串 * * @param date 日期 * @return MM-dd HH:mm:ss */ public static String getDateStringMdHms(Date date) { SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT_MDHMS, Locale.CHINA); return sdf.format(date); } /** * 将日期转换为yyyy-MM-dd格式字符串 * * @param date 日期 * @return yyyy-MM-dd */ public static String getYMDString(Date date) { SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT_YMD, Locale.CHINA); return sdf.format(date); } /** * 将日期转换为yyyy-MM-dd格式字符串 * * @param date 日期 * @return yyyyMMdd */ public static String getYMDString2(Date date) { SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT_YMD_2, Locale.CHINA); return sdf.format(date); } /** * 得到某年某月的第一天 */ public static String getFirstDayOfMonth(int year, int month) { Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, year); cal.set(Calendar.MONTH, month - 1); cal.set(Calendar.DAY_OF_MONTH, cal.getMinimum(Calendar.DATE)); return new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA).format(cal.getTime()); } /** * 得到某年某月的最后一天 */ public static String getLastDayOfMonth(int year, int month) { Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, year); cal.set(Calendar.MONTH, month - 1); cal.set(Calendar.DAY_OF_MONTH, 1); int value = cal.getActualMaximum(Calendar.DAY_OF_MONTH); cal.set(Calendar.DAY_OF_MONTH, value); return new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA).format(cal.getTime()); } public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); public static String getDate(long time) { String times = ""; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); times = sdf.format(time); return times; } public static String getTimeVideo(long time) { String times = ""; SimpleDateFormat sdf = new SimpleDateFormat("dd HH:mm:ss"); times = sdf.format(time); return times; } public static String getDateDay(long time) { String times = ""; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); times = sdf.format(time); return times; } public static Calendar StringToCalendar(String time) { Date date = null; Calendar calendar = null; try { date = sdf.parse(time); calendar = Calendar.getInstance(); calendar.setTime(date); } catch (ParseException e) { e.printStackTrace(); } return calendar; } public static Calendar LongToCalendar(long time) { Date date = null; Calendar calendar = null; try { date = new Date(time); calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(calendar.MONTH, -3); } catch (Exception e) { e.printStackTrace(); } return calendar; } public static int getDistanceMonth(String date1, String date2) { int distance = 0; int distanceTime = 0; Calendar calendar1 = Calendar.getInstance(); Calendar calendar2 = Calendar.getInstance(); try { calendar1.setTime(sdf.parse(date1)); calendar2.setTime(sdf.parse(date2)); distance = calendar2.get(Calendar.MONTH) - calendar1.get(Calendar.MONTH); distanceTime = calendar2.get(Calendar.DAY_OF_MONTH) - calendar1.get(Calendar.DAY_OF_MONTH); } catch (ParseException e) { e.printStackTrace(); } if (distance == 3 && distanceTime > 0) distance = 4; return distance; } /** * 获取两个时间点之间的时间间隔 * * @param date1 第一个时间点 * @param date2 第二个时间点 * @return 时间间隔 */ public static String getDistanceTime(String date1, String date2) { SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT_YMDHMS); try { long time1 = sdf.parse(date1).getTime(); long time2 = sdf.parse(date2).getTime(); int second = (int) ((time2 - time1) / 1000); return getVideoLength(second); } catch (ParseException e) { e.printStackTrace(); return "0"; } } /** * 获取两个时间点之间的时间间隔 * * @param date1 第一个时间点 * @param date2 第二个时间点 * @return 时间间隔 */ public static String getDistanceTextTime(String date1, String date2) { SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT_YMDHMS); try { long time1 = sdf.parse(date1).getTime(); long time2 = sdf.parse(date2).getTime(); int second = (int) ((time2 - time1) / 1000); return getVideoTextLength(second); } catch (ParseException e) { e.printStackTrace(); return "0"; } } /** * 获取格式化之后的视频时间长度 * * @return 视频时间 */ public static String getVideoTextLength(int videoSecond) { int hour = 0; int minute = 0; int second = 0; if (videoSecond > 0) { hour = videoSecond / (60 * 60); minute = (videoSecond - hour * 60 * 60) / 60; second = videoSecond % 60; } int time_hour = Integer.parseInt(formatTimeUnite(hour)); int time_minute = Integer.parseInt(formatTimeUnite(minute)); int time_second = Integer.parseInt(formatTimeUnite(second)); if (time_hour > 0) { return time_hour + "小时" + time_minute + "分钟" + time_second + "秒"; } if (time_minute > 0) { return time_minute + "分钟" + time_second + "秒"; } if (time_second > 0) { return time_second + "秒"; } return "0"; } public static String getDistanceTime(long date2) { int timeLong = (int) (date2 * 1000); int hour = new Long(timeLong / (1000 * 60 * 60)).intValue(); int tempLeft_minute = new Long(timeLong % (1000 * 60 * 60)) .intValue(); int minute = new Long(tempLeft_minute / (1000 * 60)).intValue(); int tempLeft_second = new Long(tempLeft_minute % (1000 * 60)) .intValue(); int second = new Long(tempLeft_second / 1000).intValue(); StringBuilder sb = new StringBuilder(); if (hour > 0) { if (hour > 9) sb.append(hour + ":"); else sb.append("0" + hour + ":"); } else { sb.append("00:"); } if (minute > 0) { if (minute > 9) sb.append(minute + ":"); else sb.append("0" + minute + ":"); } else { sb.append("00:"); } if (second > 0) { if (second > 9) sb.append(second + ""); else sb.append("0" + second + ""); } else { sb.append("00:"); } return sb.toString(); } public static Calendar getCurrentCalendar() { Calendar calendar = Calendar.getInstance(); return calendar; } /** * 获取格式化之后的视频时间长度 * * @return 视频时间 */ public static String getVideoLength(int videoSecond) { int hour = 0; int minute = 0; int second = 0; if (videoSecond > 0) { hour = videoSecond / (60 * 60); minute = (videoSecond - hour * 60 * 60) / 60; second = videoSecond % 60; } return formatTimeUnite(hour) + ":" + formatTimeUnite(minute) + ":" + formatTimeUnite(second); } /** * 格式化时间组成单元 * * @param num 时间 * @return 格式化后的时间 */ private static String formatTimeUnite(int num) { if (num > 9) { return String.valueOf(num); } else { return "0" + num; } } /** * 转换成计时时间 00:00:00 * * @param ttime * @return */ public static String timeCalculate(long ttime) { long daysuuu, hoursuuu, minutesuuu, secondsuuu; String daysT = "", restT = ""; daysuuu = (Math.round(ttime) / 86400); hoursuuu = (Math.round(ttime) / 3600) - (daysuuu * 24); minutesuuu = (Math.round(ttime) / 60) - (daysuuu * 1440) - (hoursuuu * 60); secondsuuu = Math.round(ttime) % 60; if (daysuuu == 1) daysT = String.format("%d day ", daysuuu); if (daysuuu > 1) daysT = String.format("%d days ", daysuuu); restT = String.format("%02d:%02d:%02d", hoursuuu, minutesuuu, secondsuuu); return daysT + restT; }}
0 0
- 时间处理的工具类
- 处理时间的工具类
- 时间处理工具类
- 时间处理工具类
- 时间处理工具类
- 时间处理工具类
- TimeUtils,有关时间处理的工具类
- TimeUtils 有关时间处理的工具类
- DayUtils 处理开始时间和结束时间的工具类
- 日期时间处理工具类
- JAVA时间处理工具类
- java时间处理工具类
- java时间处理工具类
- 时间处理工具类TimeUtil
- Java时间处理工具类
- 时间转换处理工具类
- Java时间处理工具类
- 处理日期时间工具类
- IE中使用Uploadify文件上传插件报错: SCRIPT5007: 缺少对象
- 史上最全的ReactNative视频
- 2017.1.10 算法测试题集 - 1002 - 相等二叉树
- 史上最大DDoS攻击的本质与防范
- 多线程-1、NSThread
- 时间处理的工具类
- Deep learning 的一点点笔记
- Multi-Class Classification Tutorial with the Keras Deep Learning Library
- CSS滤镜效果(俗称毛玻璃)
- 洛谷 1120 小木棍 [数据加强版]
- java HashMap--统计其中有相同value的key的个数
- java中String,StringBuffer,StringBuilder拼接效率对比
- SLF4J: Class path contains multiple SLF4J bindings.
- 墨香带你学Launcher之(三)-绑定屏幕、图标、文件夹和Widget