From 786b8d0df40401f734b589264ab73f1b0485765e Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Fri, 20 Mar 2020 14:56:48 +0100 Subject: [PATCH] [Minor] Added SimpleGs1 parser --- .../main/java/li/strolch/utils/SimpleGs1.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 li.strolch.utils/src/main/java/li/strolch/utils/SimpleGs1.java diff --git a/li.strolch.utils/src/main/java/li/strolch/utils/SimpleGs1.java b/li.strolch.utils/src/main/java/li/strolch/utils/SimpleGs1.java new file mode 100644 index 000000000..e93a9dac0 --- /dev/null +++ b/li.strolch.utils/src/main/java/li/strolch/utils/SimpleGs1.java @@ -0,0 +1,58 @@ +package li.strolch.utils; + +import static li.strolch.utils.helper.ExceptionHelper.formatException; +import static li.strolch.utils.helper.ExceptionHelper.getRootCause; + +import java.time.LocalDate; +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; + +public class SimpleGs1 { + + private String gtin; + private ZonedDateTime expirationDate; + private String batchNo; + + private SimpleGs1(String gs1) { + if (gs1.length() < 32 || gs1.length() > 46) + throw new IllegalArgumentException("Can not parse GS1 " + gs1); + try { + this.gtin = gs1.substring(2, 14); + this.expirationDate = LocalDate.parse(gs1.substring(18, 24), DateTimeFormatter.ofPattern("yyMMdd")) + .atStartOfDay(ZoneId.systemDefault()); + this.batchNo = gs1.substring(26); + } catch (Exception e) { + throw new IllegalArgumentException("Can not parse GS1 " + gs1 + ": " + formatException(getRootCause(e))); + } + } + + public String getGtin() { + return this.gtin; + } + + public ZonedDateTime getExpirationDate() { + return this.expirationDate; + } + + public String getBatchNo() { + return this.batchNo; + } + + public static SimpleGs1 valueOf(String gs1) { + return new SimpleGs1(gs1); + } + + @Override + public String toString() { + return "SimpleGs1{" + "gtin='" + gtin + '\'' + ", expirationDate=" + expirationDate + ", batchNo='" + batchNo + + '\'' + '}'; + } + + public static void main(String[] args) { + String example = "01076806134200251723090110D72375"; + //String example = "0107680671300017172106301092405"; + SimpleGs1 gs1 = SimpleGs1.valueOf(example); + System.out.println(gs1); + } +}