Hibernate Date Format

This section contains description of Hibernate Date Format.


 

Ads

Tutorials   
Hibernate Examples Hibernate 5 build SessionFactory Example Hibernate 5 SessionFactory Example Hibernate 5 Annotation Example How to download Hibernate 5.x? Hibernate 5 Features Hibernate 5 Tutorials Learning Hibernate ORM - Free Training/Tutorial videos of Hibernate ORM with Java How to write hello world example program in Hibernate? How to create one to one relationship in Hibernate 4 with Annotation? How to calculate minimum in Hibernate using the min() Function Hibernate ORM 4.2.21.Final Released - Create CURD application in latest Hibernate Hibernate ORM 4.2.21 Tutorial - Learn Hibernate ORM Framework Hibernate Error: Failed to create sessionFactory object.java.lang.NoClassDefFoundError How to create Hibernate Project in Eclipse? Examples of Hibernate Criteria Query Hibernate ORM 4.3.6.Final released Hibernate ORM 4.2.14.Final released Hibernate 4.3.4 Released: Hibernate 4.3.4.Final Maven dependency Hibernate association and join example Learn Hibernate programming with Examples Introduction to Hibernate Framework Hibernate Tutorial: Learn Hibernate with examples Hibernate 4.3 Hello World: Example Hibernate ORM 4.3.1.Final released How to create SessionFactory in Hibernate 4.3.1? Hibernate 4 Hello World: Example of Hello World program Hibernate 4 create Session Factory: Example of creating Session Factory in Hibernate 4 Hibernate 4.3 and JPA 2.1 Example What is Hibernate in Java? Hibernate Overview and Architecture What is JPA 2.1? Hibernate 4.3.0.Final Maven dependency Hibernate 4.3 ORM Features Hibernate 4.3 Tutorial Hibernate Criteria average example Hibernate Criteria load all objects from table Hibernate Criteria Hibernate ORM 4.3.0.Final released Hibernate beginner tutorial Hibernate Session.delete() Example Hibernate save or update method example Hibernate Load Example Hibernate save Example - Rose India Hibernate 4 tutorials Hibernate Example Step by Step in Eclipse Hibernate Configuration files Setup Hibernate Environment Hibernate Architecture Hibernate Overview Hibernate 4.2 Tutorial
Ads

Hibernate Date Format

This section contains description of Hibernate Date Format.

Hibernate Date Format :

When you save date to the database using hibernate, the date is converted to local time and stored.
Suppose your date is '12-06-2012' ,hibernate inserts it into database in default format of"yyyy-mm-dd".
Timestamp format is yyyy-mm-dd hh:mm:ss.

Timestamp format takes 4 byte whereas DtaeTimeFormat takes 8 byte.

Hibernate provides date type annotation which takes format 'yyyy-mm-dd'

@Type(type = "date")
@Column(name = "date_of_join")
private Date dateOfJoin;

Example : In this example we are selecting employee whose id is 1 and displaying date in different format.

package net.roseindia.main;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import net.roseindia.table.Employee;
import net.roseindia.util.HibernateUtil;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class HibernateDateFormat {
public static void main(String[] args) {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();;

try {
String hql = "SELECT emp from Employee emp WHERE emp.id=1";
Query query1 = session.createQuery(hql);
List<Employee> objectList = query1.list();
Iterator iterator = objectList.iterator();
Employee employee = (Employee) iterator.next();
Date date = employee.getDateOfJoin();
SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
String dateBufferString = format.format(date);
Date dateBuffer;
try {

dateBuffer = format.parse(dateBufferString);
System.out.println("Database Date Format :" + date);
System.out.println("(dd-MM-yyyy) date: " + format.format(date));
System.out.println("dateBufferSTring " + dateBufferString);
System.out.println("dateBuffer: " + dateBuffer);

} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (HibernateException e) {
// TODO: handle exception
} finally {
session.clear();
session.close();
}
}
}

Output :

Hibernate: select employee0_.emp_id as emp1_0_, employee0_.date_of_join as date2_0_, employee0_.name as name0_, employee0_.salary as salary0_ from employee employee0_ where employee0_.emp_id=1
Database Date Format :2010-03-12
(dd-MM-yyyy) date: 12-03-2010
dateBufferSTring 12-03-2010
dateBuffer: Fri Mar 12 00:00:00 IST 2010

Click here to download complete source code

Advertisements

Ads
Share on Google+Share on Google+

Hibernate Date Format

Posted on: August 1, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: Hibernate Date Format  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads