Skip to content

Instantly share code, notes, and snippets.

@SrBlecaute01
Last active June 18, 2021 16:41
Show Gist options
  • Save SrBlecaute01/92dd276d85580ac3f9997513c413c137 to your computer and use it in GitHub Desktop.
Save SrBlecaute01/92dd276d85580ac3f9997513c413c137 to your computer and use it in GitHub Desktop.
format time and unformat it
package br.com.blecaute.core.utils;
import lombok.Getter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@Getter
public enum TimeUtils {
DAY(86400000, "days","day","d","dia", "dias"),
HOUR(3600000, "hours", "hour", "h", "hora", "horas"),
MINUTE(60000, "minutes", "minute", "m", "minuto", "minutos"),
SECOND(1000, "seconds", "second", "s", "segundo", "segundos");
private final long millis;
private final List<String> formats;
private static final Pattern pattern = Pattern.compile("(\\d+)([a-zA-Z]+)");
private static final String[] SMALL_FORMAT = {"D", "H", "M", "S"};
private static final String[] LONG_FORMAT = {" dia", " hora", " minuto", " segundo"};
public static final TimeUtils[] values = values();
TimeUtils(long millis, String... formats) {
this.millis = millis;
this.formats = Arrays.asList(formats);
}
public static Long getTime(String string) {
Matcher matcher = pattern.matcher(string);
long time = 0;
while(matcher.find()) {
try {
int value = Integer.parseInt(matcher.group(1));
TimeUtils type = fromFormat(matcher.group(2));
if(type != null) {
time += (value * type.getMillis());
}
}catch (Exception ignored){}
}
return time;
}
public static String smallFormat(long value) {
return value <= 0 ? "0" + SMALL_FORMAT[3] : getFormat(parseTime(value), SMALL_FORMAT, false);
}
public static String format(long value) {
return value <= 0 ? "0" + LONG_FORMAT[3] + "s" : getFormat(parseTime(value), LONG_FORMAT, true);
}
private static String getFormat(long[] times, String[] names, boolean plural) {
List<String> values = new ArrayList<>();
for(int index = 0; index < times.length; index++) {
long time = times[index];
if(time > 0) {
values.add(time + names[index] + (plural && time > 1 ? "s" : ""));
}
}
if(values.isEmpty()) {
return "";
}
if(values.size() == 1) {
return values.get(0);
}
return String.join(", ", values.subList(0, values.size() - 1)) + " e " + values.get(values.size() - 1);
}
private static long[] parseTime(long value) {
long days = TimeUnit.MILLISECONDS.toDays(value);
long hours = TimeUnit.MILLISECONDS.toHours(value) - (days * 24);
long minutes = TimeUnit.MILLISECONDS.toMinutes(value) - (TimeUnit.MILLISECONDS.toHours(value) * 60);
long seconds = TimeUnit.MILLISECONDS.toSeconds(value) - (TimeUnit.MILLISECONDS.toMinutes(value) * 60);
return new long[]{days, hours, minutes, seconds};
}
private static TimeUtils fromFormat(String format) {
format = format.toLowerCase();
for(TimeUtils time : values) {
if(time.getFormats().contains(format)) {
return time;
}
}
return null;
}
}
@Yuhtin
Copy link

Yuhtin commented Jan 29, 2021

8316 of complexity, monkaW https://prnt.sc/xtqmas

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment