Date类型时间查询,前台不显示时间显示时间戳数字

1.可将Date数据类型转换为String。

2.在pom引入依赖,(Maven)

<!-- jackson-databind依赖 -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.2</version>
        </dependency>

在Date所在的实体类中找到Date的get方法,在方法上加入@JsonFormat加入注释,这样在获取的时候就能获取到指定时间格式,详细用法请搜索@JsonFormat

@JsonFormat(pattern="yyyy-MM-dd",timezone="GMT+8")
    public Date getCreateTime() {
        return createTime;
    }

 

### 雪花算法在 JavaScript 前端的实现 雪花算法是一种高效的分布式 ID 生成算法,适用于需要高性能和唯一性的场景[^1]。然而,在前端环境中,由于 JavaScript 的数字精度限制,直接使用大整数可能会导致精度丢失问题。因此,可以通过字符串形式传递 ID 或者借助第三方库(如 `decimal.js` 或 `bignumber.js`)来解决这一问题[^4]。 以下是基于 JavaScript 的雪花算法简化版实现示例: #### 示例代码 ```javascript class Snowflake { constructor(workerId, dataCenterId) { this.twepoch = 1288834974657n; // 自定义起始时间戳 (毫秒) this.workerIdBits = 5n; this.dataCenterIdBits = 5n; this.maxWorkerId = -1n ^ (-1n << this.workerIdBits); this.maxDataCenterId = -1n ^ (-1n << this.dataCenterIdBits); this.sequenceBits = 12n; this.workerIdShift = this.sequenceBits; this.dataCenterIdShift = this.sequenceBits + this.workerIdBits; this.timestampLeftShift = this.sequenceBits + this.workerIdBits + this.dataCenterIdBits; this.sequenceMask = -1n ^ (-1n << this.sequenceBits); if (workerId > this.maxWorkerId || workerId < 0) { throw new Error(`worker Id can't be greater than ${this.maxWorkerId} or less than 0`); } if (dataCenterId > this.maxDataCenterId || dataCenterId < 0) { throw new Error(`datacenter Id can't be greater than ${this.maxDataCenterId} or less than 0`); } this.workerId = BigInt(workerId); this.dataCenterId = BigInt(dataCenterId); this.sequence = 0n; this.lastTimestamp = -1n; } tilNextMillis(lastTimestamp) { let timestamp = this.timeGen(); while (timestamp <= lastTimestamp) { timestamp = this.timeGen(); } return timestamp; } timeGen() { return BigInt(Math.floor(Date.now())); } generateId() { let currentTimestamp = this.timeGen(); if (currentTimestamp < this.lastTimestamp) { throw new Error('Clock moved backwards'); } if (currentTimestamp === this.lastTimestamp) { this.sequence = (this.sequence + 1n) & this.sequenceMask; if (this.sequence === 0n) { currentTimestamp = this.tilNextMillis(this.lastTimestamp); } } else { this.sequence = 0n; } this.lastTimestamp = currentTimestamp; const id = ((currentTimestamp - this.twepoch) << this.timestampLeftShift) | (this.dataCenterId << this.dataCenterIdShift) | (this.workerId << this.workerIdShift) | this.sequence; return String(id); // 返回字符串类型的 ID 来避免精度丢失 } } // 测试代码 const snowflake = new Snowflake(1, 1); console.log(snowflake.generateId()); ``` 上述代码实现了雪花算法的核心逻辑,并返回字符串类型的 ID 以规避 JavaScript 中的大整数精度问题。 --- ### 关键点解析 1. **时间戳偏移量 (`twepoch`)** 时间戳偏移量用于减少存储需求并提高性能。通常设置为系统的启动时间或其他固定的时间点。 2. **工作节点与数据中心位分配** 工作节点 ID 和数据中心 ID 占用了部分比特位,具体数量可以根据业务需求调整。本示例中分别设置了 5 比特。 3. **序列号管理** 当同一毫秒内多次调用时,通过增加序列号确保生成的 ID 重复。 4. **精度处理** 将最终生成的 ID 转换为字符串类型,从而避免因 JavaScript 数字精度足而导致的数据错误。 --- ### Three.js 结合雪花动画的应用 如果希望进一步扩展功能,可以利用 Three.js 创建动态雪花效果。例如,结合着色器材质配置模拟真实的雪景[^5]。以下是一个简单的顶点着色器片段: ```glsl void main() { vec4 mvPosition = modelViewMatrix * vec4(position, 1.0); gl_PointSize = 2.0; float offset = sin(iTime * position.y) * 0.1; mvPosition.x += offset; mvPosition.z -= iTime * 0.1; gl_Position = projectionMatrix * mvPosition; } ``` 此代码展示了如何通过时间变量控制雪花的垂直移动和水平摆动效果。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值