java 查询solr时间格式

时间:2022-04-10 04:54:32

solr时间格式是2015-07-06T00:00:00.0Z,所以下面是把当前时间做转换

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
//Date d = format.parse(str.replace("Z", " UTC"));//注意是空格+UTC
//System.out.println(d);
Date date=new Date();
Calendar calendar=Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - 1);//获得昨天时间
System.out.println(date);
String temp=format.format(date);
String hours=format.format(calendar.getTime());
System.out.println(temp);
System.out.println(":::"+hours);
int end=temp.indexOf("+");
String re=temp.substring(0,end)+"Z";
int end2=hours.indexOf("+");
String re2=hours.substring(0,end2)+"Z";
System.out.println("当前时间"+re);
System.out.println("前一小时"+re2);
try {
System.out.println(format.parse(re.replace("Z", "UTC")));
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

solr解决时区差8小时的问题,你可以通过配置文件修改solr时间差,也可以通过程序

/**
* 获得时间减8
*/
public static Timestamp formatDateToTimestamp(Date dateTime){
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar date2 = Calendar.getInstance();
date2.setTime(dateTime);
date2.set(Calendar.HOUR, date2.get(Calendar.HOUR) - 8);
Date endDate = null;
try {
endDate = sdf.parse(sdf.format(date2.getTime()));
} catch (ParseException e) {
e.printStackTrace();
} sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
String date=sdf.format(endDate);
Timestamp stamp=Timestamp.valueOf(date);
return stamp;
}