diff --git a/src/main/java/com/nis/domain/restful/UpdateStatConfig.java b/src/main/java/com/nis/domain/restful/UpdateStatConfig.java deleted file mode 100644 index 872bb7e..0000000 --- a/src/main/java/com/nis/domain/restful/UpdateStatConfig.java +++ /dev/null @@ -1,64 +0,0 @@ -/** - * - */ -package com.nis.domain.restful; - -import java.io.Serializable; - -import com.wordnik.swagger.annotations.ApiModelProperty; - -/** - * @ClassName:CancleConfigSource - * @Description:TODO(这里用一句话描述这个类的作用) - * @author (zdx) - * @date 2018年6月1日 下午6:08:09 - * @version V1.0 - */ -public class UpdateStatConfig implements Serializable{ - - private static final long serialVersionUID = 6346336602908201877L; - @ApiModelProperty(value = "配置ID", required = true) - private Long cfgId; - @ApiModelProperty(value = "业务ID", required = true) - private Integer service; - @ApiModelProperty(value = "是否有效", required = true) - private Integer isValid; - - /** - * - */ - public UpdateStatConfig() { - super(); - // TODO Auto-generated constructor stub - } - /** - * @param cfgId - * @param service - * @param isValid - */ - public UpdateStatConfig(Long cfgId, Integer service, Integer isValid) { - super(); - this.cfgId = cfgId; - this.service = service; - this.isValid = isValid; - } - public Long getCfgId() { - return cfgId; - } - public void setCfgId(Long cfgId) { - this.cfgId = cfgId; - } - public Integer getService() { - return service; - } - public void setService(Integer service) { - this.service = service; - } - public Integer getIsValid() { - return isValid; - } - public void setIsValid(Integer isValid) { - this.isValid = isValid; - } - -} diff --git a/src/main/java/com/nis/domain/restful/UpdateStatConfigSource.java b/src/main/java/com/nis/domain/restful/UpdateStatConfigSource.java deleted file mode 100644 index c60fe72..0000000 --- a/src/main/java/com/nis/domain/restful/UpdateStatConfigSource.java +++ /dev/null @@ -1,47 +0,0 @@ -/** - * - */ -package com.nis.domain.restful; - -import java.io.Serializable; -import java.util.List; - -/** - * @ClassName:CancleConfigSource - * @Description:TODO(这里用一句话描述这个类的作用) - * @author (zdx) - * @date 2018年6月1日 下午6:08:09 - * @version V1.0 - */ -public class UpdateStatConfigSource implements Serializable{ - - /** - * - */ - private static final long serialVersionUID = 277603807201929340L; - private List UpdateStatCfgList; - - /** - * - */ - public UpdateStatConfigSource() { - super(); - // TODO Auto-generated constructor stub - } - - /** - * @param cancleConfigList - */ - public UpdateStatConfigSource(List UpdateStatCfgList) { - super(); - this.UpdateStatCfgList = UpdateStatCfgList; - } - - public List getUpdateStatCfgList() { - return UpdateStatCfgList; - } - - public void setUpdateStatCfgList(List updateStatCfgList) { - UpdateStatCfgList = updateStatCfgList; - } -} diff --git a/src/main/java/com/nis/util/CompileVal.java b/src/main/java/com/nis/util/CompileVal.java index 35a21a8..69bf0fb 100644 --- a/src/main/java/com/nis/util/CompileVal.java +++ b/src/main/java/com/nis/util/CompileVal.java @@ -681,9 +681,11 @@ public class CompileVal { public static String serviceConfigCompileVal(ConfigCompile configCompile) { Long compileId = configCompile.getCompileId(); - if (configCompile.getAction() != 1 && configCompile.getAction() != 2 && configCompile.getAction() != 5 - && configCompile.getAction() != 6 && configCompile.getAction() != 7) { - return "编译配置id为" + compileId + "的配置中action的值只能是1(阻断),2(监测),5(封堵白名单),6(监测白名单),7(封堵监测都白名单)"; + Integer action = ServiceAndRDBIndexReal.getActionByService(configCompile.getService()); + if (StringUtil.isEmpty(action)){ + return "编译配置id为" + compileId + "的配置中service的值为"+configCompile.getService()+"时action不存在"; + }else if(action.compareTo(configCompile.getAction())!=0) { + return "编译配置id为" + compileId + "的配置中service的值为"+configCompile.getService()+"时action只能为"+action; } if (configCompile.getDoBlacklist() != 1) { return "编译配置id为" + compileId + "的配置中doBlacklist的值只能是1"; diff --git a/src/main/java/com/nis/util/ServiceAndRDBIndexReal.java b/src/main/java/com/nis/util/ServiceAndRDBIndexReal.java index e029708..570e16a 100644 --- a/src/main/java/com/nis/util/ServiceAndRDBIndexReal.java +++ b/src/main/java/com/nis/util/ServiceAndRDBIndexReal.java @@ -24,13 +24,18 @@ public class ServiceAndRDBIndexReal { * key是业务类型,value是业务类型对应的redisdbIndex */ private static Map> serviceDBIndexmap = new HashMap>(); - + /** + * key是业务类型,value是业务类型对应的动作(action) + */ + private static Map serviceActionMap = new HashMap(); static { String service = Configurations.getStringProperty("service", ""); if (service != null && !service.trim().equals("")) { String[] split = service.split(";"); for (String str : split) { - String type = Configurations.getStringProperty(str, ""); + String[] serviceAction = str.split(":"); + serviceActionMap.put(Integer.valueOf(serviceAction[0]), Integer.valueOf(serviceAction[1])); + String type = Configurations.getStringProperty(serviceAction[0], ""); if (type != null && !type.trim().equals("")) { Map typeMap = new HashMap(); String[] typeArrs = type.split(";"); @@ -41,7 +46,7 @@ public class ServiceAndRDBIndexReal { typeMap.put(Integer.parseInt(typeArr[0]), tableName.toUpperCase()); } } - sercieNameMap.put(Integer.parseInt(str), typeMap); + sercieNameMap.put(Integer.parseInt(serviceAction[0]), typeMap); } } @@ -88,7 +93,10 @@ public class ServiceAndRDBIndexReal { } } - + public static Integer getActionByService(Integer service) { + return serviceActionMap.get(service); + } + public static Map> getSercieNameMap() { return sercieNameMap; } diff --git a/src/main/java/com/nis/web/controller/restful/ConfigSourcesController.java b/src/main/java/com/nis/web/controller/restful/ConfigSourcesController.java index 818c063..a22e0f6 100644 --- a/src/main/java/com/nis/web/controller/restful/ConfigSourcesController.java +++ b/src/main/java/com/nis/web/controller/restful/ConfigSourcesController.java @@ -27,7 +27,6 @@ import com.nis.domain.restful.ConfigCompile; import com.nis.domain.restful.ConfigSource; import com.nis.domain.restful.FileDesc; import com.nis.domain.restful.MaatConfig; -import com.nis.domain.restful.UpdateStatConfigSource; import com.nis.restful.RestBusinessCode; import com.nis.restful.RestServiceException; import com.nis.util.Constants; @@ -503,19 +502,17 @@ public class ConfigSourcesController extends BaseRestController { @RequestMapping(value = "/cfg/v1/commonSources", method = RequestMethod.PUT) @ApiOperation(value = "回调配置状态修改", httpMethod = "PUT", response = Map.class, notes = "回调配置状态修改服务") @ApiParam(value = "回调配置状态修改", name = "updateStatConfigSource", required = true) - public Map updateCommonConfigSource(@RequestBody UpdateStatConfigSource updateStatConfigSource, - HttpServletRequest request, HttpServletResponse response) { + public Map updateCommonConfigSource(@RequestBody String jsonString, HttpServletRequest request, + HttpServletResponse response) { ConfigSourcesService.setMsgList(new ArrayList());// 清除上次记录的日志信息 long start = System.currentTimeMillis(); - SaveRequestLogThread thread = super.saveRequestLog(servicesRequestLogService, Constants.OPACTION_PUT, request, - updateStatConfigSource); + SaveRequestLogThread thread = super.saveRequestLog(servicesRequestLogService, Constants.OPACTION_PUT, request,jsonString); StringBuffer sb = new StringBuffer(); + + configSourcesService.updateCommonSources(thread, start,jsonString, new Date(),sb); - configSourcesService.updateCommonSources(thread, start, updateStatConfigSource.getUpdateStatCfgList(), - new Date(), sb); - - return compileServiceResponse(thread, System.currentTimeMillis() - start, request, response, "配置状态修改成功", - Constants.IS_DEBUG ? updateStatConfigSource : null); + return compileServiceResponse(thread, System.currentTimeMillis() - start, request, response, + "配置状态修改成功" , Constants.IS_DEBUG ? jsonString : null); } @RequestMapping(value = "/cfg/v1/fileUploadSources", method = RequestMethod.POST) @@ -564,8 +561,8 @@ public class ConfigSourcesController extends BaseRestController { e.printStackTrace(); } JSONObject jsonObj = new JSONObject(); - jsonObj.put("accessUrl", filePath.substring(filePath.indexOf("group"))); - // jsonObj.put("accessUrl", "filePath"); +// jsonObj.put("accessUrl", filePath.substring(filePath.indexOf("group"))); + jsonObj.put("accessUrl", "filePath"); return compileServiceResponse(thread, System.currentTimeMillis() - start, request, response, "文件上传成功", jsonObj); } @@ -606,7 +603,8 @@ public class ConfigSourcesController extends BaseRestController { // meta_list[4] = new NameValuePair("fileMd5", md5); logger.info("-----------------调用接口上传文件---------------"); String filePath = FileManager.upload(fdsfile, null); - resultObject.put("path", filePath.substring(filePath.indexOf("group"))); +// resultObject.put("path", filePath.substring(filePath.indexOf("group"))); + resultObject.put("path", filePath); } CommonsMultipartFile filetemp = (CommonsMultipartFile) file; diff --git a/src/main/java/com/nis/web/service/restful/ConfigSourcesService.java b/src/main/java/com/nis/web/service/restful/ConfigSourcesService.java index f3d95d2..dbd6db4 100644 --- a/src/main/java/com/nis/web/service/restful/ConfigSourcesService.java +++ b/src/main/java/com/nis/web/service/restful/ConfigSourcesService.java @@ -18,6 +18,8 @@ import java.util.concurrent.CountDownLatch; import java.util.regex.Matcher; import java.util.regex.Pattern; +import net.sf.json.JSONObject; + import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; @@ -38,7 +40,6 @@ import com.nis.domain.restful.IpRegion; import com.nis.domain.restful.MaatConfig; import com.nis.domain.restful.NumRegion; import com.nis.domain.restful.StrRegion; -import com.nis.domain.restful.UpdateStatConfig; import com.nis.restful.CompileJudgeCode; import com.nis.restful.RestBusinessCode; import com.nis.restful.RestServiceException; @@ -61,8 +62,6 @@ import com.nis.web.service.BaseService; import com.nis.web.service.SaveRequestLogThread; import com.nis.web.service.SpringContextHolder; -import net.sf.json.JSONObject; - /** * @ClassName: ControlService * @Description: TODO(编译配置持久化) @@ -2566,31 +2565,40 @@ public class ConfigSourcesService extends BaseService { return "ok"; } - public String updateCommonSources(SaveRequestLogThread thread, long start, List updateStatCfgList, + public String updateCommonSources(SaveRequestLogThread thread, long start,String jsonString, Date opTime, StringBuffer sb) { - Map> cfgMap = new HashMap>(); - if (null != updateStatCfgList && updateStatCfgList.size() > 0) { - for (UpdateStatConfig config : updateStatCfgList) { - String msg = checkOptForUpdate(config); - if (!msg.equals("ok")) { - thread.setExceptionInfo(msg + sb.toString()); - throw new RestServiceException(thread, System.currentTimeMillis() - start, msg + sb.toString(), - RestBusinessCode.config_integrity_error.getValue()); - } - if (cfgMap.containsKey(config.getService())) { - cfgMap.get(config.getService()).add(config.getCfgId()); - } else { - List idList = new ArrayList(); - idList.add(config.getCfgId()); - cfgMap.put(config.getService(), idList); - } - } - - } else { - thread.setExceptionInfo("配置列表不能为空" + sb.toString()); - throw new RestServiceException(thread, System.currentTimeMillis() - start, "配置列表不能为空" + sb.toString(), - RestBusinessCode.config_integrity_error.getValue()); + JsonArray jsonObjectList = null; + try { + jsonObjectList = new JsonParser().parse(jsonString).getAsJsonArray(); + } catch (Exception e) { + // TODO: handle exception + logger1.error(e.getMessage()); + thread.setExceptionInfo(e.getMessage() + sb.toString()); + throw new RestServiceException(thread, System.currentTimeMillis() - start, e.getMessage() + sb.toString(), + RestBusinessCode.config_integrity_error.getValue()); } + // + Map> cfgMap = new HashMap>(); + for (int i = 0; i < jsonObjectList.size(); i++) { + JsonObject jsonObj=(JsonObject) jsonObjectList.get(i); + Map srcMap = JSONObject.fromObject(JSONObject.fromObject((jsonObj.toString()))); + String msg = checkOptForUpdate(srcMap); + if (!msg.equals("ok")) { + thread.setExceptionInfo(msg + sb.toString()); + throw new RestServiceException(thread, System.currentTimeMillis() - start, msg + sb.toString(), + RestBusinessCode.config_integrity_error.getValue()); + } + Integer service = Integer.valueOf(srcMap.get("service").toString()); + Long cfgId = Long.valueOf(srcMap.get("cfgId").toString()); + if (cfgMap.containsKey(service)) { + cfgMap.get(service).add(cfgId); + } else { + List idList = new ArrayList(); + idList.add(cfgId); + cfgMap.put(service, idList); + } + } + Map>> restMap = new HashMap>>(); Iterator serviceIterator = cfgMap.keySet().iterator(); while (serviceIterator.hasNext()) { @@ -2610,22 +2618,31 @@ public class ConfigSourcesService extends BaseService { return "ok"; } - private String checkOptForUpdate(UpdateStatConfig config) { - if (StringUtil.isEmpty(config.getCfgId())) { + private String checkOptForUpdate(Map srcMap) { + String cfgId = srcMap.get("cfgId").toString(); + String service = srcMap.get("service").toString(); + String isValid = srcMap.get("isValid").toString(); + if (StringUtil.isEmpty(cfgId)) { return "cfgId字段不能为空"; + }else if(!StringUtil.isNumeric(cfgId)){ + return "cfgId字段格式不正确"; } - if (StringUtil.isEmpty(config.getService())) { + if (StringUtil.isEmpty(service)) { return "service字段不能为空"; + }else if(!StringUtil.isNumeric(service)){ + return "service字段格式不正确"; } - if (StringUtil.isEmpty(config.getIsValid())) { - return "配置id为" + config.getCfgId()+"的IsValid字段不能为空"; + if (StringUtil.isEmpty(isValid)) { + return "配置id为" + srcMap.get("cfgId")+"的isValid字段不能为空"; + }else { + if(!StringUtil.isNumeric(service)){ + return "isValid字段格式不正确"; + }else if(!isValid.equals("0")){ + return "isValid字段取值只能是0(失效)"; + } } - if (config.getIsValid() != 0) { - return "配置id为" + config.getCfgId() + "的配置在修改时不能为有效"; - } - return "ok"; } private boolean isIp(String ipStr){ diff --git a/src/main/resources/maatXml/maat.xml b/src/main/resources/maatXml/maat.xml index 79046d9..8ad42b1 100644 --- a/src/main/resources/maatXml/maat.xml +++ b/src/main/resources/maatXml/maat.xml @@ -45,7 +45,7 @@ - + EFFECTIVE_RULE;:;{compile_table_name};,;[compile_id] [compile_id];\t;[service];\t;[action];\t;[do_blacklist];\t;[do_log];\t;[effective_range];\t;[user_region];\t;[is_valid];\t;[group_num];\t;[father_cfg_id];\t;[op_time] diff --git a/src/main/resources/serviceTable.properties b/src/main/resources/serviceTable.properties index 66cd44f..3d87f7e 100644 --- a/src/main/resources/serviceTable.properties +++ b/src/main/resources/serviceTable.properties @@ -5,11 +5,12 @@ ####3:例如:80=10:MM_COMPILE;11:MM_GROUP;12:AV_CONT_IP_PORT ####4:type中10代表是编译配置,11代表是分组配置,12代表是ip类域配置,13代表是数值类配置,14代表是字符串类域配置,15代表是增强字符串类域配置,16代表是文件摘要类域配置,17代表是文本相似性域配置 ################################################################################# -service=3;1;80;81;82;83;84;85;144;145;146;147;148;149 +service=1:128;2:128;3:32;4:96;16:48;17:16;18:16;19:16;20:16;21:16;22:16;23:16;24:16;25:32;26:16;27:16;28:32;29:32;30:16;31:16;128:1;129:1;130:1;131:1;132:1;133:1;134:1;135:1;136:1;137:1;138:1;139:1;140:1;141:1;256:16;257:16;258:16;259:16;260:16;261:16;262:16;263:16;264:16;265:16;266:16;384:1;385:1;386:1;387:1;388:1;389:1;390:1;391:1;392:1;393:1;394:1;512:1;513:1;528:48;529:48;544:2;545:2;560:80;561:80;1024:16 -3=10:NTC_COMPILE;11:NTC_GROUP;12:NTC_UNIVERSAL_IP + +17=10:NTC_COMPILE;11:NTC_GROUP;12:NTC_UNIVERSAL_IP 1=10:WHITE_LIST_COMPILE;11:WHITE_LIST_GROUP;12:WHITE_LIST_IP 80=10:MM_COMPILE;11:MM_GROUP;12:MM_AV_IP 81=10:MM_COMPILE;11:MM_GROUP;14:MM_AV_URL diff --git a/src/test/java/com/nis/test/RestTest.java b/src/test/java/com/nis/test/RestTest.java index 04bdcf0..4dbc209 100644 --- a/src/test/java/com/nis/test/RestTest.java +++ b/src/test/java/com/nis/test/RestTest.java @@ -81,136 +81,153 @@ public class RestTest { } //@Test - public void testGetPzId() { - CloseableHttpResponse response = null; - try { + public void testGetPzId() { + CloseableHttpResponse response = null; + try { - HttpGet request = new HttpGet( - "http://10.0.6.104:8080/galaxy/service/cfg/v1/configPzIdSources?sourceName=CONFIGCOMPILE"); - //galaxy/service/cfg/v1/fileUploadSources - JSONObject jsonObject = new JSONObject(); - jsonObject.put("sourceName", "CONFIGCOMPILE"); - jsonObject.put("createTime", "2018-05-21 21:00:00"); - jsonObject.put("key", "123456789"); - jsonObject.put("fileName", "1.png"); - jsonObject.put("checksum", "d99f9aa4f3748fa48aa6e350c628c67f"); + HttpGet request = new HttpGet( + "http://10.0.6.104:8080/galaxy/service/cfg/v1/configPzIdSources?sourceName=CONFIGCOMPILE"); + //galaxy/service/cfg/v1/fileUploadSources + JSONObject jsonObject = new JSONObject(); + jsonObject.put("sourceName", "CONFIGCOMPILE"); + jsonObject.put("createTime", "2018-05-21 21:00:00"); + jsonObject.put("key", "123456789"); + jsonObject.put("fileName", "1.png"); + jsonObject.put("checksum", "d99f9aa4f3748fa48aa6e350c628c67f"); -// request.setHeader("File-Desc", jsonObject.toString()); - CloseableHttpClient httpClient = HttpClients.createDefault(); +// request.setHeader("File-Desc", jsonObject.toString()); + CloseableHttpClient httpClient = HttpClients.createDefault(); - response = httpClient.execute(request); - //HttpEntity httpEntity = response.getEntity(); - String result = EntityUtils.toString(response.getEntity()); - Map resultMap = (Map)JsonMapper.fromJsonString(result, Map.class); - Map dataMap = (Map)resultMap.get(RestConstants.REST_SERVICE_DATA); - List idObject = (List) dataMap.get("pzIdList"); - for (Object object : idObject) { - if(object instanceof Integer){ - System.out.println("Integer"); - }else if(object instanceof Long){ - System.out.println("Long"); + response = httpClient.execute(request); + //HttpEntity httpEntity = response.getEntity(); + String result = EntityUtils.toString(response.getEntity()); + Map resultMap = (Map)JsonMapper.fromJsonString(result, Map.class); + Map dataMap = (Map)resultMap.get(RestConstants.REST_SERVICE_DATA); + List idObject = (List) dataMap.get("pzIdList"); + for (Object object : idObject) { + if(object instanceof Integer){ + System.out.println("Integer"); + }else if(object instanceof Long){ + System.out.println("Long"); + } } + System.out.println("aa"); + } catch (ClientProtocolException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); } - System.out.println("aa"); - } catch (ClientProtocolException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + } - - } - //@Test - public void test() { - Date date = new Date(); - System.out.println(date.getTime()); - - Calendar calendar = Calendar.getInstance(); - System.out.println(calendar.getTimeInMillis()); - } - //@Test - public void testSdf() { - // TODO Auto-generated method stub - try { - SimpleDateFormat df1 = new SimpleDateFormat( - "yyyy-MM-dd HH:mm:ss"); - // Date t = new Date(); - Date t = df1.parse("2018-05-23 13:48:11"); - Long time = t.getTime(); - System.out.println(df1.format(t)); - System.out.println(time); - Long time2 = (time/1000)*1000; - System.out.println(time2+" "+df1.format(time2)); + //@Test + public void test() { + Date date = new Date(); + System.out.println(date.getTime()); - System.out.println(df1.getTimeZone().getID()+"----------------"+df1.format(t)); - - df1.setTimeZone(TimeZone.getTimeZone("UTC")); - System.out.println(df1.format(t)); - - - System.out.println("-------------------------------------------------------------------"); - - SimpleDateFormat df2 = new SimpleDateFormat( - "yyyy-MM-dd'T'HH:mm:ss.SSS"); - Date t2 = new Date(); Calendar calendar = Calendar.getInstance(); - calendar.setTimeInMillis(time); - System.out.println(df2.getTimeZone().getID()+"----------------"+df2.format(calendar.getTime())); - - - df2.setTimeZone(TimeZone.getTimeZone("UTC")); - System.out.println(df2.getTimeZone().getID()+"----------------"+df2.format(calendar.getTime())); - df2.setTimeZone(TimeZone.getTimeZone("UTC-8")); - - System.out.println(df2.getTimeZone().getID()+"----------------"+df2.format(calendar.getTime())); - df2.setTimeZone(TimeZone.getTimeZone("GMT+8")); - - System.out.println(df2.getTimeZone().getID()+"----------------"+df2.format(calendar.getTime())); - df2.setTimeZone(TimeZone.getTimeZone("GMT")); - System.out.println(df2.getTimeZone().getID()+"----------------"+df2.format(calendar.getTime())); - - - System.out.println("-----------------------------------------"); - - df2.setTimeZone(TimeZone.getTimeZone("UTC")); - Date date = df2.parse("2018-05-23T13:48:11.077"); - - System.out.println("2018-05-23 13:48:11"+"def:"+time+" "+date.getTime()+" 差:"+(time-date.getTime())/(60*60*1000)); - - - - - - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); + System.out.println(calendar.getTimeInMillis()); + } + //@Test + public void testSdf() { + // TODO Auto-generated method stub + try { + SimpleDateFormat df1 = new SimpleDateFormat( + "yyyy-MM-dd HH:mm:ss"); + // Date t = new Date(); + Date t = df1.parse("2018-05-23 13:48:11"); + Long time = t.getTime(); + System.out.println(df1.format(t)); + System.out.println(time); + Long time2 = (time/1000)*1000; + System.out.println(time2+" "+df1.format(time2)); + + System.out.println(df1.getTimeZone().getID()+"----------------"+df1.format(t)); + + df1.setTimeZone(TimeZone.getTimeZone("UTC")); + System.out.println(df1.format(t)); + + + System.out.println("-------------------------------------------------------------------"); + + SimpleDateFormat df2 = new SimpleDateFormat( + "yyyy-MM-dd'T'HH:mm:ss.SSS"); + Date t2 = new Date(); + Calendar calendar = Calendar.getInstance(); + calendar.setTimeInMillis(time); + System.out.println(df2.getTimeZone().getID()+"----------------"+df2.format(calendar.getTime())); + + + df2.setTimeZone(TimeZone.getTimeZone("UTC")); + System.out.println(df2.getTimeZone().getID()+"----------------"+df2.format(calendar.getTime())); + df2.setTimeZone(TimeZone.getTimeZone("UTC-8")); + + System.out.println(df2.getTimeZone().getID()+"----------------"+df2.format(calendar.getTime())); + df2.setTimeZone(TimeZone.getTimeZone("GMT+8")); + + System.out.println(df2.getTimeZone().getID()+"----------------"+df2.format(calendar.getTime())); + df2.setTimeZone(TimeZone.getTimeZone("GMT")); + System.out.println(df2.getTimeZone().getID()+"----------------"+df2.format(calendar.getTime())); + + + System.out.println("-----------------------------------------"); + + df2.setTimeZone(TimeZone.getTimeZone("UTC")); + Date date = df2.parse("2018-05-23T13:48:11.077"); + + System.out.println("2018-05-23 13:48:11"+"def:"+time+" "+date.getTime()+" 差:"+(time-date.getTime())/(60*60*1000)); + + System.out.println(utileDate2TimeStamp(t)); + System.out.println("=====--------------------------------------------------"); + + Calendar calendar2 = Calendar.getInstance(); + calendar2.setTimeZone(TimeZone.getTimeZone("UTC")); + calendar2.setTimeInMillis(1527229532956l); + System.out.println(calendar2.getTime()); + + Calendar calendar3 = Calendar.getInstance(); + calendar3.setTimeZone(TimeZone.getTimeZone("UTC+8")); + calendar3.setTimeInMillis(1527229532956l); + System.out.println(calendar3.getTime()); + + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + +// try { +// String date = "2000-10-01T10:10:10.123Z"; +// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd\'T\'HH:mm:ss.SSS\'Z\'"); +// SimpleDateFormat df2 = new SimpleDateFormat( +// "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); +// df2.setTimeZone(TimeZone.getTimeZone("GMT+8")); +// //创建被转换的对象calendar,有时区信息 +// Calendar calendar = new GregorianCalendar(); +// calendar.setTime(df2.parse(date)); +// TimeZone timeZone = TimeZone.getTimeZone("GMT+8:00"); +// calendar.setTimeZone(timeZone); + // +// User test = new User(); +// test.setId("111"); +// test.setDate(calendar.getTime()); +// ObjectMapper objectMapper = new ObjectMapper(); +// String json = objectMapper.writeValueAsString(test); +// System.out.println(json); + // +// User restore = objectMapper.readValue(json, User.class); +// //System.out.print(restore.getDate().getTimeZone().getDisplayName()); +// }catch (Exception e) { +// e.printStackTrace(System.out); +// } + } + + private java.sql.Timestamp utileDate2TimeStamp(java.util.Date udate) { + java.sql.Timestamp sqlDate = null; + long t = udate.getTime(); + sqlDate = new java.sql.Timestamp(t); + return sqlDate; + } - -// try { -// String date = "2000-10-01T10:10:10.123Z"; -// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd\'T\'HH:mm:ss.SSS\'Z\'"); -// SimpleDateFormat df2 = new SimpleDateFormat( -// "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); -// df2.setTimeZone(TimeZone.getTimeZone("GMT+8")); -// //创建被转换的对象calendar,有时区信息 -// Calendar calendar = new GregorianCalendar(); -// calendar.setTime(df2.parse(date)); -// TimeZone timeZone = TimeZone.getTimeZone("GMT+8:00"); -// calendar.setTimeZone(timeZone); -// -// User test = new User(); -// test.setId("111"); -// test.setDate(calendar.getTime()); -// ObjectMapper objectMapper = new ObjectMapper(); -// String json = objectMapper.writeValueAsString(test); -// System.out.println(json); -// -// User restore = objectMapper.readValue(json, User.class); -// //System.out.print(restore.getDate().getTimeZone().getDisplayName()); -// }catch (Exception e) { -// e.printStackTrace(System.out); -// } - } }