前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >多种时间格式字符串转换为Date对象「建议收藏」

多种时间格式字符串转换为Date对象「建议收藏」

作者头像
全栈程序员站长
发布2022-09-27 13:41:38
5630
发布2022-09-27 13:41:38
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

maven引入包

org.apache.commons

commons-lang3

3.8

使用类

org.apache.commons.lang3.time.DateUtils

调用示例

Date date = DateUtils.parseDate(strDate, “yyyy-MM-dd”,”yyyy-MM-dd HH:mm:ss”,”yyyy/MM/dd”,”yyyy/MM/dd HH:mm:ss”);

方法说明

代码语言:javascript
复制
/**
     * <p>Parses a string representing a date by trying a variety of different parsers.</p>
     *
     * <p>The parse will try each parse pattern in turn.
     * A parse is only deemed successful if it parses the whole of the input string.
     * If no parse patterns match, a ParseException is thrown.</p>
     * The parser will be lenient toward the parsed date.
     *
     * @param str  the date to parse, not null
     * @param parsePatterns  the date format patterns to use, see SimpleDateFormat, not null
     * @return the parsed date
     * @throws IllegalArgumentException if the date string or pattern array is null
     * @throws ParseException if none of the date patterns were suitable (or there were none)
     */
    public static Date parseDate(final String str, final String... parsePatterns) throws ParseException {
        return parseDate(str, null, parsePatterns);
    }

第一个参数是时间字符串

后面的参数都是时间格式字符串,可以添加多个

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/189170.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • maven引入包
  • 使用类
  • 调用示例
  • 方法说明
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档