Skip to content

Instantly share code, notes, and snippets.

@JonasGao
Created August 15, 2023 11:58
Show Gist options
  • Select an option

  • Save JonasGao/0fbafc8f24b6aa9c606316047aafb7f1 to your computer and use it in GitHub Desktop.

Select an option

Save JonasGao/0fbafc8f24b6aa9c606316047aafb7f1 to your computer and use it in GitHub Desktop.
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.SignStyle;
import java.util.Random;
import static java.time.temporal.ChronoField.*;
import static java.time.temporal.ChronoField.SECOND_OF_MINUTE;
public class ErrorLogIdBuilder {
private static final DateTimeFormatter FORMATTER;
private static final Random RANDOM = new Random();
private static final String XEID = "XEID-";
static {
FORMATTER = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.appendValue(YEAR, 4, 10, SignStyle.EXCEEDS_PAD)
.appendValue(MONTH_OF_YEAR, 2)
.appendValue(DAY_OF_MONTH, 2)
.appendLiteral('T')
.appendValue(HOUR_OF_DAY, 2)
.appendValue(MINUTE_OF_HOUR, 2)
.optionalStart()
.appendValue(SECOND_OF_MINUTE, 2)
.optionalStart()
.toFormatter();
}
public String build() {
String timestamp = LocalDateTime.now().format(FORMATTER);
int anInt = RANDOM.nextInt(Integer.MAX_VALUE);
String value = String.format("%s-%010d", timestamp, anInt);
return XEID + value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment