I have a scheduler that run after midnight (around 1 am), to move data from Redis to Oracle. To get the actual date from yesterday I used JodaTime and substract the date by 1 day, the code looks like this
DateTime today = new DateTime();
logger.info("today date = {}", today);
DateTime dateTimeMinus1 = today.minusDays(1);
logger.info("today dateTimeMinus1 = {}", dateTimeMinus1);
And the logger output like this :
today date = 2015-02-13T01:13:00.073+07:00
today dateTimeMinus1 = 2015-02-12T01:13:00.073+07:00
after that I convert the JodaTime object to java.util.Date and set a property on a Java object (don't ask why, this is the requirement) using code like this :
myObject.setTxDate(dateTimeMinus1.toDate());
and then I save myObject
to Oracle 11g R2 database using Hibernate, the mapping look like this
<property name="txDate" column="TX_DATE" type="timestamp" not-null="false" />
As you can see from log output above the dateTimeMinus1
is print the correct date (12 Feb 2015 1:13 AM) but when I query the date in Oracle in change to 11-02-2015 18:13:00
I check the timezone between the apps server and db server, both of them set to same timezone (+07:00)
Any ideas what that could have been change the date when it's saved to database ?
While quering to DB whatever format it may show, but when it comes to java it always shows the default format(ie. Fri Feb 13 14:42:01 IST 2015) irrespective of the DB format.
To solve this, you need to make use of SimpleDateFormat.
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
Date today = new Date();
System.out.println("Original date: "+today);
String date = sdf.format(today);
System.out.println("Formatted date: "+date);
Output is:
Original date: Fri Feb 13 14:42:01 IST 2015
Formatted date: 13-02-2015 02:42:01
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.