001/*
002 *  Copyright 2021 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.forms;
017
018import java.time.LocalDate;
019import java.time.ZoneId;
020import java.time.ZonedDateTime;
021
022import org.ametys.core.util.DateUtils;
023
024/** Forms date utils */
025public final class FormDateUtils
026{
027    private FormDateUtils()
028    {
029        // empty
030    }
031    
032    /** 
033     * Converts this {@link LocalDate} object to a {@link ZonedDateTime}.
034     * @param date the LocalDate
035     * @return an instant representing the same point on the time-line as this {@link LocalDate} object
036     */
037    public static ZonedDateTime asZonedDateTime(LocalDate date)
038    {
039        return date.atStartOfDay(ZoneId.of("UTC"));
040    }
041    
042    /** 
043     * Converts a {@link ZonedDateTime} object to {@link String} using the ISO date formatter
044     * @param zonedDateTime the zoned date time
045     * @return the zoned date time as a {@link String}
046     */
047    public static String zonedDateTimeToString(ZonedDateTime zonedDateTime)
048    {
049        return zonedDateTime.format(DateUtils.getISODateTimeFormatter());
050    }
051    
052    /** 
053     * Parses a String into a {@link ZonedDateTime}, using ISO date formatter.
054     * @param zonedDateTimeAsString the zoned date time as string
055     * @return the {@link ZonedDateTime} object
056     */
057    public static ZonedDateTime parseZonedDateTime(String zonedDateTimeAsString)
058    {
059        return ZonedDateTime.parse(zonedDateTimeAsString, DateUtils.getISODateTimeFormatter());
060    }
061}