All Downloads are FREE. Search and download functionalities are using the official Maven repository.

be.skylark.weather.darkskyclient.utils.DateTimeUtils Maven / Gradle / Ivy

package be.skylark.weather.darkskyclient.utils;

import java.time.DateTimeException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;

/**
 * This class provides utility methods to extract dates and times from the API
 */
public class DateTimeUtils {

    /**
     * This method transforms the given Unix time in a LocalDateTime object
     * @param unixTime The Unix time to transform
     * @param timezone The timezone of the Unix time ; or the System default if null is provided
     * @return The Java LocalDateTime representation of the given Unix time and time zone
     * @throws DateTimeException if an error during the date generation occurs
     */
    public static LocalDateTime getDateTime( Long unixTime , String timezone ) throws DateTimeException {
        if ( unixTime == null )
            return null ;
        final ZoneId zoneId = ( timezone != null ) ? ZoneId.of( timezone ) : ZoneId.systemDefault() ;
        return Instant.ofEpochSecond( unixTime ).atZone( zoneId ).toLocalDateTime() ;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy