Java 和 MySQL 时间处理
MySQL 数据表:
CREATE TABLE `date_test` (
`id` int(10) UNSIGNED NOT NULL,
`u_date` date NOT NULL,
`u_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`unix_timestamp` int(10) UNSIGNED NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='日期时间测试表';插入数据:
INSERT INTO `date_test` (`id`, `u_date`, `u_timestamp`, `unix_timestamp`) VALUES
(1, '2024-01-10', '2024-01-10 08:53:51', 1704876980);Java 对象:
import com.mybatisflex.annotation.Id;
import com.mybatisflex.annotation.KeyType;
import lombok.Data;
import java.time.LocalDate;
import java.time.LocalDateTime;
@Data
public class DateTest {
@Id(keyType = KeyType.Auto)
private Integer id;
private LocalDate uDate;
private LocalDateTime uTimestamp;
private Long unixTimestamp;
}Mapper
import com.mybatisflex.core.BaseMapper;
public interface DateTestMapper extends BaseMapper<DateTest> {
}测试:
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
@SpringBootTest
class DateTestMapperTest {
@Resource
private DateTestMapper mapper;
@Test
void test1() {
System.out.println(mapper.selectAll());
}
}输出:
[DateTest(id=1, uDate=2024-01-10, uTimestamp=2024-01-10T16:53:51, unixTimestamp=1704876980)]
评论已关闭