下面列出了org.joda.time.format.ISODateTimeFormat#date ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private Serializer() {
dateTimeFormatter = ISODateTimeFormat.dateTimeNoMillis();
localDateFormatter = ISODateTimeFormat.date();
objectMapper = new ObjectMapper()
.registerModule(new JodaModule()
.addSerializer(DateTime.class, new DateTimeSerializer()
.withFormat(new JacksonJodaDateFormat(dateTimeFormatter), 0)
)
.addSerializer(LocalDate.class, new LocalDateSerializer()
.withFormat(new JacksonJodaDateFormat(localDateFormatter), 0)
)
)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE, true)
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false); // TODO: Deprecate in vNext
}
/**
* Gets a debugging toString.
*
* @return a debugging string
*/
public String toString() {
StringBuffer sb = new StringBuffer(60);
sb.append("GJChronology");
sb.append('[');
sb.append(getZone().getID());
if (iCutoverMillis != DEFAULT_CUTOVER.getMillis()) {
sb.append(",cutover=");
DateTimeFormatter printer;
if (withUTC().dayOfYear().remainder(iCutoverMillis) == 0) {
printer = ISODateTimeFormat.date();
} else {
printer = ISODateTimeFormat.dateTime();
}
printer.withChronology(withUTC()).printTo(sb, iCutoverMillis);
}
if (getMinimumDaysInFirstWeek() != 4) {
sb.append(",mdfw=");
sb.append(getMinimumDaysInFirstWeek());
}
sb.append(']');
return sb.toString();
}
/**
* Gets a debugging toString.
*
* @return a debugging string
*/
public String toString() {
StringBuffer sb = new StringBuffer(60);
sb.append("GJChronology");
sb.append('[');
sb.append(getZone().getID());
if (iCutoverMillis != DEFAULT_CUTOVER.getMillis()) {
sb.append(",cutover=");
DateTimeFormatter printer;
if (withUTC().dayOfYear().remainder(iCutoverMillis) == 0) {
printer = ISODateTimeFormat.date();
} else {
printer = ISODateTimeFormat.dateTime();
}
printer.withChronology(withUTC()).printTo(sb, iCutoverMillis);
}
if (getMinimumDaysInFirstWeek() != 4) {
sb.append(",mdfw=");
sb.append(getMinimumDaysInFirstWeek());
}
sb.append(']');
return sb.toString();
}
/**
* Gets a debugging toString.
*
* @return a debugging string
*/
public String toString() {
StringBuffer sb = new StringBuffer(60);
sb.append("GJChronology");
sb.append('[');
sb.append(getZone().getID());
if (iCutoverMillis != DEFAULT_CUTOVER.getMillis()) {
sb.append(",cutover=");
DateTimeFormatter printer;
if (withUTC().dayOfYear().remainder(iCutoverMillis) == 0) {
printer = ISODateTimeFormat.date();
} else {
printer = ISODateTimeFormat.dateTime();
}
printer.withChronology(withUTC()).printTo(sb, iCutoverMillis);
}
if (getMinimumDaysInFirstWeek() != 4) {
sb.append(",mdfw=");
sb.append(getMinimumDaysInFirstWeek());
}
sb.append(']');
return sb.toString();
}
/**
* Gets a debugging toString.
*
* @return a debugging string
*/
public String toString() {
StringBuffer sb = new StringBuffer(60);
sb.append("GJChronology");
sb.append('[');
sb.append(getZone().getID());
if (iCutoverMillis != DEFAULT_CUTOVER.getMillis()) {
sb.append(",cutover=");
DateTimeFormatter printer;
if (withUTC().dayOfYear().remainder(iCutoverMillis) == 0) {
printer = ISODateTimeFormat.date();
} else {
printer = ISODateTimeFormat.dateTime();
}
printer.withChronology(withUTC()).printTo(sb, iCutoverMillis);
}
if (getMinimumDaysInFirstWeek() != 4) {
sb.append(",mdfw=");
sb.append(getMinimumDaysInFirstWeek());
}
sb.append(']');
return sb.toString();
}
/**
* Gets a debugging toString.
*
* @return a debugging string
*/
public String toString() {
StringBuffer sb = new StringBuffer(60);
sb.append("GJChronology");
sb.append('[');
sb.append(getZone().getID());
if (iCutoverMillis != DEFAULT_CUTOVER.getMillis()) {
sb.append(",cutover=");
DateTimeFormatter printer;
if (withUTC().dayOfYear().remainder(iCutoverMillis) == 0) {
printer = ISODateTimeFormat.date();
} else {
printer = ISODateTimeFormat.dateTime();
}
printer.withChronology(withUTC()).printTo(sb, iCutoverMillis);
}
if (getMinimumDaysInFirstWeek() != 4) {
sb.append(",mdfw=");
sb.append(getMinimumDaysInFirstWeek());
}
sb.append(']');
return sb.toString();
}
/**
* Gets a debugging toString.
*
* @return a debugging string
*/
public String toString() {
StringBuffer sb = new StringBuffer(60);
sb.append("GJChronology");
sb.append('[');
sb.append(getZone().getID());
if (iCutoverMillis != DEFAULT_CUTOVER.getMillis()) {
sb.append(",cutover=");
DateTimeFormatter printer;
if (withUTC().dayOfYear().remainder(iCutoverMillis) == 0) {
printer = ISODateTimeFormat.date();
} else {
printer = ISODateTimeFormat.dateTime();
}
printer.withChronology(withUTC()).printTo(sb, iCutoverMillis);
}
if (getMinimumDaysInFirstWeek() != 4) {
sb.append(",mdfw=");
sb.append(getMinimumDaysInFirstWeek());
}
sb.append(']');
return sb.toString();
}
/**
* @param isNullable isNullable
* @param earliest lower boundary date
* @param latest upper boundary date
* @return a list of boundary dates
*/
public List<String> negativeCase(boolean isNullable, String earliest, String latest) {
List<String> values = new LinkedList<>();
DateTimeFormatter parser = ISODateTimeFormat.date();
DateTime earlyDate = parser.parseDateTime(earliest);
DateTime lateDate = parser.parseDateTime(latest);
String prevDay = parser.print(earlyDate.minusDays(1));
String nextDay = parser.print(lateDate.plusDays(1));
values.add(prevDay);
values.add(nextDay);
values.add(nextDay.substring(5, 7) + "-" + nextDay.substring(8, 10) + "-" + nextDay.substring(0, 4));
values.add(getRandomHoliday(earliest, latest));
if (!isNullable) {
values.add("");
}
return values;
}
/**
* Grab random holiday from the equivalence class that falls between the two dates
*
* @param earliest the earliest date parameter as defined in the model
* @param latest the latest date parameter as defined in the model
* @return a holiday that falls between the dates
*/
public String getRandomHoliday(String earliest, String latest) {
String dateString = "";
DateTimeFormatter parser = ISODateTimeFormat.date();
DateTime earlyDate = parser.parseDateTime(earliest);
DateTime lateDate = parser.parseDateTime(latest);
List<Holiday> holidays = new LinkedList<>();
int min = Integer.parseInt(earlyDate.toString().substring(0, 4));
int max = Integer.parseInt(lateDate.toString().substring(0, 4));
int range = max - min + 1;
int randomYear = (int) (Math.random() * range) + min;
for (Holiday s : EquivalenceClassTransformer.HOLIDAYS) {
holidays.add(s);
}
Collections.shuffle(holidays);
for (Holiday holiday : holidays) {
dateString = convertToReadableDate(holiday.forYear(randomYear));
if (toDate(dateString).after(toDate(earliest)) && toDate(dateString).before(toDate(latest))) {
break;
}
}
return dateString;
}
/**
* Check if a string has the ISO date format. Uses the ISODateTimeFormat.date() from JodaTime
* and returns a DateTime instance. The correct format is yyyy-MM-dd or yyyyMMdd
* @param arg the string to check
* @return a DateTime instance if the string is in the correct ISO format.
*/
public static DateTime isDate (final String arg) {
if (arg == null)
return null;
DateTime ret = null;
DateTimeFormatter df;
if (arg.contains("-")) {
df = ISODateTimeFormat.date();
} else {
df = ISODateTimeFormat.basicDate();
}
try {
ret = df.parseDateTime(arg);
} catch (IllegalArgumentException e) {
l.debug("{} is not a valid ISO date", arg);
}
return ret;
}
/**
* Create a new {@code DateTimeFormatter} using this factory.
* <p>If no specific pattern or style has been defined,
* the supplied {@code fallbackFormatter} will be used.
* @param fallbackFormatter the fall-back formatter to use
* when no specific factory properties have been set
* @return a new date time formatter
*/
public DateTimeFormatter createDateTimeFormatter(DateTimeFormatter fallbackFormatter) {
DateTimeFormatter dateTimeFormatter = null;
if (StringUtils.hasLength(this.pattern)) {
dateTimeFormatter = DateTimeFormat.forPattern(this.pattern);
}
else if (this.iso != null && this.iso != ISO.NONE) {
switch (this.iso) {
case DATE:
dateTimeFormatter = ISODateTimeFormat.date();
break;
case TIME:
dateTimeFormatter = ISODateTimeFormat.time();
break;
case DATE_TIME:
dateTimeFormatter = ISODateTimeFormat.dateTime();
break;
default:
throw new IllegalStateException("Unsupported ISO format: " + this.iso);
}
}
else if (StringUtils.hasLength(this.style)) {
dateTimeFormatter = DateTimeFormat.forStyle(this.style);
}
if (dateTimeFormatter != null && this.timeZone != null) {
dateTimeFormatter = dateTimeFormatter.withZone(DateTimeZone.forTimeZone(this.timeZone));
}
return (dateTimeFormatter != null ? dateTimeFormatter : fallbackFormatter);
}
/**
* Create a new {@code DateTimeFormatter} using this factory.
* <p>If no specific pattern or style has been defined,
* the supplied {@code fallbackFormatter} will be used.
* @param fallbackFormatter the fall-back formatter to use
* when no specific factory properties have been set
* @return a new date time formatter
*/
public DateTimeFormatter createDateTimeFormatter(DateTimeFormatter fallbackFormatter) {
DateTimeFormatter dateTimeFormatter = null;
if (StringUtils.hasLength(this.pattern)) {
dateTimeFormatter = DateTimeFormat.forPattern(this.pattern);
}
else if (this.iso != null && this.iso != ISO.NONE) {
switch (this.iso) {
case DATE:
dateTimeFormatter = ISODateTimeFormat.date();
break;
case TIME:
dateTimeFormatter = ISODateTimeFormat.time();
break;
case DATE_TIME:
dateTimeFormatter = ISODateTimeFormat.dateTime();
break;
default:
throw new IllegalStateException("Unsupported ISO format: " + this.iso);
}
}
else if (StringUtils.hasLength(this.style)) {
dateTimeFormatter = DateTimeFormat.forStyle(this.style);
}
if (dateTimeFormatter != null && this.timeZone != null) {
dateTimeFormatter = dateTimeFormatter.withZone(DateTimeZone.forTimeZone(this.timeZone));
}
return (dateTimeFormatter != null ? dateTimeFormatter : fallbackFormatter);
}
/**
* Create a new {@code DateTimeFormatter} using this factory.
* <p>If no specific pattern or style has been defined,
* the supplied {@code fallbackFormatter} will be used.
* @param fallbackFormatter the fall-back formatter to use when no specific
* factory properties have been set (can be {@code null}).
* @return a new date time formatter
*/
public DateTimeFormatter createDateTimeFormatter(DateTimeFormatter fallbackFormatter) {
DateTimeFormatter dateTimeFormatter = null;
if (StringUtils.hasLength(this.pattern)) {
dateTimeFormatter = DateTimeFormat.forPattern(this.pattern);
}
else if (this.iso != null && this.iso != ISO.NONE) {
switch (this.iso) {
case DATE:
dateTimeFormatter = ISODateTimeFormat.date();
break;
case TIME:
dateTimeFormatter = ISODateTimeFormat.time();
break;
case DATE_TIME:
dateTimeFormatter = ISODateTimeFormat.dateTime();
break;
case NONE:
/* no-op */
break;
default:
throw new IllegalStateException("Unsupported ISO format: " + this.iso);
}
}
else if (StringUtils.hasLength(this.style)) {
dateTimeFormatter = DateTimeFormat.forStyle(this.style);
}
if (dateTimeFormatter != null && this.timeZone != null) {
dateTimeFormatter = dateTimeFormatter.withZone(DateTimeZone.forTimeZone(this.timeZone));
}
return (dateTimeFormatter != null ? dateTimeFormatter : fallbackFormatter);
}
/**
* Create a new {@code DateTimeFormatter} using this factory.
* <p>If no specific pattern or style has been defined,
* the supplied {@code fallbackFormatter} will be used.
* @param fallbackFormatter the fall-back formatter to use when no specific
* factory properties have been set (can be {@code null}).
* @return a new date time formatter
*/
public DateTimeFormatter createDateTimeFormatter(DateTimeFormatter fallbackFormatter) {
DateTimeFormatter dateTimeFormatter = null;
if (StringUtils.hasLength(this.pattern)) {
dateTimeFormatter = DateTimeFormat.forPattern(this.pattern);
}
else if (this.iso != null && this.iso != ISO.NONE) {
switch (this.iso) {
case DATE:
dateTimeFormatter = ISODateTimeFormat.date();
break;
case TIME:
dateTimeFormatter = ISODateTimeFormat.time();
break;
case DATE_TIME:
dateTimeFormatter = ISODateTimeFormat.dateTime();
break;
case NONE:
/* no-op */
break;
default:
throw new IllegalStateException("Unsupported ISO format: " + this.iso);
}
}
else if (StringUtils.hasLength(this.style)) {
dateTimeFormatter = DateTimeFormat.forStyle(this.style);
}
if (dateTimeFormatter != null && this.timeZone != null) {
dateTimeFormatter = dateTimeFormatter.withZone(DateTimeZone.forTimeZone(this.timeZone));
}
return (dateTimeFormatter != null ? dateTimeFormatter : fallbackFormatter);
}
/**
* @param isNullable isNullable
* @param earliest lower boundary date
* @param latest upper boundary date
* @param onlyBusinessDays only business days
* @return a list of boundary dates
*/
public List<String> positiveCase(boolean isNullable, String earliest, String latest, boolean onlyBusinessDays) {
List<String> values = new LinkedList<>();
if (earliest.equalsIgnoreCase(latest)) {
values.add(earliest);
if (isNullable) {
values.add("");
}
return values;
}
DateTimeFormatter parser = ISODateTimeFormat.date();
DateTime earlyDate = parser.parseDateTime(earliest);
DateTime lateDate = parser.parseDateTime(latest);
String earlyDay = parser.print(earlyDate);
String nextDay = getNextDay(earlyDate.toString().substring(0, 10), onlyBusinessDays);
String prevDay = getPreviousDay(lateDate.toString().substring(0, 10), onlyBusinessDays);
String lateDay = parser.print(lateDate);
values.add(earlyDay);
values.add(nextDay);
values.add(prevDay);
values.add(lateDay);
if (isNullable) {
values.add("");
}
return values;
}
public ConvertDateToString(ScalarWriter baseWriter) {
super(baseWriter);
final String formatValue = baseWriter.schema().format();
dateTimeFormatter = formatValue == null
? ISODateTimeFormat.date() : DateTimeFormat.forPattern(formatValue);
}
public static final DateTimeFormatter getDateFormatter(boolean alternativeDateFormat) {
if ( !alternativeDateFormat )
return ISODateTimeFormat.dateTimeNoMillis().withZoneUTC();
else
return ISODateTimeFormat.date();
}
public LocalDateTypeAdapter() {
this(ISODateTimeFormat.date());
}