java 查詢solr時間格式


  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; }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM