泛微ECOLOGY9-如何添加转换规则,以日期时间格式转换为时间戳为例

实现效果

当前日期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应用数据映射配置中使用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

图图闹海

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值