001/*
002 *  Copyright 2019 Anyware Services
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 */
016package org.ametys.plugins.survey;
017
018import java.time.ZoneId;
019import java.time.ZonedDateTime;
020import java.util.Calendar;
021import java.util.GregorianCalendar;
022
023import org.ametys.core.util.DateUtils;
024
025/**
026 * Survey date utils
027 * FIXME When the plugins Survey will be not compatible with 4.2.x version, use the methods in {@link DateUtils}
028 * See issue SURVEY-208
029 */
030public final class SurveyDateUtils
031{
032    private SurveyDateUtils()
033    {
034        // empty
035    }
036    
037    /** 
038     * Converts this {@link Calendar} object to a {@link ZonedDateTime}.
039     * @param calendar the calendar
040     * @return an instant representing the same point on the time-line as this {@link Calendar} object
041     */
042    public static ZonedDateTime asZonedDateTime(Calendar calendar)
043    {
044        ZonedDateTime zonedDateTime =  ZonedDateTime.of(calendar.get(Calendar.YEAR),
045                calendar.get(Calendar.MONTH) + 1,
046                calendar.get(Calendar.DAY_OF_MONTH),
047                calendar.get(Calendar.HOUR_OF_DAY),
048                calendar.get(Calendar.MINUTE),
049                calendar.get(Calendar.SECOND),
050                calendar.get(Calendar.MILLISECOND),
051                calendar.getTimeZone().toZoneId());
052        return zonedDateTime.withZoneSameInstant(ZoneId.of("UTC"));
053    }
054    
055    /** 
056     * Converts this {@link ZonedDateTime} object to a {@link Calendar}.
057     * @param zonedDateTime the zoned date time
058     * @return the {@link Calendar} object
059     */
060    public static Calendar asCalendar(ZonedDateTime zonedDateTime)
061    {
062        ZonedDateTime dateTimeOnUTC = zonedDateTime.withZoneSameInstant(ZoneId.of("UTC"));
063        return GregorianCalendar.from(dateTimeOnUTC);
064    }
065    
066    /** 
067     * Converts a {@link ZonedDateTime} object to {@link String} using the ISO date formatter
068     * @param zonedDateTime the zoned date time
069     * @return the zoned date time as a {@link String}
070     */
071    public static String zonedDateTimeToString(ZonedDateTime zonedDateTime)
072    {
073        return zonedDateTime.format(DateUtils.getISODateTimeFormatter());
074    }
075    
076    /** 
077     * Parses a String into a {@link ZonedDateTime}, using ISO date formatter.
078     * @param zonedDateTimeAsString the zoned date time as string
079     * @return the {@link ZonedDateTime} object
080     */
081    public static ZonedDateTime parseZonedDateTime(String zonedDateTimeAsString)
082    {
083        return ZonedDateTime.parse(zonedDateTimeAsString, DateUtils.getISODateTimeFormatter());
084    }
085}