JAVA processing time - in the Date field in the database conversion method

2010-03-29  来源:本站原创  分类:Java  人气:651 

1, how java.util.Date into a java.sql.Date?

java.sql.Date sd;
java.util.Date ud;
/ / initialize the ud such as ud = new java.util.Date ();

sd = new java.sql.Date (ud.getTime ());

2, if you want to insert into the database and the corresponding field for the Date type then you can use PreparedStatement.setDate (int, java.sql.Date) method which can use the above method of java.sql.Date to be

Database can also be used to provide TO_DATE function, such as the current ud
TO_DATE (new SimpleDateFormat (). Format (ud, "yyyy-MM-dd HH: mm: ss"),
Note that in java format and database format of the different

A practical example

sql = "update tablename set timer = to_date ( '" + t +"',' yyyymmddhh24miss') where ....."

Where t is the variable to look like: 20051211131223

3, how to "yyyy-mm-dd" format string to convert to java.sql.Date

Method 1

SimpleDateFormat bartDateFormat =
new SimpleDateFormat ( "yyyy-MM-dd");
String dateStringToParse = "2007-7-12";
try (
java.util.Date date = bartDateFormat.parse (dateStringToParse);
java.sql.Date sqlDate = new java.sql.Date (date.getTime ());
System.out.println (sqlDate.getTime ());
catch (Exception ex) (
System.out.println (ex.getMessage ());

-------------------------------------------------- ----------
Method 2
String strDate = "2002-08-09";
StringTokenizer st = new StringTokenizer (strDate, "-");
java.sql.Date date = new java.sql.Date (Integer.parseInt (st.nextToken ()),
Integer.parseInt (st.nextToken ()),
Integer.parseInt (st.nextToken ()));

similarities and differences between java.util.Date and java.sql.Date
java.sql.Date, java.sql.Time and java.sql.Timestamp three are java.util.Date sub-class (wrapper class).

But why the value of the java.sql.Date type inserted into the database in the Date field in the data interception does occur?

java.sql.Date is to tie in with SQL DATE and set data types. "Normalized" in java.sql.Date contains only date information will be cleared when the minutes and seconds milliseconds. Format similar to: YYYY-MM-DD. When we call the ResultSet of the getDate () method to get the return value, java program will make reference to "norms" of the java.sql.Date to format the database values. Therefore, if the database exists in the non-standardized part of the information will be robbed.

In the sun in this way provides to comment on the getDate:
Retrieves the of the designated column in the current row of this <code> ResultSet </ code> object as a "java.sql.Date" object in the Java programming language.

By the same token. If we put a java.sql.Date value of the setDate method adopted by PrepareStatement into the database, java process on the incoming java.sql.Date standardized, non-standardized parts will be robbed. However, we generally java.util.Date conversion java.sql.Date over, such as: java.sql.Date sqlDate = new java.sql.Date (new java.util.Date (). GetTime ()).
Obviously, such a transformation over a java.sql.Date is often not a standardized java.sql.Date. To preserve the exact java.util.Date value,
We need to use java.sql.Timestamp.


Calendar calendar = Calendar.getInstance ();
/ / Get the current time, time variable declaration
int year = calendar.get (Calendar.YEAR);
/ / Get year
int month = calendar.get (Calendar.MONTH);
/ / Get month, but would like to add a month
month = month +1;
int date = calendar.get (Calendar.DATE);
/ / Get dates
String today = "" + year +"-"+ month +"-"+ date + "";

We are actually used in the project is TimeStamp.toLocaleString () / / converted into 2007-11-19 15:30:00.0 such a format,
When writing sqlproc must add 'TimeStamp.toLocaleString ()'

This article comes from CSDN blog, reproduced please indicate source:

  • JAVA processing time - in the Date field in the database conversion method 2010-03-29

    1, how java.util.Date into a java.sql.Date? Transformation: java.sql.Date sd; java.util.Date ud; / / initialize the ud such as ud = new java.util.Date (); sd = new java.sql.Date (ud.getTime ()); 2, if you want to insert into the database and the corr

  • and String 2010-03-18

    package date_transfor; import java.text.SimpleDateFormat; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; / ** * * @ Author henry.xiao * * / public class Date_transfor ( private static Log log = LogFactory.getLog

  • Oracle Date field operations (java.util.Date | java.sql.Date | java.sql.Timestamp)) 2011-02-16

    First show a little bit of time these common relationships between the classes A java.util.Date into a String java.util.Date udate=Calendar.getInstance().getTime(); Format f = new SimpleDateFormat("yyyy-MM-dd hh24:mm:ss"); System.out.println(f.f

  • 2012-07-17

    转自: java.lang.Object ....|__java.util.Date ..........|__java.sql.Date/java.sql.Timestamp /java.sql.Time [父类]java.util.Date日期格式为:年月日时分秒 [子类]java.sql.Date日期格式为:年月日[只存储日期数据不存储时间数据] [子类]java.sql.Time日期格式为:时分秒 [子类]java.sql.Timestamp日期格式为:年月日时分秒纳秒(毫微秒) 针对不

  • java type and standard sql type table 2010-09-26

    Type of standard java type mapping sql type integer int or Integer INTEGER long long or java.lang.Long BIGINT short short or java.lang.Short SMALLINT float float or java.lang.Float FLOAT double double or java.lang.Double DOUBLE big_decimal java.math.

  • The most commonly used date java class method 2011-04-22

    1. Calculate the maximum number of days a month Calendar time = Calendar.getInstance (); time.clear (); time.set (Calendar.YEAR, year); / / year as int time.set (Calendar.MONTH, i-1); / / Note, Calendar object to 0 by default in January int day = tim

  • java way to connect SQL 2005 2010-05-15

    java way to connect SQL 2005: (2008-04-23 00:48:02) reproduced Tags: it Category: JAVA Learning 1. To the Microsoft website to download and unzip jdbc 2005, access to documents sqljdbc.jar 2. Copy files to the jdk directory sqljdbc.jar \ jdk1.5 \ jre

  • Save BLOB type reported java.lang.ClassCastException: oracle.sql.BLOB solution 2009-03-20

    Default tomcat's common / lib is not under ojdbc14.jar this jar package, However, if you want to use jndi configure the database connection pool, then have to manually put ojdbc14.jar into common / lib, the When publishing the works which need to ach

  • Solution java.lang.classcastexception oracle.sql.blob cannot be cast to oracle.sql.blob 2010-06-03

    Save the file to the database at the time, reported java.lang.classcastexception oracle.sql.blob cannot be cast to oracle.sql.blob Solutions, see red OutputStream out = rs.getBlob (1). SetBinaryStream (1L);

  • java how to connect sql server 2005 2011-03-10

    One. JAVA Connection SQL Statement JAVA Connect to SQL2000 statement : Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); DriverManager.getConnection("jdbc:odbc: Database name "," database login name "," Password "); Bu

  • java跟踪执行的sql语句示例分享 2015-04-07

    这篇文章主要介绍了java跟踪执行的sql语句示例分享,需要的朋友可以参考下 代码: package com.lwj.test.proxy; import java.lang.reflect.InvocationHandler; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.sql.Co

  • Java Service Wrapper tool for the Java program into Windows Service 2010-09-03

    Download: Using a version Annex is a configured catalog, download directly available (make sure the same jdk path). Is a surveillance F: \ TDDownload directory changes in

  • Java SE for Business延长对Java SE 1.4的支持 2013-11-05

    基于大量客户和ISV的回馈,Sun决定启动Java SE for Business支持,在Sun对Java SE发布的支持以外提供更长时间的维护支持.这一服务将通过付费订阅来实现.Sun客户端软件小组的副主席Jeet Kaul这样说到: 在更早的发布系列上运行Java应用的客户和协作方,现在可以选择移植到较新的发布上,或者也可以选择注册Java SE for Business来继续获得关键的可靠性.有效性以及安全的升级,同时还可以得到对现存应用的新操作环境的支持. Java SE for Bus

  • Java基础-环境变量设置及Java命令行使用 2012-11-11

    一.Java环境变量设置 windows 下配置JDK环境变量: 1. 安装JDK,安装过程中可以自定义安装目录等信息,例如我们选择安装目录为D:\java\jdk1.5.0_08; 2. 安装完成后,右击"我的电脑",点击"属性"; 3. 选择"高级"选项卡,点击"环境变量"; 4. 在"系统变量"中,设置3项属性,JAVA_HOME,PATH,CLASSPATH(大小写无所谓),若已存在则点击"

  • Java对象的实例化过程以及Java变量如何进行初始化 2013-06-22

    Java对象的实例化过程: 1 检查类是否加载,如果没有加载就加载这个类,要先加载所有父类. * 懒惰式加载(按需加载):如果第一次用到就加载,只加载一次.通过CLASSPATH指定的路径寻找类文件(.class),加载以后是一个对象,类型是Class. * 获得这个类对象:Student.class-> Class 实例 2 在内存堆中分配对象空间.递归分配所有父类和子类属性空间. 属性默认自动初始化.自动初始化为"0"值. 3 进行属性的赋值. 4 递归调用父类构造器.(默认

  • Java异常处理终结篇--如何进行Java异常处理设计 2014-03-03

    有一句这样话:一个衡量Java设计师水平和开发团队纪律性的好方法就是读读他们应用程序里的异常处理代码. 本文主要讨论开发Java程序时,如何设计异常处理的代码,如何时抛异常,捕获到了怎么处理,而不是讲异常处理的机制和原理. 在我自己研究Java异常处理之前,我查过很多资料,翻过很多书藉,试过很多搜索引擎,换过很多英文和中文关键字,但是关于异常处理设计的文章实在太少,在我研究完Java异常处理之后,我面试过很多人,也问过很多老员工,极少碰到对Java异常有研究的人,看来研究这个主题的人很少,本文内

  • How To Use the Analysis Services Processing Task in SQL Server 2005 Integration 2010-11-11

    Transfer from the following: Problem We are looking to automate the processing of our SQL Server Analysis Services dimensions and cubes. We'd like to add this processing to our existing SQL Server Integration

  • sql server date format conversion method Daquan ---- reproduced 2010-09-08

    sql server date format conversion method Daquan Sql server database in operation, it often will use the date format conversion method, I compiled a frequently used inside SQL Server date format conversion of several methods for later use. Chinese ver

  • Supports different formats date string that is automatically parsed date tools 2010-03-29

    Write java code in the process, often use some general-purpose processing functions, apache's commons series library is prepared for us, most of the time there is no need to re-develop their own libraries, almost the majority of applications are dire

  • How to java.util.Date objects into MySQL's DATETIME type field? 2010-08-28

    // Log Table of dt field is DATETIME Type PreparedStatement ps = conn.preparedStatement("insert into log(dt) value(?)"); // MySQL DATETIME type in the default format for "yyyy-MM-dd HH:mm:ss" SimpleDateFormat sdf = new SimpleDateFormat