Difference and conversion of java.sql.date and Java.util.date

Source: Internet
Author: User

Different points:
Java.util.Date is used in cases other than SQL statements.
Java.sql.Date is used for SQL statements, it contains only dates and no time parts
java.util.Date 是 java.sql.Date 的父类,Namely: Inheritance relation: Java.lang.Object--"java.util.Date--" java.sql.Date
Common:
The GetTime method returns the number of milliseconds that can be directly constructed

To convert each other between:

java.sql.Date转为java.util.Date

java.sql.Datedate=new java.sql.Date();java.util.Dated=new java.util.Date(date.getTime());java.util.Date转为java.sql.Datejava.util.DateutilDate=new Date();java.sql.DatesqlDate=new java.sql.Date(utilDate.getTime());java.util.Date utilDate=new Date();java.sql.DatesqlDate=new java.sql.Date(utilDate.getTime());java.sql.TimesTime=new java.sql.Time(utilDate.getTime());java.sql.Timestampstp=new java.sql.Timestamp(utilDate.getTime());这里所有时间日期都可以被SimpleDateFormat格式化format()SimpleDateFormat f=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");f.format(stp);f.format(sTime);f.format(sqlDate);f.format(utilDate)java.sql.DatesqlDate=java.sql.Date.valueOf(" 2017-9-9");utilDate=new java.util.Date(sqlDate.getTime());另类取得年月日的方法:import java.text.SimpleDateFormat;import java.util.*;java.util.Datedate= new java.util.Date(); 如果希望分开得到年,月,日SimpleDateFormatsy=new SimpleDateFormat("yyyy");SimpleDateFormat sm=new SimpleDateFormat("MM");SimpleDateFormat sd=new SimpleDateFormat("dd");String syear=sy.format(date);String smon=sm.format(date);String sday=sd.format(date);

Difference and conversion of java.sql.date and Java.util.date

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.