Java程序向MySQL添加时间格式的数据

java向MySQL插入当前时间的四种方式

第一种:将java.util.Date类型的时间转成mysql数据库识别的java.sql.Date类型时间

注:java.util.Date是java.sql.Date的父类

Date time= new java.sql.Date(newjava.util.Date().getTime());

pstat.setDate(8,new Date(userinfo.getStarttime().getTime()));

第二种: java用PreparedStatement来setDate,用问号的形式给日期问号赋值

pstmt.setTimestamp(8, newTimestamp(System.currentTimeMillis()));

第三:使用Timestamp的valueOf方法

Mysql 与 java 的时间类型

MySql时间类型Java与之对应的时间类型
datejava.sql.Date
Datetimejava.sql.Timestamp
Timestampjava.sql.Timestamp
Timejava.sql.Time
Yearjava.sql.Date

于是便通过以下方式去实现:

Date date = new Date();//获得系统时间.

String nowTime = new SimpleDateFormat(“yyyy-MM-ddHH:mm:ss”).format(date);//将时间格式转换成符合Timestamp要求的格式.

Timestamp goodsC_date =Timestamp.valueOf(nowTime);//把时间转换

java时间日期格式化的几种方法

方法一:使用格式转换工具

importjava.sql.Timestamp;
importjava.text.ParseException;
importjava.text.SimpleDateFormat;
import java.util.Date;
 
public class TransformDate {
 
    /**
    * 直接将当前时间只按日期(时间为0)作为mysql时间戳字段的条件
    * 最终返回时间类型java.sql.Date
    */
    public voidtransformCurDate(){
       SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
       java.sql.Date timePara  = null;
       try {
           timePara = new java.sql.Date(new Date().getTime());
           System.out.println(timePara);
       } catch (Exception e) {
           e.printStackTrace();
       }
    }
    /**
    * 将java的当前时间转成指定格式(yyyy-MM-0100:00:00")作为mysql时间戳字段的条件
    *  最终返回时间类型java.sql.Date
    */
    public voidtransformCurYearMon(){
       SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
       String time = format.format(new Date()).concat("-0100:00:00");
        java.sql.Date timePara  = null;
        try {
            timePara = newjava.sql.Date(format.parse(time).getTime());
            System.out.println(timePara);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
   
    /**
    * 将java的当前时间转成Timestamp作为mysql时间戳字段的条件
    *  最终返回时间类型java.sql.Timestamp
    */
    public static void testData() {
       try {
           SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-ddhh:mm:ss");
            Timestamp date =  java.sql.Timestamp.valueOf("2012-12-1201:12:11");
           System.out.println(date);
       } catch (Exception e) {
           e.printStackTrace();
       }
    }
 
    /**
    * 处理当前时间只按日期(时间为0)
    * 最终返回时间类型java.util.Date
    */
    public static void dataTest() {
       try {
           SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
          String time =  format.format(new Date());
         Date date = format.parse(time.concat(" 00:00:00"));
         System.out.println(date);
       } catch (Exception e) {
           e.printStackTrace();
       }
    }
   
    public static void main(String[]args) {
       testData();
    }
}

方法二:使用@JsonFormat注解的方法

import java.sql.Timestamp;
import com.fasterxml.jackson.annotation.JsonFormat;

@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
private Timestamp orderTime; 

@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
public Timestamp getOrderTime() {
    return orderTime;
}

public void setOrderTime(Timestamp orderTime) {
    this.orderTime = orderTime;
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值