提问:布和纸怕什么?

关于Elasticsearch获取时间带TZ的问题

Elasticsearch | 作者 caobo | 发布于2019年11月29日 | 阅读数:2697

存储进Elasticsearch的所有时间,好像都会被自动转换成带T带Z的那种国际化日期格式,例:2019-11-22T07:39:30.000Z,我通过java API的方式写了个排序查询,获取到了最大时间,但是,怎么才能转换成我们常用的那种格式呢。从es查询出来的是一个map,从map里面取出来的是一个object的类型梦里面放的就是这个日期,我现在想要转换成:yyyy-M-dd HH:mm:ss
已邀请:

jiaxs - elasticsearch的忠实用户

赞同来自:

2019-11-22T07:39:30.000Z这种是UTC时间,相对于北京时间慢了八个小时

5293D1A4-DC8E-4a70-9B2C-7757B68F8479.png

 

caobo

赞同来自:

问题已经解决了,记录一下过程,第一个带T带Z的时间是我从es获取到的,要拿去数据库查询,需要转换成uct的时间格式,这个是我的解决过程
String utcTime = "2019-11-22T07:39:30.000Z";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formatPattern = "yyyy-MM-dd HH:mm:ss";
    ZonedDateTime zdt  = ZonedDateTime.parse(utcTime);
    LocalDateTime localDateTime = zdt.toLocalDateTime();
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern(formatPattern);
    String gst = formatter.format(localDateTime.plusHours(8));
    System.err.println(gst);
 
最后的输出结果是:
2019-11-22 15:39:30
 

caobo

赞同来自:

今天合并代码,告诉我服务器上的jdk是1.6的,我这个方法是基于jdk1.8的,我就呵呵了,然后又找了种新的方法,在1.6中也能用

public static void main(String[] args) {
String utcTime = "2019-07-10T16:00:00.000Z";
String UTC = "2017-11-09T23:16:03.562Z";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        System.out.println(TimeZone.getTimeZone("UTC"));
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
        Date UtcDate = null;
        try {
            UtcDate = sdf.parse(UTC);
        } catch (Exception e) {
            return;
        }
        System.out.println(UtcDate);
        SimpleDateFormat localFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(TimeZone.getDefault());
        localFormater.setTimeZone(TimeZone.getDefault());
        String localTime = localFormater.format(UtcDate.getTime());
        System.out.println(localTime);
    
    
}
 

要回复问题请先登录注册