Hibernate Date Format

This section contains description of Hibernate Date Format.


Hibernate 5.3 features Hibernate 5 JPA Configuration Hibernate 5 persistence.xml example Hibernate 5 JPA Tutorial Hibernate and JPA Example How properties of a class are mapped to the columns of a database table in Hibernate? Hibernate 5 Annotations Maven dependency Hibernate 5 annotationconfiguration Doctype Hibernate-configuration 5 Hibernate 5 Dialect Hibernate 5 configuration dtd Hibernate 5 Hello World Getting started with Hibernate 5 Hibernate 5.2.10 jar Download Hibernate 5.2.10 Maven Dependency Hibernate 5 Maven Dependency Hibernate 5 native query example Hibernate 5 query deprecated When should you use Hibernate? What's new in Hibernate 5? How much time it takes to learn Hibernate? Hibernate envers custom revinfo table Hibernate 5 Envers Hibernate Envers get all revisions 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 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
} catch (HibernateException e) {
// TODO: handle exception
} finally {

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

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



Discuss: Hibernate Date Format  

Post your Comment

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