mirror of
https://github.com/plexusorg/toml4j.git
synced 2025-02-22 07:54:28 +00:00
Add methods to control date formatting and time zones.
This commit is contained in:
parent
ecbd9f048d
commit
3d90260d32
2 changed files with 65 additions and 5 deletions
|
@ -1,9 +1,9 @@
|
||||||
package com.moandjiezana.toml;
|
package com.moandjiezana.toml;
|
||||||
|
|
||||||
|
import java.text.DateFormat;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.GregorianCalendar;
|
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
@ -12,7 +12,6 @@ class DateConverter implements ValueConverter, ValueWriter {
|
||||||
|
|
||||||
static final DateConverter DATE_PARSER = new DateConverter();
|
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 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
|
@Override
|
||||||
public boolean canConvert(String s) {
|
public boolean canConvert(String s) {
|
||||||
|
@ -91,10 +90,22 @@ class DateConverter implements ValueConverter, ValueWriter {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(Object value, WriterContext context) {
|
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));
|
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
|
@Override
|
||||||
|
|
|
@ -1,8 +1,11 @@
|
||||||
package com.moandjiezana.toml;
|
package com.moandjiezana.toml;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
import java.text.DateFormat;
|
||||||
|
import java.util.GregorianCalendar;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.TimeZone;
|
||||||
|
|
||||||
import static com.moandjiezana.toml.ValueWriters.WRITERS;
|
import static com.moandjiezana.toml.ValueWriters.WRITERS;
|
||||||
|
|
||||||
|
@ -27,6 +30,8 @@ public class TomlWriter {
|
||||||
|
|
||||||
private WriterIndentationPolicy indentationPolicy = new WriterIndentationPolicy();
|
private WriterIndentationPolicy indentationPolicy = new WriterIndentationPolicy();
|
||||||
private boolean wantSpaceyArraysValue = true;
|
private boolean wantSpaceyArraysValue = true;
|
||||||
|
private GregorianCalendar calendar = new GregorianCalendar();
|
||||||
|
private DateFormat customDateFormat = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a TomlWriter instance.
|
* Creates a TomlWriter instance.
|
||||||
|
@ -125,4 +130,48 @@ public class TomlWriter {
|
||||||
public boolean wantSpaceyArrays() {
|
public boolean wantSpaceyArrays() {
|
||||||
return wantSpaceyArraysValue;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue