use of java.time.Period in project OpenOLAT by OpenOLAT.
the class Formatter method formatDateRelative.
/**
* adds the given period in day/month/years to the baseLineDate and formats it in a short format, e.g. 05.12.2015 or 12/05/2015
*
* @param baseLineDate the date
* @return a String with the formatted date
*/
public String formatDateRelative(Date baseLineDate, int days, int months, int years) {
if (baseLineDate == null)
return null;
LocalDate date = LocalDateTime.ofInstant(baseLineDate.toInstant(), ZoneId.systemDefault()).toLocalDate();
Period period = Period.of(years, months, days);
LocalDate relativeDate = date.plus(period);
Date result = Date.from(relativeDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
return formatDate(result);
}
use of java.time.Period in project jdbi by jdbi.
the class TestPeriod method testHandlesNulls.
@Test
public void testHandlesNulls() {
handle.execute("insert into intervals(id, foo) values(?, ?)", 5, null);
final Period p = handle.createQuery("select foo from intervals where id=?").bind(0, 5).mapTo(Period.class).findOnly();
assertThat(p).isNull();
}
use of java.time.Period in project jdbi by jdbi.
the class TestPeriod method testWritesViaFluentApi.
@Test
public void testWritesViaFluentApi() {
handle.execute("insert into intervals(id, foo) values(?, ?)", 6, testPeriod);
final Period p = handle.createQuery("select foo from intervals where id=?").bind(0, 6).mapTo(Period.class).findOnly();
assertThat(p).isEqualTo(testPeriod);
}
use of java.time.Period in project jdbi by jdbi.
the class TestPeriod method testTrivialPeriod.
@Test
public void testTrivialPeriod() {
handle.execute("insert into intervals(id, foo) values(?, ?)", 4, Period.of(0, 0, 0));
Period p = handle.createQuery("select foo from intervals where id=?").bind(0, 4).mapTo(Period.class).findOnly();
assertThat(p.isZero());
}
use of java.time.Period in project fastjson by alibaba.
the class Jdk8DateCodec method deserialze.
@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName, String format, int feature) {
JSONLexer lexer = parser.lexer;
if (lexer.token() == JSONToken.NULL) {
lexer.nextToken();
return null;
}
if (lexer.token() == JSONToken.LITERAL_STRING) {
String text = lexer.stringVal();
lexer.nextToken();
DateTimeFormatter formatter = null;
if (format != null) {
if (defaultPatttern.equals(format)) {
formatter = defaultFormatter;
} else {
formatter = DateTimeFormatter.ofPattern(format);
}
}
if ("".equals(text)) {
return null;
}
if (type == LocalDateTime.class) {
LocalDateTime localDateTime;
if (text.length() == 10 || text.length() == 8) {
LocalDate localDate = parseLocalDate(text, format, formatter);
localDateTime = LocalDateTime.of(localDate, LocalTime.MIN);
} else {
localDateTime = parseDateTime(text, formatter);
}
return (T) localDateTime;
} else if (type == LocalDate.class) {
LocalDate localDate;
if (text.length() == 23) {
LocalDateTime localDateTime = LocalDateTime.parse(text);
localDate = LocalDate.of(localDateTime.getYear(), localDateTime.getMonthValue(), localDateTime.getDayOfMonth());
} else {
localDate = parseLocalDate(text, format, formatter);
}
return (T) localDate;
} else if (type == LocalTime.class) {
LocalTime localTime;
if (text.length() == 23) {
LocalDateTime localDateTime = LocalDateTime.parse(text);
localTime = LocalTime.of(localDateTime.getHour(), localDateTime.getMinute(), localDateTime.getSecond(), localDateTime.getNano());
} else {
boolean digit = true;
for (int i = 0; i < text.length(); ++i) {
char ch = text.charAt(i);
if (ch < '0' || ch > '9') {
digit = false;
break;
}
}
if (digit && text.length() > 8 && text.length() < 19) {
long epochMillis = Long.parseLong(text);
localTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(epochMillis), JSON.defaultTimeZone.toZoneId()).toLocalTime();
} else {
localTime = LocalTime.parse(text);
}
}
return (T) localTime;
} else if (type == ZonedDateTime.class) {
if (formatter == defaultFormatter) {
formatter = ISO_FIXED_FORMAT;
}
if (formatter == null) {
if (text.length() <= 19) {
JSONScanner s = new JSONScanner(text);
TimeZone timeZone = parser.lexer.getTimeZone();
s.setTimeZone(timeZone);
boolean match = s.scanISO8601DateIfMatch(false);
if (match) {
Date date = s.getCalendar().getTime();
return (T) ZonedDateTime.ofInstant(date.toInstant(), timeZone.toZoneId());
}
}
}
ZonedDateTime zonedDateTime = parseZonedDateTime(text, formatter);
return (T) zonedDateTime;
} else if (type == OffsetDateTime.class) {
OffsetDateTime offsetDateTime = OffsetDateTime.parse(text);
return (T) offsetDateTime;
} else if (type == OffsetTime.class) {
OffsetTime offsetTime = OffsetTime.parse(text);
return (T) offsetTime;
} else if (type == ZoneId.class) {
ZoneId offsetTime = ZoneId.of(text);
return (T) offsetTime;
} else if (type == Period.class) {
Period period = Period.parse(text);
return (T) period;
} else if (type == Duration.class) {
Duration duration = Duration.parse(text);
return (T) duration;
} else if (type == Instant.class) {
boolean digit = true;
for (int i = 0; i < text.length(); ++i) {
char ch = text.charAt(i);
if (ch < '0' || ch > '9') {
digit = false;
break;
}
}
if (digit && text.length() > 8 && text.length() < 19) {
long epochMillis = Long.parseLong(text);
return (T) Instant.ofEpochMilli(epochMillis);
}
Instant instant = Instant.parse(text);
return (T) instant;
}
} else if (lexer.token() == JSONToken.LITERAL_INT) {
long millis = lexer.longValue();
lexer.nextToken();
if ("unixtime".equals(format)) {
millis *= 1000;
} else if ("yyyyMMddHHmmss".equals(format)) {
int yyyy = (int) (millis / 10000000000L);
int MM = (int) ((millis / 100000000L) % 100);
int dd = (int) ((millis / 1000000L) % 100);
int HH = (int) ((millis / 10000L) % 100);
int mm = (int) ((millis / 100L) % 100);
int ss = (int) (millis % 100);
if (type == LocalDateTime.class) {
return (T) LocalDateTime.of(yyyy, MM, dd, HH, mm, ss);
}
}
if (type == LocalDateTime.class) {
return (T) LocalDateTime.ofInstant(Instant.ofEpochMilli(millis), JSON.defaultTimeZone.toZoneId());
}
if (type == LocalDate.class) {
return (T) LocalDateTime.ofInstant(Instant.ofEpochMilli(millis), JSON.defaultTimeZone.toZoneId()).toLocalDate();
}
if (type == LocalTime.class) {
return (T) LocalDateTime.ofInstant(Instant.ofEpochMilli(millis), JSON.defaultTimeZone.toZoneId()).toLocalTime();
}
if (type == ZonedDateTime.class) {
return (T) ZonedDateTime.ofInstant(Instant.ofEpochMilli(millis), JSON.defaultTimeZone.toZoneId());
}
if (type == Instant.class) {
return (T) Instant.ofEpochMilli(millis);
}
throw new UnsupportedOperationException();
} else if (lexer.token() == JSONToken.LBRACE) {
JSONObject object = parser.parseObject();
if (type == Instant.class) {
Object epochSecond = object.get("epochSecond");
Object nano = object.get("nano");
if (epochSecond instanceof Number && nano instanceof Number) {
return (T) Instant.ofEpochSecond(TypeUtils.longExtractValue((Number) epochSecond), TypeUtils.longExtractValue((Number) nano));
}
if (epochSecond instanceof Number) {
return (T) Instant.ofEpochSecond(TypeUtils.longExtractValue((Number) epochSecond));
}
}
} else {
throw new UnsupportedOperationException();
}
return null;
}
Aggregations