MySQL和Java时间毫秒之间的转换问题的总结
在做关于依时间查询用户认证的业务,因为在MySQL中,time字段使用的是bigint类型,所以存在其中都是毫秒数。当需要从数据库中取出
最近几天在做关于依时间查询用户认证的业务,因为在MySQL中,time字段使用的是bigint类型,所以存在其中都是毫秒数。当需要从数据库中取出毫秒数转换成日期格式时,需要用到MySQL数据库自己的函数,只需要在SQL语句中调用即可,但是我的业务也就是这个地方出了问题。
一般在MySQL中,我们常常使用FROM_UNIXTIME(unix_timestamp,format)函数来转换成日期格式。具体使用方法如下:
---------------------------------以下为摘录网页内容-----------------------------------------------
返回表示时间标记的一个字符串,根据format字符串格式化。format可以包含与DATE_FORMAT()函数列出的条目同样的修饰符。
根据format字符串格式化date值。
下列修饰符可以被用在format字符串中:
%M 月名字(January……December)
%W 星期名字(Sunday……Saturday)
%D 有英语前缀的月份的日期(1st, 2nd, 3rd, 等等。)
%Y 年, 数字, 4 位
%y 年, 数字, 2 位
%a 缩写的星期名字(Sun……Sat)
%d 月份中的天数, 数字(00……31)
%e 月份中的天数, 数字(0……31)
%m 月, 数字(01……12)
%c 月, 数字(1……12)
%b 缩写的月份名字(Jan……Dec)
%j 一年中的天数(001……366)
%H 小时(00……23)
%k 小时(0……23)
%h 小时(01……12)
%I 小时(01……12)
%l 小时(1……12)
%i 分钟, 数字(00……59)
%r 时间,12 小时(hh:mm:ss [AP]M)
%T 时间,24 小时(hh:mm:ss)
%S 秒(00……59)
%s 秒(00……59)
%p AM或PM
%w 一个星期中的天数(0=Sunday ……6=Saturday )
%U 星期(0……52), 这里星期天是星期的第一天
%u 星期(0……52), 这里星期一是星期的第一天
%% 一个文字“%”。
使用MYSQL语句解释时间戳语法举例:
SELECT FROM_UNIXTIME(1234567890, '%Y-%m-%d %H:%i:%S')
-------------------------------------转录结束-----------------------------------------------------
既然可以把MySQL中的毫秒转换成日期格式,我们也需要把业务逻辑中的日期格式转换成毫秒数
这是我学长写的一个函数
-------------------------------------代码如下-----------------------------------------------------
public static Long timeStrTran1970Seconds(String timeStr) throws java.text.ParseException {
Date stat = null;
stat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(timeStr);
if (stat != null)
return stat.getTime() / 1000;
else
return 0l;
}
------------------------------------代码结束------------------------------------------------------
需要注意的是,这两种转换时间的方法都是基于1970:01:01的,,这一点要特别注意。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

This article explores optimizing MySQL memory usage in Docker. It discusses monitoring techniques (Docker stats, Performance Schema, external tools) and configuration strategies. These include Docker memory limits, swapping, and cgroups, alongside

This article addresses MySQL's "unable to open shared library" error. The issue stems from MySQL's inability to locate necessary shared libraries (.so/.dll files). Solutions involve verifying library installation via the system's package m

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

This article compares installing MySQL on Linux directly versus using Podman containers, with/without phpMyAdmin. It details installation steps for each method, emphasizing Podman's advantages in isolation, portability, and reproducibility, but also

This article provides a comprehensive overview of SQLite, a self-contained, serverless relational database. It details SQLite's advantages (simplicity, portability, ease of use) and disadvantages (concurrency limitations, scalability challenges). C

This guide demonstrates installing and managing multiple MySQL versions on macOS using Homebrew. It emphasizes using Homebrew to isolate installations, preventing conflicts. The article details installation, starting/stopping services, and best pra

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]
