I managed in JAVA to store a calendar into a mysql DATETIME field
To fetch this value
entry.date = Calendar.getInstance(TimeZone.getTimeZone("UT"));
entry.date.setTime(rs.getDate(DBBLogEntries.entDate));
Where the entry.date is a java.util.Calendar
In the database the value is this: '2012-07-07 07:18:46'
I store all date values in a unique timezone in the db. ready to make all the extra work required to add or substract hours depending on the country from wich the request is comming.
The problem is that it brings the date but doesn't seem to brinng me the time.
Any sugestion please?
Thanks in advance.
Probably because Java has a different date format than mysql format(YYYY-MM-DD HH:MM:SS)
Visit the link :
http://www.coderanch.com/t/304851/JDBC/java/Java-date-MySQL-date-conversion
You may use SimpleDateFormat as follows.
java.util.Date dt = new java.util.Date();
java.text.SimpleDateFormat sdf =
new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateTime = sdf.format(dt);
You should read a timestamp from the ResultSet object.
java.sql.Timestamp ts = rs.getTimestamp( DBBLogEntries.entDate );
Which returns a Timestamp instance that includes date and time.
don't use Java.util.Date ,use the Java.sql.Date.
Are you using the MySql DATE type? This does not preserve the time component.
http://dev.mysql.com/doc/refman/5.5/en/datetime.html
Alternatively how are you retrieving the date from the db?
Related
I'm using Spring Boot and Hibernate 5 along with MySQL in my project.
How can I get a DateTime from the database in UTC instead of my JVM's timezone?
Is there any configuration to add to the application.properties like jadira.usertype.javaZone for Jadira or some other trick?
Since Hibernate 5.2, you can now use the hibernate.jdbc.time_zone configuration property.
Basically, you can now force the UTC time zone for all TIMESTAMP and TIME columns using the following configuration property:
<property name="hibernate.jdbc.time_zone" value="UTC"/>
To set UTC for all the date fields add following fields to MySQL connection string:
useTimezone=true
serverTimezone=UTC
useLegacyDatetimeCode=false
Example connection string:
jdbc:mysql://hostname/databaseName?useTimezone=true&serverTimezone=UTC&useLegacyDatetimeCode=false
AND
Use jadira user type to set UTC for a particular field
#Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime",
parameters = { #Parameter(name = "databaseZone", value = "UTC")})
private DateTime dateTime;
You can set the default timezone of your JVM to UTC when you start your app like
TimeZone.setDefault(TimeZone.getTimeZone("Etc/UTC"));
This will make sure the date is always in UTC when it moves around in your application, saved to db, retrieved from db etc.. and can be converted to any desired timezone when required.
You can get the date from the database and convert it to UTC
Date localTime = getDateFromDB();
String format = "yyyy/MM/dd HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(format);
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
Date gmtTime = new Date(sdf.format(localTime));
Thank you all for your help, i tried Arul Kumaran and Sanj solutions but it's not working well. So i've chosen to store dates as varchar and use a converter to get it as a ZonedDateTime
I am doing some XML parsing with SAX
Some elements look like this:
<show time="2014-07-05 20:00:00" />
I need to get a Timestamp out of above String.
Furthermore I also need to get a java.sql.Date, which of course should ignore the exact time.
I saw that there is a Timestamp.parse(String s) method available, but it is deprecated.
Therefore I wanted to ask what is the best Method to get a Timestamp out of this string, and a sql Date out of the Timestamp or String.
Thanks in advance
Edit:
I by the way still have to use Java 1.7 for this project
java.text.SimpleDateFormat dateFormat = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.util.Date date = dateFormat.parse("2014-07-05 20:00:00");
java.sql.Timestamp timestamp = new java.sql.Timestamp(date.getTime());
java.sql.Date sqlDate = new java.sql.Date(date.getTime());
I have bunch of dates in a format like Jan. 14,2014,Apr. 20,2014,Aug. 14,2014 etc,. which are extracted from a set of PDF documents.
My Problem
I added the above dates to a mysql column with Column Datatype as Date using java (by PreparedStatement).
....
st.SetDate(3,"Jan. 14,2014");
....
I added the datas using ExecuteQuery.
But when program executes an error message returned by MySql stating that the date formats are incompatible with MySql column type) Date..
My Question
How to convert this above mentioned date formats into mysql compatible Date formats ?
By your current posted code:
st.SetDate(3,"Jan. 14,2014");
This does not even compile. You could try getting a new Date object from a String (since this is what you're trying to accomplish), so use a SimpleDateFormat for this:
SimpleDateFormat sdf = new SimpleDateFormat("MMM. dd,yyyy");
Date date = sdf.parse("Jan. 14,2014");
st.setDate(3, new java.sql.Date(date.getTime()));
//rest of your code...
Similar to this, you can parse time or date and time into a java.util.Date using SimpleDateFormat and then convert it to the respective class java.sql.Time and java.sql.Timestamp using date.getTime().
Also note that you can p̶a̶s̶s̶ retrieve a java.util.Date object reference to PreparedStatement#getDate (and getTime and getTimestamp) since java.sql.Date, java.sql.Time and java.sql.Timestamp extend from java.util.Date. For more info, please refer here: Date vs TimeStamp vs calendar?
Assuming the column type supports a Date value, you could use a SimpleDateFormat to parse the String values to a java.util.Date and create a java.sql.Date which can be applied to the setDate method...
SimpleDateFormat sdf = new SimpleDateFormat("MMM. dd,yyyy");
Date date = sdf.parse("Jan. 14,2014");
java.sql.Date sqlDate = new java.sql.Date(date.getTime());
Check it SimpleDateFormat for more details
One possible solution is to use the String datatype instead of date in your table.
Use SimpleDateFormat to get the string representation of the date to a Date Object.
This date object can then be used to feed the set date method of the prepared statement.
SimpleDateFormat sdf = new SimpleDateFormat(....)
java.util.Date date = sdf.parse(....);
preparedStmt.setDate(...., date);
first convert the java.util.Date to java.sql.Date then try to set the Java.sql.Date
you can use this logic to convert
If your date is String then first convert it into Java.util.Date type either by using the SimpleDateFormat or DateFormat
If u want to use a DateFormat you can use it also:
But this changes the expected date format depending on the locale settings of the machine it's running on.
If you have a specific date format, you can use SimpleDateFormat:
Date d = new SimpleDateFormat("MMM. dd,yyyy").parse("Jan. 14,2014");
java.sql.Date sqlDate = new java.sql.Date(d.getTime());
I don't really think this all java stuff is necessary. You can simply use this very easy sql process to insert date: NOW() in mysql query like INSERT INTO table_name(c1,c2,datecolumn) VALUES('value1','value2',NOW()); It is much simplier I think :D
so i have a mssql database with a DateObjectCreated column of type DateTime. The values it will accept into the table are in the format 2013-12-23 12:23:56.567. However, my java program is creating a joda DateTime object with the format 2013-12-23T 12:23:56.567Z. this wont insert into my db. i need to either convert "2013-12-23T 12:23:56.567Z" to 2013-12-23 12:23:56.567 or find a way to allow my db table to accept "2013-12-23T 12:23:56.567Z" format
any help on this matter will be much appreciated
Many thanks
Billy
Controller
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
String formattedDate = formatter.format(date);//this gives me the string as i need it
DateTime dt = new DateTime(formattedDate);//here it adds the 'T' and 'Z'
ive tried
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
String formattedDate = formatter.format(date);//this gives me the string as i need it
Date dt = formatter.parse(formattedDate);//here it gives me the same as new Date()
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date myDate = formatter.parse(date);
2.enter link description here
The TO_DATE function can be used in Oracle/PLSQL. For example:
TO_DATE('2003/07/09', 'yyyy/mm/dd') would return a date value of July 9, 2003
TO_DATE('070903', 'MMDDYY') would return a date value of July 9, 2003
TO_DATE('20020315', 'yyyymmdd') would return a date value of Mar 15, 2002
Your DB column has a DateTime type. The text representation of your date is irrelevant for persisting it.
Regardless of the API you are using (JDBC, JPA, Hibernate) there will be something like a "setDateTime(Date date)" method that allows you to pass in a java.util.Date or java.sql.Date or a Java long. You can use the milli-second value of your Joda DateTime object to create whatever is required by the API.
You are working way too hard.
Convert your Joda-Time DateTime object to a java.util.Date. Just call toDate() method.
Pass Date instance to your framework or SQL.
The answer by Ralf is correct in its first part, but is wrong in the end in that you need not deal with milliseconds. Joda-Time knows how to convert to java.util.Date.
org.joda.time.DateTime now = new org.joda.time.DateTime();
java.util.Date nowAsJavaUtilDate = now.toDate();
By the way, the java.sql.Date class is simply a very thin subclass of java.util.Date. So don't let that throw you.
In the future, this will become even easier. Java 8 brings the new JSR 310 classes in the java.time.* package. These classes supplant the mess that is java.util.Date/Calendar. They are inspired by Joda-Time but are entirely re-architected. As they are a built-in part of Java, expect JDBC and related frameworks to be updated to handle these new types directly.
I need to convert a SQL Server timestamp format (stored on Timestamp java data type) into this format yyyy-mm-ddThh:mm:ss.sssZ (it's the date format parsed by Alfresco)
How can I do it?
thanks for your time!
Andrea
Try this SimpleDateFormat:
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" );
String yourformattedDate = sdf.format(yourDate);
You could try and get the time (in ms) from your TimeStamp instance, create a Date instance and use the DateFormatter class to format it anyway you want. That is, assuming DateFormatter supports the format you want.