积累的操作时间的工具类dateutil
来源:互联网 发布:淘宝客服晚班在家上班 编辑:程序博客网 时间:2024/04/30 00:22
dateutil之获取两个时间相差多少天和指定时间的前后N天日期
获取两个字符串类型的日期相差多少天,第一个时间比第二个时间小N天
public static int daysBetween(String smdate,String bdate) throws ParseException, java.text.ParseException{ SimpleDateFormat sdf=new SimpleDateFormat(DATE_FORMAT_SHORT); Calendar cal = Calendar.getInstance(); cal.setTime(sdf.parse(smdate)); long time1 = cal.getTimeInMillis(); cal.setTime(sdf.parse(bdate)); long time2 = cal.getTimeInMillis(); long between_days=(time2-time1)/(1000*3600*24); return Integer.parseInt(String.valueOf(between_days)); }
获取两个date类型的日期相差多少天,第一个时间比第二个时间小N天
/** * 计算两个日期之间相差的天数 * @param smdate 较小的时间 * @param bdate 较大的时间 * @return 相差天数 * @throws ParseException * @throws java.text.ParseException */ public static int daysBetween(Date smdate,Date bdate) throws ParseException, java.text.ParseException { SimpleDateFormat sdf=new SimpleDateFormat(DATE_FORMAT_SHORT); smdate=sdf.parse(sdf.format(smdate)); bdate=sdf.parse(sdf.format(bdate)); Calendar cal = Calendar.getInstance(); cal.setTime(smdate); long time1 = cal.getTimeInMillis(); cal.setTime(bdate); long time2 = cal.getTimeInMillis(); long between_days=(time2-time1)/(1000*3600*24); return Integer.parseInt(String.valueOf(between_days)); }
得到指定日期的前N天或者后N天 前N个月或者后N个月 前N秒或者后N秒 前N小时或者后N小时的字符串日期
/** * 得到指定日期的前后几天日期 * @param format 格式 * @return 字符串日期 */public static String beforeAfterNDaysDate(Date dt,int type,Integer day,String format) {Date dBefore = new Date();Calendar calendar = Calendar.getInstance(); // 得到日历calendar.setTime(dt);// 把当前时间赋给日历calendar.add(type, day); // 设置为前后N天dBefore = calendar.getTime(); // 得到前一天的时间SimpleDateFormat sdf = new SimpleDateFormat(format); // 设置时间格式String timed = sdf.format(dBefore); // 格式化前一天return timed;}
得到指定日期的最后一天
//得到指定日期的最后一天public static Date getLastDayOfMonth(Date dt) {Calendar calendar = Calendar.getInstance();calendar.setTime(dt);// 把当前时间赋给日历calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + 1);calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DATE));calendar.set(Calendar.HOUR_OF_DAY, 0);calendar.set(Calendar.SECOND, 0);calendar.set(Calendar.MINUTE, 0);return calendar.getTime();}
/** * 字符串转日期 * @return Date */public static Date parseStringToDate(String thedate, String format) throws java.text.ParseException {DateFormat sdf = new SimpleDateFormat(format);Date dd1 = null; if(thedate != null && !thedate.equals("")){dd1 = sdf.parse(thedate); }return dd1;}
日期转字符串
/** * 日期转字符串 * * @return String */public static String parseDateToString(Date thedate, String format) {DateFormat df = new SimpleDateFormat(format);if (thedate != null) {return df.format(thedate.getTime());}return "";}
将时间戳转化为时间
/** * 将时间戳转化为时间 * @param seconds * @param format * @return * @throws java.text.ParseException */public static String timeStamp2Date(String seconds, String format) throws Exception { if (seconds == null || seconds.isEmpty() || seconds.equals("null")) { return ""; } if (format == null || format.isEmpty()) format = "yyyy-MM-dd HH:mm:ss"; SimpleDateFormat sdf = new SimpleDateFormat(format); String sd = sdf.format(new Date(Long.parseLong(seconds))); return sd; }
1 0
- 积累的操作时间的工具类dateutil
- 关于日期时间操作工具类DateUtil(三) -----对系统时间的操作.
- 关于日期时间操作工具类DateUtil(一)-----对java.util.Date 的操作.
- 关于日期时间操作工具类DateUtil(二) -----对java.sql.Timestamp 的操作.
- 时间工具类[DateUtil]
- 时间工具类(DateUtil)
- 时间工具类(DateUtil)
- 时间工具类(DateUtil)
- java开发中常用的日期时间工具类 DateUtil
- 获取指定(当前)月的天数。时间工具类 DateUtil
- DateUtil工具类的用法
- 积累的时间工具类
- 时间工具类[DateUtil] 2
- [Utils]时间工具类--DateUtil
- DateUtil时间转换工具类
- 时间工具类,DateUtil 你想要的时间问题都不是问题
- DateUtil 有关时间、日期的公用类
- DateUtil工具类解析(SimpleDateFormat的使用)
- HDU 5901 1-n中素数个数
- 一个偶数总能表示为两个素数之和(do)
- iOS形变之CATransform3D
- 我的Spark学习之路(二)geotrellis初探:如何发起WEB服务显示地图
- SQL盲注的框架——BBQSQL
- 积累的操作时间的工具类dateutil
- [精]Oracle VPD详解(虚拟专用数据库)
- perl中的字符编码
- 【Android】操作Camera对象采集图像
- ORACLE建表空间及用户
- 安卓面试:activity
- unity5.0新功能-布料、动画系统
- NodeJs 畅谈异步
- E:安桌层及文件系统层的PRINTf输出原理