把 Jul 8, 2020 12:00:00 AM 格式的时间转换为 2020-07-08 12:00:00 格式的时间

 

 /**
     * 把 Jul 8, 2020 12:00:00 AM 格式的时间转换为 2020-07-08 12:00:00 格式的时间
     *
     * @param date Jul 8, 2020 12:00:00 AM 格式的时间
     * @param type true 返回年月日格式  false 返回年月日时分秒
     */
    public static String parse_date(String date, boolean type) {
        SimpleDateFormat sdf = new SimpleDateFormat("MMM d, yyyy K:m:s a", Locale.ENGLISH);
        Date d2 = null;
        try {
            //把Jul 8, 2020 12:00:00 AM格式转换为常规的Date格式
            d2 = sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        SimpleDateFormat simpleDateFormat;
        if (type) {
            simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        } else {
            simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
        //再把Date的时间转换为需要的格式
        String format = simpleDateFormat.format(d2);
        return format;
    }

    /**
     * 把 2020-07-08 12:00:00 格式的时间转换为 Jul 8, 2020 12:00:00 AM 格式的时间
     *
     * @param date 年月日时分秒 格式的时间
     */
    public static String format_date(String date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d2 = null;
        try {
            //把 2020-07-08 12:00:00 格式的时间转换为 常规Date时间
            d2 = simpleDateFormat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        SimpleDateFormat sdf = new SimpleDateFormat("MMM d, yyyy K:m:s a", Locale.ENGLISH);
        //再把 常规Date时间转换回 Jul 8, 2020 12:00:00 AM 格式的时间
        String format = sdf.format(d2);
        return format;
    }

 

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,可以使用Java的SimpleDateFormat类将日期字符串转换为指定格式的日期对象。在引用\[2\]和引用\[3\]中提供了两种不同的方法来实现日期转换。以下是一个示例代码,演示了如何将日期字符串"Mon Feb 13 08:00:00 GMT+08:00 2012"转换为指定格式的日期对象: ```java import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; public class DateFormat { public static void main(String\[\] args) throws ParseException { String dateString = "Mon Feb 13 08:00:00 GMT+08:00 2012"; SimpleDateFormat inputFormat = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy", Locale.ENGLISH); Date date = inputFormat.parse(dateString); SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String result = outputFormat.format(date); System.out.println(result); } } ``` 运行以上代码,输出结果为:"2012-02-13 08:00:00"。这样就完成了将日期字符串转换为指定格式的日期对象的操作。 #### 引用[.reference_title] - *1* *2* *3* [GMT时间格式转换(Mon Feb 13 08:00:00 GMT+08:00 2012)](https://blog.csdn.net/yaohucaizi/article/details/8845329)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值