下面列出了怎么用org.joda.time.format.ISODateTimeFormat的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Gets a formatter suitable for the fields in this partial.
* <p>
* If there is no appropriate ISO format, null is returned.
* This method may return a formatter that does not display all the
* fields of the partial. This might occur when you have overlapping
* fields, such as dayOfWeek and dayOfMonth.
*
* @return a formatter suitable for the fields in this partial, null
* if none is suitable
*/
public DateTimeFormatter getFormatter() {
DateTimeFormatter[] f = iFormatter;
if (f == null) {
if (size() == 0) {
return null;
}
f = new DateTimeFormatter[2];
try {
List<DateTimeFieldType> list = new ArrayList<DateTimeFieldType>(Arrays.asList(iTypes));
f[0] = ISODateTimeFormat.forFields(list, true, false);
if (list.size() == 0) {
f[1] = f[0];
}
} catch (IllegalArgumentException ex) {
// ignore
}
iFormatter = f;
}
return f[0];
}
/**
* Gets a formatter suitable for the fields in this partial.
* <p>
* If there is no appropriate ISO format, null is returned.
* This method may return a formatter that does not display all the
* fields of the partial. This might occur when you have overlapping
* fields, such as dayOfWeek and dayOfMonth.
*
* @return a formatter suitable for the fields in this partial, null
* if none is suitable
*/
public DateTimeFormatter getFormatter() {
DateTimeFormatter[] f = iFormatter;
if (f == null) {
if (size() == 0) {
return null;
}
f = new DateTimeFormatter[2];
try {
List<DateTimeFieldType> list = new ArrayList<DateTimeFieldType>(Arrays.asList(iTypes));
f[0] = ISODateTimeFormat.forFields(list, true, false);
if (list.size() == 0) {
f[1] = f[0];
}
} catch (IllegalArgumentException ex) {
// ignore
}
iFormatter = f;
}
return f[0];
}
private static long toUnixTimeMicros(Object obj, Type type, String name)
{
if (Number.class.isAssignableFrom(obj.getClass())) {
return ((Number) obj).longValue();
}
else if (obj instanceof String) {
String s = (String) obj;
s = s.trim().replace(' ', 'T');
long millis = ISODateTimeFormat.dateOptionalTimeParser().withZone(DateTimeZone.UTC).parseMillis(s);
return millis * 1000;
}
else {
handleInvalidValue(name, type, obj);
return 0;
}
}
public String getDateTimeWithTimezoneConversion(Date dateToConvert) {
if (dateToConvert == null) {
return null;
}
DateTime dt = new DateTime(dateToConvert);
DateTimeFormatter fmt = ISODateTimeFormat.yearMonthDay();
DateTimeFormatter fmtTime = ISODateTimeFormat.hourMinuteSecond();
// If the client browser is in a different timezone than server, need to modify date
if (m_client_timezone !=null && m_server_timezone!=null && !m_client_timezone.hasSameRules(m_server_timezone)) {
DateTimeZone dateTimeZone = DateTimeZone.forTimeZone(m_client_timezone);
fmt = fmt.withZone(dateTimeZone);
fmtTime = fmtTime.withZone(dateTimeZone);
}
return dt.toString(fmt) + " " + dt.toString(fmtTime);
}
public static Event build(JsonObject channeldata, String name, Member from,
Member recipient, String channelId, String serviceUrl, String conversationId) {
Event event = new Event();
event.setType(Event.EVENT_TYPE);
event.setChanneldata(channeldata);
event.setName(name);
event.setFrom(from);
event.setTimestamp(
ISODateTimeFormat.dateHourMinuteSecondMillis().withZoneUTC().print(System.currentTimeMillis()));
event.setTimestamp(ISODateTimeFormat.dateHourMinuteSecondMillis().withZone(DateTimeZone.forOffsetHours(-3))
.print(System.currentTimeMillis()));
event.setRecipient(recipient);
event.setConversation(new Conversation(conversationId));
event.setServiceUrl(serviceUrl);
event.setChannelId(channelId);
setActivityProperties(event);
return event;
}
/**
* Gets a formatter suitable for the fields in this partial.
* <p>
* If there is no appropriate ISO format, null is returned.
* This method may return a formatter that does not display all the
* fields of the partial. This might occur when you have overlapping
* fields, such as dayOfWeek and dayOfMonth.
*
* @return a formatter suitable for the fields in this partial, null
* if none is suitable
*/
public DateTimeFormatter getFormatter() {
DateTimeFormatter[] f = iFormatter;
if (f == null) {
if (size() == 0) {
return null;
}
f = new DateTimeFormatter[2];
try {
List<DateTimeFieldType> list = new ArrayList<DateTimeFieldType>(Arrays.asList(iTypes));
f[0] = ISODateTimeFormat.forFields(list, true, false);
if (list.size() == 0) {
f[1] = f[0];
}
} catch (IllegalArgumentException ex) {
// ignore
}
iFormatter = f;
}
return f[0];
}
@Before
public void setUp() {
formatter = ISODateTimeFormat.dateTime();
processor1 = new FmtDateTime();
processor2 = new FmtDateTime(formatter);
processor3 = new FmtDateTime(DATE_TIME_FORMAT);
processor4 = new FmtDateTime(DATE_TIME_FORMAT, Locale.ENGLISH);
processorChain1 = new FmtDateTime(new IdentityTransform());
processorChain2 = new FmtDateTime(formatter, new IdentityTransform());
processorChain3 = new FmtDateTime(DATE_TIME_FORMAT,
new IdentityTransform());
processorChain4 = new FmtDateTime(DATE_TIME_FORMAT, Locale.ENGLISH,
new IdentityTransform());
processors = Arrays.asList(processor1, processor2, processor3,
processor4, processorChain1, processorChain2, processorChain3,
processorChain4);
}
/**
* Gets a formatter suitable for the fields in this partial.
* <p>
* If there is no appropriate ISO format, null is returned.
* This method may return a formatter that does not display all the
* fields of the partial. This might occur when you have overlapping
* fields, such as dayOfWeek and dayOfMonth.
*
* @return a formatter suitable for the fields in this partial, null
* if none is suitable
*/
public DateTimeFormatter getFormatter() {
DateTimeFormatter[] f = iFormatter;
if (f == null) {
if (size() == 0) {
return null;
}
f = new DateTimeFormatter[2];
try {
List<DateTimeFieldType> list = new ArrayList<DateTimeFieldType>(Arrays.asList(iTypes));
f[0] = ISODateTimeFormat.forFields(list, true, false);
if (list.size() == 0) {
f[1] = f[0];
}
} catch (IllegalArgumentException ex) {
// ignore
}
iFormatter = f;
}
return f[0];
}
@ProcessElement
public void processElement(ProcessContext c) throws Exception {
KV<Instant, Iterable<KV<K, V>>> kv = c.element();
// Create a writer on the sink and use it brutally to write all records to one file.
Sink.Writer<KV<K, V>, ?> writer = sink.createWriteOperation().createWriter(c.getPipelineOptions());
writer.open(UUID.randomUUID().toString());
for (KV<K, V> v : kv.getValue())
writer.write(v);
// Use the write result to move the file to the expected output name.
Object writeResult = writer.close();
if (writer instanceof ConfigurableHDFSFileSink.HDFSWriter) {
String attemptResultName = String.valueOf(writeResult);
String timeslice = ISODateTimeFormat.basicDateTime().print(kv.getKey().getMillis());
String resultName = "output-" + timeslice + "-" + timeslice + "-00001-of-00001";
((ConfigurableHDFSFileSink.HDFSWriter) writer).commitManually(attemptResultName, resultName);
}
}
Map<String, Object> getDocumentMap(TikaDocument document) throws IOException {
Map<String, Object> jsonDocument = new HashMap<>();
jsonDocument.put(esCfg.docTypeField, ES_DOCUMENT_TYPE);
jsonDocument.put(esCfg.indexJoinField, new HashMap<String, String>() {{
put("name", "Document");
}});
jsonDocument.put("path", document.getPath().toString());
jsonDocument.put("dirname", ofNullable(document.getPath().getParent()).orElse(get("")).toString());
jsonDocument.put("status", "INDEXED");
jsonDocument.put("nerTags", new HashSet<>());
jsonDocument.put("tags", new HashSet<>());
jsonDocument.put("extractionDate", ISODateTimeFormat.dateTime().print(new Date().getTime()));
jsonDocument.put("metadata", getMetadata(document));
jsonDocument.put("contentType", ofNullable(document.getMetadata().get(CONTENT_TYPE)).orElse(DEFAULT_VALUE_UNKNOWN).split(";")[0]);
jsonDocument.put("contentLength", valueOf(ofNullable(document.getMetadata().get(CONTENT_LENGTH)).orElse("-1")));
jsonDocument.put("contentEncoding", ofNullable(document.getMetadata().get(CONTENT_ENCODING)).orElse(DEFAULT_VALUE_UNKNOWN));
String content = toString(document.getReader()).trim();
jsonDocument.put("language", languageGuesser.guess(content));
jsonDocument.put(ES_CONTENT_FIELD, content);
return jsonDocument;
}
/**
* Gets a formatter suitable for the fields in this partial.
* <p>
* If there is no appropriate ISO format, null is returned.
* This method may return a formatter that does not display all the
* fields of the partial. This might occur when you have overlapping
* fields, such as dayOfWeek and dayOfMonth.
*
* @return a formatter suitable for the fields in this partial, null
* if none is suitable
*/
public DateTimeFormatter getFormatter() {
DateTimeFormatter[] f = iFormatter;
if (f == null) {
if (size() == 0) {
return null;
}
f = new DateTimeFormatter[2];
try {
List<DateTimeFieldType> list = new ArrayList<DateTimeFieldType>(Arrays.asList(iTypes));
f[0] = ISODateTimeFormat.forFields(list, true, false);
if (list.size() == 0) {
f[1] = f[0];
}
} catch (IllegalArgumentException ex) {
// ignore
}
iFormatter = f;
}
return f[0];
}
@Override
public void writeTimestamp(boolean isNull) throws IOException {
TimeStampMilliWriter ts = writer.timeStampMilli();
if(!isNull){
switch (parser.getCurrentToken()) {
case VALUE_NUMBER_INT:
LocalDateTime dt = new LocalDateTime(parser.getLongValue(), org.joda.time.DateTimeZone.UTC);
ts.writeTimeStampMilli(com.dremio.common.util.DateTimes.toMillis(dt));
break;
case VALUE_STRING:
DateTimeFormatter f = ISODateTimeFormat.dateTime();
ts.writeTimeStampMilli(com.dremio.common.util.DateTimes.toMillis(LocalDateTime.parse(parser.getValueAsString(), f)));
break;
default:
throw UserException.unsupportedError()
.message(parser.getCurrentToken().toString())
.build(LOG);
}
}
}
/**
* 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();
}
/**
* Check if a string has the ISO date time format. Uses the ISODateTimeFormat.dateTime() from JodaTime
* and returns a DateTime instance. The correct format is yyyy-MM-ddTHH:mm:ss.SSSZ
* @param arg the string to check
* @return a DateTime instance if the string is in the correct ISO format.
*/
public static DateTime isDateTime (final String arg) {
if (arg == null)
return null;
DateTimeFormatter f = ISODateTimeFormat.dateTime();
DateTime ret = null;
try {
ret = f.parseDateTime(arg);
} catch (IllegalArgumentException e) {
l.debug("{} is not a valid ISO DateTime", arg);
}
return ret;
}
@Override
public ObjectNode toJSONNode(TCAPIVersion version) {
ObjectNode node = super.toJSONNode(version);
DateTimeFormatter fmt = ISODateTimeFormat.dateTime().withZoneUTC();
if (this.id != null) {
node.put("id", this.getId().toString());
}
if (this.stored != null) {
node.put("stored", fmt.print(this.getStored()));
}
if (this.authority != null) {
node.put("authority", this.getAuthority().toJSONNode(version));
}
//Include 0.95 specific fields if asking for 0.95 version
if (TCAPIVersion.V095.equals(version)) {
if (this.getVoided() != null) {
node.put("voided", this.getVoided());
}
}
//Include 1.0.x specific fields if asking for 1.0.x version
if (version.ordinal() <= TCAPIVersion.V100.ordinal()) {
if (this.getVersion() != null) {
node.put("version", this.getVersion().toString());
}
}
return node;
}
@Before
public void setUp() {
formatter = ISODateTimeFormat.localTimeParser();
processor1 = new ParseLocalTime();
processor2 = new ParseLocalTime(formatter);
processorChain1 = new ParseLocalTime(new IdentityTransform());
processorChain2 = new ParseLocalTime(formatter, new IdentityTransform());
processors = Arrays.asList(processor1, processor2, processorChain1,
processorChain2);
}
public static String format(Object value) {
String formattedString = null;
if (value instanceof Date) {
Date date = (Date) value;
DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
DateTimeZone dateTimeZone = DateTimeZone.forTimeZone(CommandContextUtil.getProcessEngineConfiguration().getClock().getCurrentTimeZone());
formattedString = fmt.print(new DateTime(date, dateTimeZone));
} else {
formattedString = value.toString();
}
return formattedString;
}
public static SUnmarshaller getDateTimeSUnmarshaller() {
return new SUnmarshaller() {
private final DateTimeFormatter dateTimeFormatter = ISODateTimeFormat.dateTime();
@Override
public Object unmarshall(final AttributeValue value) throws ParseException {
return dateTimeFormatter.parseDateTime(value.getS());
}
};
}
/**
* 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);
}
@Test
@Ignore("https://issues.apache.org/jira/browse/CALCITE-2394")
public void testTimestampWithNonzeroTimezone() throws Exception {
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Asia/Tokyo"), Locale.ROOT);
TestTableProvider tableProvider = new TestTableProvider();
Connection connection = JdbcDriver.connect(tableProvider, PipelineOptionsFactory.create());
// A table with one TIMESTAMP column
Schema schema = Schema.builder().addDateTimeField("ts").build();
connection
.createStatement()
.executeUpdate("CREATE EXTERNAL TABLE test (ts TIMESTAMP) TYPE 'test'");
ReadableInstant july1 =
ISODateTimeFormat.dateTimeParser().parseDateTime("2018-07-01T01:02:03Z");
tableProvider.addRows("test", Row.withSchema(schema).addValue(july1).build());
ResultSet selectResult =
connection.createStatement().executeQuery(String.format("SELECT ts FROM test"));
selectResult.next();
Timestamp ts = selectResult.getTimestamp(1, cal);
assertThat(
String.format(
"Wrote %s to a table, but got back %s",
ISODateTimeFormat.basicDateTime().print(july1),
ISODateTimeFormat.basicDateTime().print(ts.getTime())),
ts.getTime(),
equalTo(july1.getMillis()));
}
@Test
public void fromMethodNameTwoPathVariables() {
DateTime now = DateTime.now();
UriComponents uriComponents = fromMethodName(
ControllerWithMethods.class, "methodWithTwoPathVariables", 1, now).build();
assertThat(uriComponents.getPath(), is("/something/1/foo/" + ISODateTimeFormat.date().print(now)));
}
@Override
public LocalDate deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException {
JsonToken t = jp.getCurrentToken();
if (t == JsonToken.VALUE_STRING) {
String str = jp.getText().trim();
return ISODateTimeFormat.dateTimeParser().parseDateTime(str).toLocalDate();
}
if (t == JsonToken.VALUE_NUMBER_INT) {
return new LocalDate(jp.getLongValue());
}
throw ctxt.mappingException(handledType());
}
public static SSUnmarshaller getDateTimeSSUnmarshaller() {
return new SSUnmarshaller() {
private final DateTimeFormatter dateTimeFormatter = ISODateTimeFormat.dateTime();
@Override
public Object unmarshall(final AttributeValue value) throws ParseException {
final Set<DateTime> argument = new HashSet<DateTime>();
for (final String s : value.getSS()) {
argument.add(dateTimeFormatter.parseDateTime(s));
}
return argument;
}
};
}
public DeviceServiceModel(final Device device, final TwinServiceModel twin, String iotHubHostName,
boolean isConnectedEdgeDevice) {
this(device.geteTag(),
device.getDeviceId(),
device.getCloudToDeviceMessageCount(),
device.getLastActivityTime() == null ? null : DateTime.parse(device.getLastActivityTime(), ISODateTimeFormat.dateTimeParser().withZoneUTC()),
isConnectedEdgeDevice || device.getConnectionState() == DeviceConnectionState.Connected,
device.getStatus() == DeviceStatus.Enabled,
device.getCapabilities() != null ? device.getCapabilities().isIotEdge() : twin.getIsEdgeDevice(),
device.getStatusUpdatedTime() == null ? null : DateTime.parse(device.getStatusUpdatedTime(), ISODateTimeFormat.dateTimeParser().withZoneUTC()),
twin,
new AuthenticationMechanismServiceModel(device),
iotHubHostName);
}
public static DateTime toDateTime(String datetimeString) {
try {
return ISODateTimeFormat.dateTimeParser().parseDateTime(datetimeString);
} catch (IllegalArgumentException e) {
return DateTime.parse(datetimeString, FALLBACK_TIMESTAMP_FORMAT);
}
}
@Override
public void serialize(DateTime value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
if (provider.isEnabled(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)) {
jgen.writeNumber(value.getMillis());
} else {
value = value.withZone(DateTimeZone.UTC);
jgen.writeString(value.toString(ISODateTimeFormat.dateTime()));
}
}
private static boolean testNoMillisFormat(String time) {
DateTimeFormatter formatNoMillis = ISODateTimeFormat.dateTimeNoMillis().withZoneUTC();
try {
formatNoMillis.parseDateTime(time);
return true;
} catch (IllegalArgumentException e) {
return false;
}
}
@Override
public String toNonNullValue(Instant value) {
String formatted = ISODateTimeFormat.dateTime().print(value);
if (formatted.endsWith(".000Z")) {
formatted = formatted.substring(0, formatted.length() - 5) + "Z";
}
return formatted;
}
@Override
public void write(JsonWriter out, Date date) throws IOException {
if (date == null) {
out.nullValue();
} else {
String value;
if (dateFormat != null) {
value = dateFormat.format(date);
} else {
value = ISODateTimeFormat.basicDateTime().print(date.getTime());
}
out.value(value);
}
}
/**
* Tests List and Date parameters in and out
* @throws Exception
*/
@Test
public void testProcess_ListDate() throws Exception {
// Set up the input
when(request.getInputStream()).thenReturn(
new TestServletInputStream(buildSoapMessage(null, listOpIn, null, null)));
when(request.getScheme()).thenReturn("http");
// Resolve the input command
soapCommandProcessor.process(command);
assertEquals(1, ev.getInvokedCount());
DateTimeFormatter xmlFormat = ISODateTimeFormat.dateTimeParser();
// Assert that we resolved the expected arguments
Object[] args = ev.getArgs();
assertNotNull(args);
assertEquals(1, args.length);
List<Date> listArg = (List<Date>)args[0];
assertEquals(2, listArg.size());
assertEquals(xmlFormat.parseDateTime("248556211-09-30T12:12:53.297+01:00").toDate(), listArg.get(0));
assertEquals(xmlFormat.parseDateTime("248556211-09-30T12:12:53.297Z").toDate(), listArg.get(1));
// Assert that the expected result is sent
assertNotNull(ev.getObserver());
List<Date> response = new ArrayList<Date>();
response.add(xmlFormat.parseDateTime("248556211-09-30T12:12:53.297+01:00").toDate());
response.add(xmlFormat.parseDateTime("248556211-09-30T12:12:53.297Z").toDate());
ev.getObserver().onResult(new ExecutionResult(response));
assertEquals(CommandStatus.Complete, command.getStatus());
assertSoapyEquals(buildSoapMessage(null, listOpOut, null, null), testOut.getOutput());
verify(logger).logAccess(eq(command), isA(ExecutionContext.class), anyLong(), anyLong(),
any(MediaType.class), any(MediaType.class), any(ResponseCode.class));
verifyTracerCalls(listOpKey);
}