今天项目中遇到一个格式问题,收到的timestamp格式是2019-08-19-16:03:21 , 但是入es时,当类型为date的时候,这种格式直接报错,因为索引建的格式是yyyy-MM-dd HH:mm:ss,即中间多了一个横杠。

解决思路,先把时间转化成Date类型,然后再把Date转成字符串的类型。

String --> Date

/**
* @Title: 字符串转日期
* @MethodName: parse
* @Description:
* @param @param strDate
* @param @param pattern
* @param @return
* @return Date
* @throws
*/
public static Date parse(String strDate, String pattern) {
SimpleDateFormat df = new SimpleDateFormat(pattern);
try {
return df.parse(strDate);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}

Date --> String

/**
* @Title: 日期格式化
* @MethodName: format
* @Description:
* @param @param date
* @param @param pattern
* @param @return
* @return String
* @throws
*/
public static String format(Date date,String pattern){
String rs = "";
if(date!=null){
SimpleDateFormat df = new SimpleDateFormat(pattern);
rs = df.format(date);
}
return rs;
}

转化的时候指定对应的pattern即可。

java 不同时间格式转化的更多相关文章

  1. Sql server 时间格式转化

    --.SQL时间格式转化 --日期转换参数 ) --2009-03-15 15:10:02 ),'-',''),' ',''),':','') ) , ) --2009/03/15 ) , ) ) , ...

  2. Java中时间格式处理,指定N天/小时等之后的时间

    1)根据当前时间,获取具体的时刻的时间 N天前 M小时之前 可用 new Date().getTime() - 24 * 60 * 60 * 1000*N[N天之前]的方法来获取处理时间之后的具体的值 ...

  3. java校验时间格式 HH:MM

    package com; import java.text.SimpleDateFormat; import java.util.Date; /** * @author Gerrard */ publ ...

  4. 时间格式转化 String2datestyle

    时间格式转化成string工具类: package cn.javass.util; import java.text.DateFormat; import java.text.SimpleDateFo ...

  5. 29-jsp中用js进行时间格式转化

    CST可以为如下4个不同的时区的缩写: 美国中部时间:Central Standard Time (USA) UT-6:00 澳大利亚中部时间:Central Standard Time (Austr ...

  6. oracle 时间格式转化以及计算

    --A表中的日期字段 create_date   例如:2017-08-05  转化为2017年8月5日   oracle 在这里的双引号会忽略 select to_char(to_date(tt.c ...

  7. java格式化时间格式

    System.out.println("Hello World!"); SimpleDateFormat format = new SimpleDateFormat( " ...

  8. win7 蛋疼的时间格式转化

    win7系统 获得系统时间为 2015年1月1日 星期5 10:10 数据库中时间格式 是不认识的 转化为 DateTime.Now.ToString("yyyy-MM-dd HH:mm:s ...

  9. Excel中将时间格式转化成时间戳格式

    时间戳转成正常日期的公式:C1=(A1+8*3600)/86400+70*365+19其中A1表示当时的1249488000时间戳数值其中C1就是所需的日期格式,C1单元格属性改成日期格式就可以了.正 ...

随机推荐

  1. Java——判断回文

    package basic; import java.util.Scanner; public class Palindrome{ public static boolean isPalindrome ...

  2. Python3 异常

    异常 --- 程序执行时产生的事件,为Python对象 <Python学习手册>笔记 异常总是通过实例对象来识别 默认的异常处理器 --- 打印标准出错信息(包括引发的异常和堆栈跟踪-异常 ...

  3. linux定时器的实现方法

    Linux提供定时器机制,可以指定在未来的某个时刻发生某个事件,定时器的结构如下: struct timer_list { struct list_head list; unsigned long e ...

  4. C语言博客作业04—数组

    0.展示PTA总分(0----2) 展示3张关于"数组题目集"分数截图. 1.本章学习总结(2分) 1.1 学习内容总结 整理数组这章学习主要知识点,必须包含内容有: (1)数组查 ...

  5. sql语句练习50题(Mysql版) 围观

    表名和字段 –.学生表 Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别 –.课程表 Course(c_id,c_name,t_id) ...

  6. C语言-malloc

    malloc函数 编辑 锁定 同义词 malloc一般指malloc函数   malloc函数是一种分配长度为num_bytes字节的内存块的函数,可以向系统申请分配指定size个字节的内存空间.ma ...

  7. #ifndef #define #endif

    在一个大的软件工程里面,可能会有多个文件同时包含一个头文件,当这些文件编译链接成一个可执行文件时,就会出现大量重定义的错误.在头文件中实用#ifndef #define #endif能避免头文件的重定 ...

  8. c# 自定义按钮,渐变颜色(含中心向四周渐变,单方向渐变)

    废话不多言,直接代码: public class RoundButton : Button { bool clickBool = false; //1.设置圆形 //2.设置渐变色 //3.设置too ...

  9. JSON Template

    public java.lang.String toString() {#if ( $members.size() > 0 ) #set ( $i = 0 )return "{\&qu ...

  10. 数据对象如何定义为Java代码示例

    想将数据保存为这样子: [{ "subject": { "code": "B123", "words": [{ &quo ...