当前所在位置:珠峰网资料 >> 计算机 >> 计算机等级考试 >> 正文
Java认证:JAVA中UTC的使用
发布时间:2010/8/24 10:45:13 来源:城市学习网 编辑:ziteng
  由于使用Date类型在数据库搜索大量数据时效率很低,公司采取使用Long型timecode进行搜索: Calendar calendar=Calendar.getInstance(); Long timecode=calendar.getTimeInMillis(); 但是由于不同机器不同系统的时区设置不同,所以同一时间产生的timecode的值也不同,导致搜索不到准确时间的数据的问题,所以采取如下方式进行解决: //将本地当前时间转换成UTC国际标准时间的毫秒形式 Date time=new Date(); Long timecode=time.UTC(time.getYear(), time.getMonth(), time.getDate(), time.getHours(), time.getMinutes(), time.getSeconds()); //将UTC国际标准时间的毫秒形式转换成本地的时间 Calendar calendar=Calendar.getInstance(); int offset=calendar.get(Calendar.ZONE_OFFSET); int dst=calendar.get(Calendar.DST_OFFSET); calendar.setTimeInMillis(timecode); calendar.add(Calendar.MILLISECOND, -(offset+dst)); Date resultDate=calendar.getTime(); 以上工作心得,如有不对请大家指正。
广告合作:400-664-0084 全国热线:400-664-0084
Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号
珠峰网 版权所有 All Rights Reserved