001/* 002 * (C) Copyright 2007 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Nuxeo - initial API and implementation 018 * 019 * $Id: RelationDate.java 28460 2008-01-03 15:34:05Z sfermigier $ 020 */ 021 022package org.nuxeo.ecm.platform.relations.api.impl; 023 024import java.text.DateFormat; 025import java.text.ParseException; 026import java.text.SimpleDateFormat; 027import java.util.Calendar; 028import java.util.Date; 029 030import org.nuxeo.ecm.platform.relations.api.Literal; 031 032/** 033 * Relation date management. 034 * 035 * @author <a href="mailto:[email protected]">Anahide Tchertchian</a> 036 */ 037public class RelationDate { 038 039 private RelationDate() { 040 } 041 042 protected static DateFormat getDateFormat() { 043 // not thread-safe so don't use a static instance 044 return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); 045 } 046 047 /** 048 * Returns Literal for given date. 049 */ 050 public static Literal getLiteralDate(Date date) { 051 return new LiteralImpl(getDateFormat().format(date)); 052 } 053 054 /** 055 * @return Literal for given Calendar instance 056 */ 057 public static Literal getLiteralDate(Calendar cal) { 058 return getLiteralDate(cal.getTime()); 059 } 060 061 /** 062 * @return Date instance for given literal. 063 */ 064 public static Date getDate(Literal dateLiteral) { 065 Date date = null; 066 if (dateLiteral != null) { 067 String dateString = dateLiteral.getValue(); 068 try { 069 date = getDateFormat().parse(dateString); 070 } catch (ParseException err) { 071 } 072 } 073 return date; 074 } 075 076 /** 077 * @return Calendar instance for given literal. 078 */ 079 public static Calendar getCalendar(Literal dateLiteral) { 080 // TODO optim ? 081 Calendar cal = Calendar.getInstance(); 082 Date date = getDate(dateLiteral); 083 if (date == null) { 084 return null; 085 } 086 087 cal.setTime(date); 088 return cal; 089 } 090 091}