diff --git a/src/main/java/com/moandjiezana/toml/DateConverter.java b/src/main/java/com/moandjiezana/toml/DateConverter.java index 07761b0..19c7448 100644 --- a/src/main/java/com/moandjiezana/toml/DateConverter.java +++ b/src/main/java/com/moandjiezana/toml/DateConverter.java @@ -1,9 +1,9 @@ package com.moandjiezana.toml; +import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; -import java.util.GregorianCalendar; import java.util.concurrent.atomic.AtomicInteger; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -12,7 +12,6 @@ class DateConverter implements ValueConverter, ValueWriter { static final DateConverter DATE_PARSER = new DateConverter(); private static final Pattern DATE_REGEX = Pattern.compile("(\\d{4}-[0-1][0-9]-[0-3][0-9]T[0-2][0-9]:[0-5][0-9]:[0-5][0-9])(\\.\\d*)?(Z|(?:[+\\-]\\d{2}:\\d{2}))(.*)"); - private static final Calendar calendar = new GregorianCalendar(); @Override public boolean canConvert(String s) { @@ -91,10 +90,22 @@ class DateConverter implements ValueConverter, ValueWriter { @Override public void write(Object value, WriterContext context) { - SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:m:ss"); + DateFormat dateFormat; + DateFormat customDateFormat = context.getTomlWriter().getDateFormat(); + if (customDateFormat != null) { + dateFormat = customDateFormat; + } else { + dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:m:ss"); + } + dateFormat.setTimeZone(context.getTomlWriter().getTimeZone()); + context.output.append(dateFormat.format(value)); - int tzOffset = (calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET)) / (60 * 1000); - context.output.append(String.format("%+03d:%02d", tzOffset / 60, tzOffset % 60)); + + if (customDateFormat == null) { + Calendar calendar = context.getTomlWriter().getCalendar(); + int tzOffset = (calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET)) / (60 * 1000); + context.output.append(String.format("%+03d:%02d", tzOffset / 60, tzOffset % 60)); + } } @Override diff --git a/src/main/java/com/moandjiezana/toml/TomlWriter.java b/src/main/java/com/moandjiezana/toml/TomlWriter.java index abb703b..8b2324a 100644 --- a/src/main/java/com/moandjiezana/toml/TomlWriter.java +++ b/src/main/java/com/moandjiezana/toml/TomlWriter.java @@ -1,8 +1,11 @@ package com.moandjiezana.toml; import java.io.*; +import java.text.DateFormat; +import java.util.GregorianCalendar; import java.util.List; import java.util.Map; +import java.util.TimeZone; import static com.moandjiezana.toml.ValueWriters.WRITERS; @@ -27,6 +30,8 @@ public class TomlWriter { private WriterIndentationPolicy indentationPolicy = new WriterIndentationPolicy(); private boolean wantSpaceyArraysValue = true; + private GregorianCalendar calendar = new GregorianCalendar(); + private DateFormat customDateFormat = null; /** * Creates a TomlWriter instance. @@ -125,4 +130,48 @@ public class TomlWriter { public boolean wantSpaceyArrays() { return wantSpaceyArraysValue; } + + /** + * Set the {@link TimeZone} used when formatting datetimes. + * + * If unset, datetimes will be rendered in the current time zone. + * + * @param timeZone custom TimeZone. + * @return this TomlWriter instance + */ + public TomlWriter setTimeZone(TimeZone timeZone) { + calendar = new GregorianCalendar(timeZone); + return this; + } + + /** + * Get the {@link TimeZone} in use for this TomlWriter. + * + * @return the currently set TimeZone. + */ + public TimeZone getTimeZone() { + return calendar.getTimeZone(); + } + + /** + * Override the default date format. + * + * If a time zone was set with {@link #setTimeZone(TimeZone)}, it will be applied before formatting + * datetimes. + * + * @param customDateFormat a custom DateFormat + * @return this TomlWriter instance + */ + public TomlWriter setDateFormat(DateFormat customDateFormat) { + this.customDateFormat = customDateFormat; + return this; + } + + public DateFormat getDateFormat() { + return customDateFormat; + } + + GregorianCalendar getCalendar() { + return calendar; + } }