码迷,mamicode.com
首页 > 数据库 > 详细

数据库时间和java时间转换 datetime与date转化

时间:2019-09-08 14:26:33      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:oid   new   src   alt   转换   gettime   mda   mamicode   rri   

一个javabean如下

有四个日期Date类型 createTime, payDate, deliverDate,confirmDate;

import java.util.Date;

public class Orders {
    private int oid;
    private int bid;//箱子号
    private int uid;//用户信息,卖家
    private String orderCode;//订单号
    private int receiver;//买家
    private String userMessage;//卖家信息
    private String phone;//联系方式
    private Date createTime;//创建订单信息
    private Date payDate;//支付日期
    private Date deliverDate;//发货日期
    private Date confirmDate;//确认收货日期
    private int orderState;//订单状态
    private int pid;//产品信息
    public Orders() {
        super();
    }
    public Orders(int oid, int bid, int uid, String orderCode, int receiver, String userMessage, String phone,
            Date createTime, Date payDate, Date deliverDate, Date confirmDate, int orderState, int pid) {
        super();
        this.oid = oid;
        this.bid = bid;
        this.uid = uid;
        this.orderCode = orderCode;
        this.receiver = receiver;
        this.userMessage = userMessage;
        this.phone = phone;
        this.createTime = createTime;
        this.payDate = payDate;
        this.deliverDate = deliverDate;
        this.confirmDate = confirmDate;
        this.orderState = orderState;
        this.pid = pid;
    }
    public int getOid() {
        return oid;
    }
    public void setOid(int oid) {
        this.oid = oid;
    }
    public int getBid() {
        return bid;
    }
    public void setBid(int bid) {
        this.bid = bid;
    }
    public int getUid() {
        return uid;
    }
    public void setUid(int uid) {
        this.uid = uid;
    }
    public String getOrderCode() {
        return orderCode;
    }
    public void setOrderCode(String orderCode) {
        this.orderCode = orderCode;
    }
    public int getReceiver() {
        return receiver;
    }
    public void setReceiver(int receiver) {
        this.receiver = receiver;
    }
    public String getUserMessage() {
        return userMessage;
    }
    public void setUserMessage(String userMessage) {
        this.userMessage = userMessage;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public Date getCreateTime() {
        return createTime;
    }
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }
    public Date getPayDate() {
        return payDate;
    }
    public void setPayDate(Date payDate) {
        this.payDate = payDate;
    }
    public Date getDeliverDate() {
        return deliverDate;
    }
    public void setDeliverDate(Date deliverDate) {
        this.deliverDate = deliverDate;
    }
    public Date getConfirmDate() {
        return confirmDate;
    }
    public void setConfirmDate(Date confirmDate) {
        this.confirmDate = confirmDate;
    }
    public int getOrderState() {
        return orderState;
    }
    public void setOrderState(int orderState) {
        this.orderState = orderState;
    }
    public int getPid() {
        return pid;
    }
    public void setPid(int pid) {
        this.pid = pid;
    }
    @Override
    public String toString() {
        return "Orders [oid=" + oid + ", bid=" + bid + ", uid=" + uid + ", orderCode=" + orderCode + ", receiver="
                + receiver + ", userMessage=" + userMessage + ", phone=" + phone + ", createTime=" + createTime
                + ", payDate=" + payDate + ", deliverDate=" + deliverDate + ", confirmDate=" + confirmDate
                + ", orderState=" + orderState + ", pid=" + pid + "]";
    }
}

 

此javabean对应的数据库如下

技术图片

 

将orders一个类存入数据库,在类型转换上遇到了点问题

 

这是写的一个datetime与date转换的一个类

  public class DateUtil {

  //将date转化为datetime
public static java.sql.Timestamp dtot(java.util.Date d) { if (null == d) return null; return new java.sql.Timestamp(d.getTime()); }
  //将datetime转化为date
public static java.util.Date ttod(java.sql.Timestamp t) { if (null == t) return null; return new java.util.Date(t.getTime()); } }

 

OrdersDao的添加方法

在添加时调用,一开始调用的话,在get方法时候又会为data(加粗部分)

    public void add(Orders order) {
        String sql = "insert into Orders(oid,uid,bid,orderCode,receiver,phone,userMessage,createTime,payDate,deliverDate,cofirmDate,orderState,pid) values(?,?,?,?,?,?,?,?,?,?,?,?,?);";
        try {
            DBUtil.update(sql, order.getOid(),order.getUid(),order.getBid(),order.getOrderCode(),order.getReceiver(),order.getPhone(),order.getUserMessage(), DateUtil.dtot(order.getCreateTime()),DateUtil.dtot(order.getPayDate()),DateUtil.dtot(order.getDeliverDate()),DateUtil.dtot(order.getConfirmDate()), order.getOrderState(),order.getPid());
        }catch(Exception e) {
            e.printStackTrace();
        }
    }

 

数据库时间和java时间转换 datetime与date转化

标签:oid   new   src   alt   转换   gettime   mda   mamicode   rri   

原文地址:https://www.cnblogs.com/zoey686/p/11485517.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!