博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
数据库时间和java时间转换 datetime与date转化
阅读量:5047 次
发布时间:2019-06-12

本文共 4665 字,大约阅读时间需要 15 分钟。

一个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();        }    }

 

转载于:https://www.cnblogs.com/zoey686/p/11485517.html

你可能感兴趣的文章
poj1611 简单并查集
查看>>
tensorflow实现迁移学习
查看>>
Ubuntu 14.04下安装CUDA8.0
查看>>
跨平台开发 -- C# 使用 C/C++ 生成的动态链接库
查看>>
关于Redis处理高并发
查看>>
C# BS消息推送 SignalR介绍(一)
查看>>
asp.net core 系列 16 Web主机 IWebHostBuilder
查看>>
WPF星空效果
查看>>
WPF Layout 系统概述——Arrange
查看>>
PIGOSS
查看>>
几款Http小服务器
查看>>
iOS 数组排序
查看>>
第三节
查看>>
PHP结合MYSQL记录结果分页呈现(比较实用)
查看>>
Mysql支持的数据类型
查看>>
openSuse beginner
查看>>
Codeforces 620E(线段树+dfs序+状态压缩)
查看>>
Windows7中双击py文件运行程序
查看>>
Market entry case
查看>>
bzoj1230 开关灯 线段树
查看>>