您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

Java编程中DateUtils日期工具类使用教程

2025/5/12 5:31:44发布41次查看
这篇文章主要介绍了java 中dateutils日期工具类的实例详解的相关资料,有时候开发java项目使用日期类型,这里介绍下日期工具类,需要的朋友可以参考下
java 中dateutils日期工具类的实例详解
介绍
在java中队日期类型的处理并不方便,通常都需要借助java.text.simpledateformat类来实现日期类型 和字符串类型之间的转换,但是在jdk1.8之后有所改善,jdk1.7以及之前的版本处理日期类型并不方便, 可以借助joda time组件来处理,尤其是日期类型的一些数学操作就更是不方便。
java代码
/** * * 日期工具类 java对日期的操作一直都很不理想,直到jdk1.8之后才有了本质的改变。 * 如果使用的是jdk1.8之前版本开发的话,推荐使用joda time组件。 * * 特别备注:unix的时间戳单位是秒 * * @author xuyi3 * @2016年7月13日 @下午2:01:00 * @dateutils * @功能说明: * @春风十里不如你 * @备注 */ public class dateutils { /** 日志对象 */ private static final logger logger = loggerfactory.getlogger(dateutils.class); /** 年-月-日 时:分:秒 显示格式 */ // 备注:如果使用大写hh标识使用24小时显示格式,如果使用小写hh就表示使用12小时制格式。 public static string date_to_string_detaial_pattern = "yyyy-mm-dd hh:mm:ss"; /** 年-月-日 显示格式 */ public static string date_to_string_short_pattern = "yyyy-mm-dd"; private static simpledateformat simpledateformat; /** * date类型转为指定格式的string类型 * * @param source * @param pattern * @return */ public static string datetostring(date source, string pattern) { simpledateformat = new simpledateformat(pattern); return simpledateformat.format(source); } /** * * unix时间戳转为指定格式的string类型 * * * system.currenttimemillis()获得的是是从1970年1月1日开始所经过的毫秒数 * unix时间戳:是从1970年1月1日(utc/gmt的午夜)开始所经过的秒数,不考虑闰秒 * * @param source * @param pattern * @return */ public static string timestamptostring(long source, string pattern) { simpledateformat = new simpledateformat(pattern); date date = new date(source * 1000); return simpledateformat.format(date); } /** * 将日期转换为时间戳(unix时间戳,单位秒) * * @param date * @return */ public static long datetotimestamp(date date) { timestamp timestamp = new timestamp(date.gettime()); return timestamp.gettime() / 1000; } /** * * 字符串转换为对应日期(可能会报错异常) * * @param source * @param pattern * @return */ public static date stringtodate(string source, string pattern) { simpledateformat = new simpledateformat(pattern); date date = null; try { date = simpledateformat.parse(source); } catch (parseexception e) { logger.error("字符串转换日期异常", e); } return date; } /** * 获得当前时间对应的指定格式 * * @param pattern * @return */ public static string currentformatdate(string pattern) { simpledateformat = new simpledateformat(pattern); return simpledateformat.format(new date()); } /** * 获得当前unix时间戳(单位秒) * * @return 当前unix时间戳 */ public static long currenttimestamp() { return system.currenttimemillis() / 1000; } //待补充 }
总结
掌握了解jdk1.8的新特性和学会使用优秀的开源组件来达到自己快速高效开发的目的。
以上就是java编程中dateutils日期工具类使用教程的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product