java中指定日期 -- 当前时间之内随机生成日期

    //随机生成2023-1-1 0:0:0 到 现在的随机日期10个?
    public void randomDay() throws ParseException {
        //格式化Date日期
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = simpleDateFormat.parse("2023-1-1 0:0:0");   //指定日期
        long time = date.getTime();     //指定日期的时间戳
        long timeMillis = System.currentTimeMillis();   //系统当前时间的时间戳

        for (int i = 0; i < 10; i++) {
            long random = (long) (Math.random()*(timeMillis-time) + time);
            System.out.println(simpleDateFormat.format(random));
        }
    }

/**
    输出结果
2023-02-08 10:09:12
2023-01-13 08:42:30
2023-02-08 09:28:25
2023-01-18 18:46:16
2023-01-30 08:55:43
2023-01-25 23:52:48
2023-01-13 18:32:11
2023-01-23 19:59:23
2023-02-12 06:50:11
2023-01-26 22:24:43

*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
MyBatis不提供直接根据当前时间随机生成id的功能,但可以通过自定义TypeHandler来实现。 TypeHandler是MyBatis用于处理Java类型与JDBC类型之间转换的接口。我们可以通过实现该接口,来自定义Java类型与JDBC类型的转换逻辑。 下面是一个根据当前时间随机生成id的TypeHandler示例: ```java public class RandomIdTypeHandler implements TypeHandler<Long> { @Override public void setParameter(PreparedStatement ps, int i, Long parameter, JdbcType jdbcType) throws SQLException { if (parameter == null) { ps.setNull(i, jdbcType.TYPE_CODE); } else { ps.setLong(i, parameter); } } @Override public Long getResult(ResultSet rs, String columnName) throws SQLException { return rs.getLong(columnName); } @Override public Long getResult(ResultSet rs, int columnIndex) throws SQLException { return rs.getLong(columnIndex); } @Override public Long getResult(CallableStatement cs, int columnIndex) throws SQLException { return cs.getLong(columnIndex); } @Override public Long getNullableResult(ResultSet rs, String columnName) throws SQLException { Long id = rs.getLong(columnName); if (rs.wasNull()) { id = generateRandomId(); } return id; } @Override public Long getNullableResult(ResultSet rs, int columnIndex) throws SQLException { Long id = rs.getLong(columnIndex); if (rs.wasNull()) { id = generateRandomId(); } return id; } @Override public Long getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { Long id = cs.getLong(columnIndex); if (cs.wasNull()) { id = generateRandomId(); } return id; } private Long generateRandomId() { // 根据当前时间生成随机id return System.currentTimeMillis() + new Random().nextInt(1000); } } ``` 使用该TypeHandler时,我们需要在MyBatis的配置文件将该TypeHandler注册到对应的Java类型: ```xml <typeHandlers> <typeHandler handler="com.example.RandomIdTypeHandler" javaType="java.lang.Long"/> </typeHandlers> ``` 然后在对应的Mapper.xml文件,将该TypeHandler应用到需要随机生成id的字段上: ```xml <insert id="insertUser" parameterType="com.example.User"> <selectKey keyProperty="id" resultType="java.lang.Long" order="BEFORE"> <if test="id == null"> select nextval('seq_user_id') as id </if> </selectKey> insert into user (id, name, age) values (#{id, typeHandler=com.example.RandomIdTypeHandler}, #{name}, #{age}) </insert> ``` 以上示例,如果传入的User对象的id为null,则会执行selectKey语句来获取下一个id,否则会使用RandomIdTypeHandler生成随机id。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

@沉住气

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

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

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

打赏作者

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

抵扣说明:

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

余额充值