相关文章推荐
无邪的橡皮擦  ·  拍照怎么连拍-掘金·  2 年前    · 
儒雅的梨子  ·  System.Data.OracleClie ...·  2 年前    · 

我们要做的任务需求是将字符串20201013转换为20202013 00:00:00的日期类型格式

解决及测试

首先想到的是,先对前端传过来的字符串进行拼接,也就是拼上 00:00:00,再用SimpleDateFormat直接格式化为yyyy-MM-dd HH:mm:ss就可以了,如下代码

我们运行一下
在这里插入图片描述

就报错了,无法解析

为什么呐?

想想以前我们是如何格式化字符串的

前端传过来

20201013 00:00:00
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
Date parse = sdf.parse("20201015 00:00:00");
System.out.println(parse);

格式化为日期类型了对吗?注意到了什么?前端的日期格式yyyyMMdd HH:mm:ss和new SimpleDateFormat(“yyyyMMdd HH:mm:ss”)中的格式化类型是一样的对吗?

说明了什么呐?

SimpleDateFormat函数只会将你给定的日期格式类型yyyyMMdd HH:mm:ss解析为日期对象

对于你想要用yyyyMMdd直接格式化为yyyy-MM-dd,当然会报错,SimpleDateFormat类没有那么智能,只能将字符串转按给定的格式转换为日期格式

SimpleDateFormat可以将字符串转为日期,也可以将日期转为字符串,那么可不可以,先按yyyyMMdd HH:mm:ss转为yyyyMMdd HH:mm:ss格式的日期对象,然后,将这个日期对象,再按照yyyy-MM-dd HH:mm:ss转化为字符串。完美

DateFormateUtils、DateUtils的使用

有一种更好用的方法,使用DateFormateUtils、DateUtils配合进行日期格式化转换,太好用了,推荐!!代码如下

Date date = DateUtils.parseDate("20201015 00:00:00", "yyyyMMdd HH:mm:ss");
        String format1 = DateFormatUtils.format(date, "yyyy-MM-dd HH:mm:ss");
        Date date1 = DateUtils.parseDate(format1, "yyyy-MM-dd HH:mm:ss");
        System.out.println(date1);

jar报如下:

<dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.4</version>
        </dependency>
                    概述我们要做的任务需求是将字符串20201013转换为20202013 00:00:00的日期类型格式解决及测试首先想到的是,先对前端传过来的字符串进行拼接,也就是拼上 00:00:00,再用SimpleDateFormat直接格式化为yyyy-MM-dd HH:mm:ss就可以了...
function nowtime(){//将当前时间转换成yyyymmdd格式
    var mydate = new Date();
    var str = "" + mydate.getFullYear();
    var mm = mydate.getMonth()+1
    if(mydate.getMonth()>9){
     str += mm;
    else{
     str += "0" + mm;
    if(mydate.getDate()>9){
     str += mydate.getDate();
				
"yyyyMM","yyyyMMdd","yyyyMMdd HH:mm:ss", "yyyy-MM","yyyy-MM-dd","yyyy-MM-dd HH:mm:ss" "yyyy.MM","yyyy.MM.dd","yyyy.MM.dd HH:mm:ss" "yyyy/MM","yyyy/MM/dd","yyyy/MM/dd HH:mm:ss" "yyyy_MM","yyyy_MM_dd","yyyy_MM_dd HH:mm:ss"
%Y%m%d%H%i%s 年月日时分秒 %Y%m%d 对应 YYYYMMDD select now() as nowTime, DATE_FORMAT(now(),'%Y%m%d') as time 不同格式对应不同的值 select now() as nowTime, DATE_FORMAT(now(),'%Y%m%d%H%
一些常用的格式,比如yyyy-MM-dd HH:mm:ss这种,可以很轻松的格式化。我居然费大半天的时间去挨个读取、拼接…… from:http://blog.csdn.net/hahapigzhu/archive/2009/09/05/4522573.aspx DateFormat: 11.5.3 DateFormat类 在java.util.Date类中实...
* @param 日期验证,验证的格式有: * "yyyyMM","yyyyMMdd","yyyyMMdd HH:mm:ss", * "yyyy-MM","yyyy-MM-dd","yyyy-MM-dd HH:mm:ss" * "yyyy.MM","yyyy.MM.dd","yyyy.MM.dd HH:mm:ss" * "yyyy/MM","yyyy/MM/dd","yyyy/MM/dd HH:mm:ss" * "yyyy_MM","yyyy_MM_dd","yyyy_MM_dd HH:mm:ss" * @param sDate * @return false/true
记一次SimpleDateFormat 格式化日期,解析出来的日期错误! 今天将2021-12-16 00:00:00格式日期转为20211216格式的,通过使用SimpleDateFormat进行格式化,解析后的结果竟然是20201102。 解决办法: 分两次解析,第一次解析格式yyyy-MM-dd格式的 发现Date 时间戳已经正确了,再创建一个SimpleDateFormat 对象,调用format方法即可得到想要的格式字符串。 1. 使用substr函数将原始日期字符串中的年、月、日分别提取出来,例如: select substr('20220101', 1, 4) as year, substr('20220101', 5, 2) as month, substr('20220101', 7, 2) as day; 这样就可以得到年、月、日分别为2022、01、01的结果。 2. 使用concat函数将年、月、日拼接成新的日期字符串,例如: select concat(substr('20220101', 1, 4), '-', substr('20220101', 5, 2), '-', substr('20220101', 7, 2)) as new_date; 这样就可以得到新的日期字符串为2022-01-01的结果。 综合起来,可以使用如下的Hive SQL语句将yyyymmdd转成yyyy-mm-dd: select concat(substr(date_str, 1, 4), '-', substr(date_str, 5, 2), '-', substr(date_str, 7, 2)) as new_date from table_name; 其中,date_str为原始日期字符串,table_name为包含原始日期字符串的表名。