Git Product home page Git Product logo

joda-time-mybatis's Introduction

Note

  • Looking for a new maintainer for this project. I no longer use Joda or Mybatis at my job, so my drive to update this project is low. Contact me if interested.

Comments

  • Your mileage may vary. Working with time zones can yield strange results
  • All databases (AFAIK) store dates in UTC (think epoch)
  • As such, joda-time-mybatis sets all dates to UTC (for Joda Instants, doesn't apply to Joda Partial classes of courses)
  • For consistent results, your JVM should either
    • be run in UTC (-Dtimezone=UTC)
    • be in the same timezone as the database server

Example

SQL (PostgreSQL in this case)

CREATE TABLE foo
(
	localdate date,
	datetime timestamp with time zone
);

POJO/VO

public class FooVO
{
	private LocalDate localdate;
	private DateTime datetime;
	...

myBatis XML

<resultMap id="fooResultMap" class="FooVO" >
	<result column="localdate" property="localdate" javaType="org.joda.time.LocalDate" typeHandler="org.joda.time.mybatis.handlers.LocalDateTypeHandler" />
	<result column="datetime" property="datetime" javaType="org.joda.time.DateTime" typeHandler="org.joda.time.mybatis.handlers.DateTimeTypeHandler" />
</resultMap>

<select id="loadFoo" resultMap="fooResultMap">
	SELECT	*
	FROM	foo
</select>

<insert id="insertFoo" parameterType="FooVO">
	INSERT INTO foo(localdate, datetime)
	VALUES(#{localdate,typeHandler=intouch.joda.mybatis.JodaLocalDateTypeHandler}, #{datetime,typeHandler=intouch.joda.mybatis.JodaDateTimeTypeHandler})
</insert>

Usage

DateTimeZone zone = DateTimeZone.forID("America/Vancouver");

FooVO fooVO = new FooVO();
fooVO.setLocaldate(new LocalDate(2000, 5, 10));
fooVO.setDatetime(new DateTime(zone));
logger.debug("LD: " + fooVO.getLocaldate());
logger.debug("DT: " + fooVO.getDatetime());
coreDAO.saveFoo(fooVO);

FooVO loadedFooVO = coreDAO.loadFoo();
logger.debug("LD: " + loadedFooVO.getLocaldate());
logger.debug("DT @ Def: " + loadedFooVO.getDatetime());
logger.debug("DT @ Van: " + loadedFooVO.getDatetime().withZone(zone));

Output (JVM running in UTC)

LD: 2000-05-10
DT: 2012-03-27T07:29:26.748-07:00
LD: 2000-05-10
DT @ Def: 2012-03-27T14:29:26.748Z
DT @ Van: 2012-03-27T07:29:26.748-07:00

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.