001/* 002 * (C) Copyright 2006-2019 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 * Florent Guillaume 018 */ 019 020package org.nuxeo.ecm.core.query.sql.model; 021 022import static org.nuxeo.common.utils.DateUtils.formatISODateTime; 023import static org.nuxeo.common.utils.DateUtils.parseISODateTime; 024 025import java.time.ZonedDateTime; 026import java.util.Calendar; 027import java.util.GregorianCalendar; 028 029/** 030 * @author Florent Guillaume 031 */ 032public class DateLiteral extends Literal { 033 034 private static final long serialVersionUID = 279219479611055690L; 035 036 public final ZonedDateTime value; 037 038 public final boolean onlyDate; 039 040 public DateLiteral(ZonedDateTime value) { 041 this.value = value; 042 this.onlyDate = false; 043 } 044 045 public DateLiteral(String value, boolean onlyDate) { 046 this.onlyDate = onlyDate; 047 this.value = parseISODateTime(value); 048 } 049 050 public Calendar toCalendar() { 051 return GregorianCalendar.from(value); 052 } 053 054 public java.sql.Date toSqlDate() { 055 return new java.sql.Date(value.toInstant().toEpochMilli()); 056 } 057 058 @Override 059 public String toString() { 060 String s = formatISODateTime(value, onlyDate); 061 if (onlyDate) { 062 return "DATE '" + s + "'"; 063 } else { 064 return "TIMESTAMP '" + s + "'"; 065 } 066 } 067 068 @Override 069 public String asString() { 070 return formatISODateTime(value, onlyDate); 071 } 072 073 @Override 074 public void accept(IVisitor visitor) { 075 visitor.visitDateLiteral(this); 076 } 077 078 @Override 079 public boolean equals(Object obj) { 080 if (obj == this) { 081 return true; 082 } 083 if (obj instanceof DateLiteral) { 084 return value.equals(((DateLiteral) obj).value); 085 } 086 return false; 087 } 088 089 @Override 090 public int hashCode() { 091 return value.hashCode(); 092 } 093 094 /** 095 * @deprecated since 11.1 as not used 096 */ 097 @Deprecated 098 public static String dateTime(DateLiteral date) { 099 return formatISODateTime(date.value); 100 } 101 102 /** 103 * @deprecated since 11.1 as not used 104 */ 105 @Deprecated 106 public static String date(DateLiteral date) { 107 return formatISODateTime(date.value); 108 } 109}