
eu.hansolo.toolbox.time.Times Maven / Gradle / Ivy
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2022 Gerrit Grunwald.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package eu.hansolo.toolbox.time;
import java.time.Instant;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
public enum Times {
/**
* Format : HH:mm:ss.SSSS
* Example: 23:59:59.9999
*/
HH_mm_ss_SSSS("HH:mm:ss.SSSS"),
/**
* Format : HH:mm:ss
* Example: 23:59:59
*/
HH_mm_ss("HH:mm:ss"),
/**
* Format : HH:mm
* Example: 23:59
*/
HH_mm("HH:mm"),
/**
* Format : HHmmss.SSSS
* Example: 235959.9999
*/
HHmmss_SSSS("HHmmss.SSSS"),
/**
* Format : HHmmss
* Example: 235959
*/
HHmmss("HHmmss"),
/**
* Format : HHmm
* Example: 2359
*/
HHmm("HHmm"),
/**
* Format : HH
* Example: 23
*/
HH("HH");
private final DateTimeFormatter formatter;
Times(final String formatString) {
formatter = DateTimeFormatter.ofPattern(formatString);
}
public LocalTime parse(final String text) { return LocalTime.parse(text, formatter); }
public Instant parseTime(final String text) { return parseTime(text, ZoneId.systemDefault()); }
public Instant parseTime(final String text, final ZoneId zoneId) { return Instant.parse(text).atZone(zoneId).toInstant(); }
public String format(final LocalTime time) { return formatter.format(time); }
public String format(final Instant time) { return format(time, ZoneId.systemDefault()); }
public String format(final Instant time, final ZoneId zoneId) {
return format(time.atZone(zoneId).toLocalTime());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy