你的浏览器禁用了JavaScript, 请开启后刷新浏览器获得更好的体验!
发现
分享
文章
活动
登录
搜索结果正在快递途中
关于Elasticsearch获取时间带TZ的问题
Elasticsearch
| 作者
caobo
| 发布于2019年11月29日 | 阅读数:
3275
分享到:
QQ空间
新浪微博
微信
QQ好友
印象笔记
有道云笔记
存储进Elasticsearch的所有时间,好像都会被自动转换成带T带Z的那种国际化日期格式,例:2019-11-22T07:39:30.000Z,我通过java API的方式写了个排序查询,获取到了最大时间,但是,怎么才能转换成我们常用的那种格式呢。从es查询出来的是一个map,从map里面取出来的是一个object的类型梦里面放的就是这个日期,我现在想要转换成:yyyy-M-dd HH:mm:ss
没有找到相关结果
已邀请:
与内容相关的链接
提交
3 个回复
jiaxs
-
elasticsearch的忠实用户
赞同来自:
2019-11-22T07:39:30.000Z这种是UTC时间,相对于北京时间慢了八个小时
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);
}
要回复问题请先
登录
或
注册
发起人
caobo
活动推荐
Aug
15
2025 Zabbix 中国峰会
上海
·
8-15 周五
·
进行中
Aug
23
Gitee Talk | 模力方舟 AI 应用开发沙龙
广州·南洋冠盛酒店
·
8-23 周六
·
报名中
Aug
23
偷懒也是生产力!算力优化与平台自动化实战
上海徐汇区模速空间
·
8-23 周六
·
报名中
Oct
17
第27届 GOPS 全球运维大会暨研运数智化技术峰会 · 上海站
上海
·
10-17 周五
·
报名中
相关问题
elasticsearch scroll查询的原理没太懂
我来写第一个帖子-你们都是什么时候知道elasticsearch的?
怎么解决elasticsearch集群占用太多虚拟内存(VIRT)的问题?占用了几十个G,有什么可以对它进行限制的相关设置吗?
elasticsearch 设置 node.data: false 依然有数据
集群稳定性的一些问题(一定量数据后集群变得迟钝)
ElasticSearch-Hadoop的目標是什麼呢??
elasticsearch functionScoreQuery scriptFunction效率问题
如何清理Elasticsearch特定时间段数据?
Elasticsearch聚合操作的时间复杂度是O(n)吗?
请教elasticsearch出现unassigned shards根本原因
elasticsearch 中的store 以及倒排索引的问题
问题状态
最新活动:
2019-12-06 19:04
浏览:
3275
关注:
2
人
3 个回复
jiaxs - elasticsearch的忠实用户
赞同来自:
caobo
赞同来自:
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
赞同来自:
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);
}