001/* 002 * (C) Copyright 2007 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Nuxeo - initial API and implementation 016 * 017 * $Id: RelationDate.java 28460 2008-01-03 15:34:05Z sfermigier $ 018 */ 019 020package org.nuxeo.ecm.platform.relations.api.impl; 021 022import java.text.DateFormat; 023import java.text.ParseException; 024import java.text.SimpleDateFormat; 025import java.util.Calendar; 026import java.util.Date; 027 028import org.nuxeo.ecm.platform.relations.api.Literal; 029 030/** 031 * Relation date management. 032 * 033 * @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a> 034 */ 035public class RelationDate { 036 037 private RelationDate() { 038 } 039 040 protected static DateFormat getDateFormat() { 041 // not thread-safe so don't use a static instance 042 return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); 043 } 044 045 /** 046 * Returns Literal for given date. 047 */ 048 public static Literal getLiteralDate(Date date) { 049 return new LiteralImpl(getDateFormat().format(date)); 050 } 051 052 /** 053 * @return Literal for given Calendar instance 054 */ 055 public static Literal getLiteralDate(Calendar cal) { 056 return getLiteralDate(cal.getTime()); 057 } 058 059 /** 060 * @return Date instance for given literal. 061 */ 062 public static Date getDate(Literal dateLiteral) { 063 Date date = null; 064 if (dateLiteral != null) { 065 String dateString = dateLiteral.getValue(); 066 try { 067 date = getDateFormat().parse(dateString); 068 } catch (ParseException err) { 069 } 070 } 071 return date; 072 } 073 074 /** 075 * @return Calendar instance for given literal. 076 */ 077 public static Calendar getCalendar(Literal dateLiteral) { 078 // TODO optim ? 079 Calendar cal = Calendar.getInstance(); 080 Date date = getDate(dateLiteral); 081 if (date == null) { 082 return null; 083 } 084 085 cal.setTime(date); 086 return cal; 087 } 088 089}