实现效果
当前日期00:00:00和23:59:59转换为时间戳
1. 用户/业务需求
需要将标准当前日期00:00:00和23:59:59转换为时间戳形式。
2. 需求分析
接口请求参数要求日期时间格式为时间戳,例如:2024-07-18T00:00:00.839+0800,标准日期时间格式为:2024-07-18 00:00:00和2024-07-18T23:59:59.839+0800,标准日期时间格式为:2024-07-18 23:59:59
3. 实现思路
ESB中心-应用中心-转换管理,新增数据转换规则接口,给接口使用。
4. 解决方案
ESB中心-应用中心-转换管理,新增数据转换规则,转换方法使用在线编辑;
标准当前日期00:00:00在线编辑JAVA代码:
package com.weaver.esb.package_20240417044123;
import java.util.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class class_20240417044123 {
/**
* 接口请求数据(可获取接口不同类别的数据)
* 优先级:url>request>header (request参数中可以获取到 URL 类别的参数,获取不到 header 类别的参数)
* contextParams.get("url") :URL地址参数
* contextParams.get("request") :表单参数
* contextParams.get("header") :Header参数
*
*/
private Map<String,Map<String,String>> contextParams = new HashMap<>();
/**
* 上下文数据(可获取转换规则映射位置的其它同级参数)
* allParams.get("a"):获取同级别名为a的参数值
*/
private Map<String,String> allParams = new HashMap<>();
/**
* @param: param Map collections
* 参数名称不能包含特殊字符+,.[]!"#$%&'()*:;<=>?@\^`{}|~/ 中文字符、标点 U+007F U+0000到U+001F
*/
public String execute(Map<String, String> params) {
// Get the current date and time
LocalDateTime now = LocalDateTime.now();
// Set the time to midnight (00:00:00.839+0800)
LocalDateTime modifiedDateTime = now.withHour(0).withMinute(0).withSecond(0).withNano(839000000);
// Define the desired date time format
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS+0800");
// Format the modified date time
String data = modifiedDateTime.format(formatter);
return data;
}
}
标准当前日期23:59:59在线编辑JAVA代码:
package com.weaver.esb.package_20240417044932;
import java.util.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class class_20240417044932 {
/**
* 接口请求数据(可获取接口不同类别的数据)
* 优先级:url>request>header (request参数中可以获取到 URL 类别的参数,获取不到 header 类别的参数)
* contextParams.get("url") :URL地址参数
* contextParams.get("request") :表单参数
* contextParams.get("header") :Header参数
*
*/
private Map<String,Map<String,String>> contextParams = new HashMap<>();
/**
* 上下文数据(可获取转换规则映射位置的其它同级参数)
* allParams.get("a"):获取同级别名为a的参数值
*/
private Map<String,String> allParams = new HashMap<>();
/**
* @param: param Map collections
* 参数名称不能包含特殊字符+,.[]!"#$%&'()*:;<=>?@\^`{}|~/ 中文字符、标点 U+007F U+0000到U+001F
*/
public String execute(Map<String,String> params) {
// Get the current date and time
LocalDateTime now = LocalDateTime.now();
// Set the time to midnight (00:00:00.839+0800)
LocalDateTime modifiedDateTime = now.withHour(23).withMinute(59).withSecond(59).withNano(839000000);
// Define the desired date time format
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS+0800");
// Format the modified date time
String data = modifiedDateTime.format(formatter);
return data;
}
}
数据转换规则配置:转换方法使用在线编辑即可,详见下图:
应用配置:
应用中数据映射选择转换规则,如下图所示:
总结:
新建转换规则,在线编辑JAVA代码,测试无问题后,在ESB应用数据映射配置中使用。