java

DATETIME

在 SQL 中,DATETIME 是一种数据类型,用于存储日期和时间信息。它允许你以日期时间的形式表示信息,格式通常为 YYYY-MM-DD HH:MM:SS。这种格式详细到年、月、日、小时、分钟和秒

创建包含 DATETIME 类型的表
在这个表 events 中,event_time 列用来存储事件的日期和时间。

CREATE TABLE events (
    id INT AUTO_INCREMENT PRIMARY KEY,
    event_name VARCHAR(255),
    event_time DATETIME
);

插入数据到 DATETIME 字段
向 events 表插入包含日期和时间的数据:

INSERT INTO events (event_name, event_time) VALUES ('Conference', '2024-04-19 10:00:00');

查询 DATETIME 字段
从 events 表中选择所有在指定日期后发生的事件:

SELECT * FROM events WHERE event_time > '2024-04-19 00:00:00';

格式化 DATETIME
在某些 SQL 实现(如 MySQL)中,你可以使用 DATE_FORMAT 函数来格式化 DATETIME 数据:

SELECT DATE_FORMAT(event_time, '%Y-%m-%d %H:%i:%s') as formatted_date FROM events;

这将把 event_time 的显示格式转换为 YYYY-MM-DD HH:MM:SS 格式。

SimpleDateFormat

import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil {
    public static void main(String[] args) {
        // 时间戳
        long timestamp = 1618858800000L; // 假设这是你的时间戳

        // 转换成指定格式的日期时间字符串
        String formattedDate = convertTimestampToString(timestamp, "yyyy-MM-dd HH:mm:ss");

        // 打印结果
        System.out.println("格式化后的日期时间字符串:" + formattedDate);
    }

    // 将时间戳转换成指定格式的日期时间字符串
    public static String convertTimestampToString(long timestamp, String format) {
        // 创建 SimpleDateFormat 实例
        SimpleDateFormat sdf = new SimpleDateFormat(format);

        // 将时间戳转换成 Date 对象
        Date date = new Date(timestamp);

        // 使用 SimpleDateFormat 格式化 Date 对象,并返回格式化后的字符串
        return sdf.format(date);
    }
}

LocalDateTime

package com.bear.reseeding.test.File;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

public class TimeUtil {
    public static void main(String[] args) {
        // 获取当前时间
        LocalDateTime currentDateTime = LocalDateTime.now();

        // 获取一周前的时间
        LocalDateTime oneWeekAgo = getTimeBefore(currentDateTime, 1, ChronoUnit.WEEKS);
        LocalDateTime twoWeekAgo = getTimeBefore(currentDateTime, 2, ChronoUnit.WEEKS);

        // 获取N个月前的时间(假设N为3)
        LocalDateTime nMonthsAgo = getTimeBefore(currentDateTime, 3, ChronoUnit.MONTHS);

        // 获取一年前的时间
        LocalDateTime oneYearAgo = getTimeBefore(currentDateTime, 1, ChronoUnit.YEARS);

        // 定义日期时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        // 格式化时间为字符串并打印结果
        System.out.println("当前时间是:" + currentDateTime.format(formatter));
        System.out.println("一周前的时间是:" + oneWeekAgo.format(formatter));
        System.out.println("2周前的时间是:" + twoWeekAgo.format(formatter));
        System.out.println("3个月前的时间是:" + nMonthsAgo.format(formatter));
        System.out.println("一年前的时间是:" + oneYearAgo.format(formatter));
    }

    // 获取指定时间段前的时间
    public static LocalDateTime getTimeBefore(LocalDateTime dateTime, long amount, ChronoUnit unit) {
        return dateTime.minus(amount, unit);
    }
}

二级目录

三级目录

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值