Java Examples for java.time.format.DateTimeFormatter.ofPattern
The following java examples will help you to understand the usage of java.time.format.DateTimeFormatter.ofPattern. These source code samples are taken from different open source projects.
Example 1
| Project: levelup-java-examples-master File: DateMinusMinutes.java View source code |
@Test
public void subtract_minutes_from_date_in_java8() {
LocalDateTime newYearsDay = LocalDateTime.of(2013, Month.JANUARY, 1, 0, 0);
LocalDateTime newYearsEve = newYearsDay.minusMinutes(1);
java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss S");
logger.info(newYearsDay.format(formatter));
logger.info(newYearsEve.format(formatter));
assertTrue(newYearsEve.isBefore(newYearsDay));
}Example 2
| Project: edison-microservice-master File: JobRepresentation.java View source code |
private String formatRuntime(OffsetDateTime started, OffsetDateTime stopped) {
Duration duration = Duration.between(started, stopped);
if (duration.toHours() >= 24) {
return "> 24h";
}
LocalTime dateTime = LocalTime.ofSecondOfDay(duration.getSeconds());
return humanReadable ? ofPattern("HH:mm:ss").format(dateTime) : ofPattern("HH:mm:ss").format(dateTime);
}Example 3
| Project: fullstop-master File: FetchAmiJob.java View source code |
private Optional<LocalDate> getExpirationDate(final Image image) {
// current implementation parse creation date from name + add 60 days support period
return Optional.ofNullable(image.getName()).filter( name -> !name.isEmpty()).map(TAUPAGE_NAME_SPLITTER::splitToList).filter(// "Taupage-AMI-20160201-123456"
list -> list.size() == 4).map( parts -> parts.get(2)).map( timestamp -> LocalDate.parse(timestamp, ofPattern("yyyyMMdd"))).map( creationDate -> creationDate.plusDays(60));
}Example 4
| Project: esper-master File: TestCastExpr.java View source code |
private void runAssertionDynamicDateFormat() throws Exception {
// try legacy date types
String epl = "select " + "cast(a,date,dateformat:b) as c0," + "cast(a,long,dateformat:b) as c1," + "cast(a,calendar,dateformat:b) as c2" + " from SupportBean_StringAlphabetic";
EPStatement stmt = epService.getEPAdministrator().createEPL(epl);
stmt.addListener(listener);
runAssertionDynamicDateFormat("20100502", "yyyyMMdd");
runAssertionDynamicDateFormat("20100502101112", "yyyyMMddhhmmss");
runAssertionDynamicDateFormat(null, "yyyyMMdd");
// invalid date
try {
epService.getEPRuntime().sendEvent(new SupportBean_StringAlphabetic("x", "yyyyMMddhhmmss"));
} catch (EPException ex) {
SupportMessageAssertUtil.assertMessageContains(ex, "Exception parsing date 'x' format 'yyyyMMddhhmmss': Unparseable date: \"x\"");
}
// invalid format
try {
epService.getEPRuntime().sendEvent(new SupportBean_StringAlphabetic("20100502", "UUHHYY"));
} catch (EPException ex) {
SupportMessageAssertUtil.assertMessageContains(ex, "Illegal pattern character 'U'");
}
stmt.destroy();
// try java 8 types
epService.getEPAdministrator().createEPL("create schema ValuesAndFormats(" + "ldt string, ldtf string," + "ld string, ldf string," + "lt string, ltf string," + "zdt string, zdtf string)");
String eplExtended = "select " + "cast(ldt,localdatetime,dateformat:ldtf) as c0," + "cast(ld,localdate,dateformat:ldf) as c1," + "cast(lt,localtime,dateformat:ltf) as c2," + "cast(zdt,zoneddatetime,dateformat:zdtf) as c3 " + " from ValuesAndFormats";
Map<String, Object> event = new HashMap<>();
event.put("ldtf", "yyyyMMddHHmmss");
event.put("ldt", "19990102030405");
event.put("ldf", "yyyyMMdd");
event.put("ld", "19990102");
event.put("ltf", "HHmmss");
event.put("lt", "030405");
event.put("zdtf", "yyyyMMddHHmmssVV");
event.put("zdt", "20100510141516America/Los_Angeles");
EPStatement stmtExtended = epService.getEPAdministrator().createEPL(eplExtended);
stmtExtended.addListener(listener);
epService.getEPRuntime().sendEvent(event, "ValuesAndFormats");
EPAssertionUtil.assertProps(listener.assertOneGetNewAndReset(), "c0,c1,c2,c3".split(","), new Object[] { LocalDateTime.parse("19990102030405", DateTimeFormatter.ofPattern("yyyyMMddHHmmss")), LocalDate.parse("19990102", DateTimeFormatter.ofPattern("yyyyMMdd")), LocalTime.parse("030405", DateTimeFormatter.ofPattern("HHmmss")), ZonedDateTime.parse("20100510141516America/Los_Angeles", DateTimeFormatter.ofPattern("yyyyMMddHHmmssVV")) });
stmtExtended.destroy();
;
}