返回java 查询......登陆

java 查询solr时间格式

阿神2016-11-07 14:41:17385

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

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时间差,也可以通过程序

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

/**

*  获得时间减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;

    }


最新手记推荐

• 用composer安装thinkphp框架的步骤• 省市区接口说明• 用thinkphp,后台新增栏目• 管理员添加编辑删除• 管理员添加编辑删除

全部回复(0)我要回复

暂无评论~
  • 取消回复发送