下面列出了java.time.LocalDate#format ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void getDiskUtilizationTest() throws Exception {
LocalDate currDate = LocalDate.now();
Map<String,Object> utilisation;
List< Map<String,Object>> utlisationList = new ArrayList<>();
for(int i=0;i<7;i++){
LocalDate temp = currDate.minusDays(i);
String date = temp.format(DateTimeFormatter.ISO_DATE);
utilisation = new HashMap<>();
utilisation.put("date",date);
utilisation.put("diskReadinBytes",Math.random() * 1000 + 26);
utilisation.put("diskWriteinBytes",Math.random() * 5000 + 12);
utlisationList.add(utilisation);
}
when(statsService.getDiskUtilization(anyString())).thenReturn(utlisationList);
ResponseEntity<Object> response = statisticsController.getDiskUtilization("aws-all");
assert(response.getStatusCodeValue()==200);
}
/**
* Gets the disk utilization test.
*
* @return the disk utilization test
* @throws Exception the exception
*/
@Test
public void getDiskUtilizationTest() throws Exception {
LocalDate currDate = LocalDate.now();
Map<String,Object> utilisation;
List< Map<String,Object>> utlisationList = new ArrayList<>();
for(int i=0;i<7;i++){
LocalDate temp = currDate.minusDays(i);
String date = temp.format(DateTimeFormatter.ISO_DATE);
utilisation = new HashMap<>();
utilisation.put("date",date);
utilisation.put("diskReadinBytes",Math.random() * 1000 + 26);
utilisation.put("diskWriteinBytes",Math.random() * 5000 + 12);
utlisationList.add(utilisation);
}
System.out.println(utlisationList);
when(elasticSearchRepository.getUtilizationByAssetGroup(anyString())).thenReturn(utlisationList);
List<Map<String, Object>> _utlisationList = statisticsService.getDiskUtilization(anyString());
System.out.println(_utlisationList);
assert(_utlisationList.size()==utlisationList.size());
}
/**
* Get a formatted String version of the date.
*
* @param date
* @return
*/
public static String defaultDateFormat(LocalDate date){
if (date != null) {
return date.format(dateFormat);
}
return "?";
}
@Test
public void formatAndParse() throws ParseException {
LocalDate someDate = LocalDate.of(2016, 12, 7);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String formattedDate = someDate.format(formatter);
LocalDate parsedDate = LocalDate.parse(formattedDate, formatter);
assertThat(formattedDate).isEqualTo("2016-12-07");
assertThat(parsedDate).isEqualTo(someDate);
}
private String generateProductIncrementName(int months) {
final String pattern = "%1s-%2s";
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
final LocalDate now = LocalDate.now();
final LocalDate changeMonthDate = now.plusMonths(months);
final String formattedChangeMonthDate = changeMonthDate.format(formatter);
final String formattedNow = now.format(formatter);
return months < 0
? String.format(pattern, formattedChangeMonthDate, formattedNow)
: String.format(pattern, formattedNow, formattedChangeMonthDate);
}
@Override
public String toString(Object o, String formatString, Locale locale) {
if (formatString == null) return o.toString();
LocalDate ld = (LocalDate) o;
return ld.format(DateTimeFormatter.ofPattern(formatString));
}
@Override
public String getMailingStatisticUrl(ComAdmin admin, String sessionId, MailingStatisticDto mailingStatistic) throws Exception {
final String language = StringUtils.defaultIfEmpty(admin.getAdminLang(), "EN");
final String reportName = getReportName(mailingStatistic);
final Map<String, Object> params = new HashMap<>();
params.put(REPORT_NAME, reportName);
params.put(IS_SVG, true);
params.put(FORMAT, "html");
params.put(COMPANY_ID, admin.getCompanyID());
params.put(LANGUAGE, language);
params.put(SECURITY_TOKEN, BirtInterceptingFilter.createSecurityToken(configService, admin.getCompanyID()));
params.put(EMM_SESSION, sessionId);
params.put(TARGET_BASE_URL.toLowerCase(), generateTargetBaseUrl());
if (DateMode.NONE != mailingStatistic.getDateMode()) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(new SimpleDateFormat(BIRTDataSet.DATE_PARAMETER_FORMAT_WITH_HOUR2).toPattern());
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern(new SimpleDateFormat(DateUtilities.YYYY_MM_DD).toPattern());
final LocalDateTime startDate = Objects.requireNonNull(mailingStatistic.getStartDate());
final LocalDateTime endDate = Objects.requireNonNull(mailingStatistic.getEndDate());
String startDateStr;
String endDateStr;
if (mailingStatistic.getDateMode() == DateMode.LAST_TENHOURS) {
startDateStr = startDate.format(dateTimeFormatter);
endDateStr = endDate.format(dateTimeFormatter);
} else {
LocalDate start = startDate.toLocalDate();
LocalDate end = endDate.toLocalDate();
if (mailingStatistic.getDateMode() == DateMode.SELECT_DAY) {
startDateStr = startDate.withHour(0).format(dateTimeFormatter);
endDateStr = endDate.withHour(23).format(dateTimeFormatter);
} else {
startDateStr = start.format(dateFormatter);
endDateStr = end.format(dateFormatter);
}
}
params.put(RECIPIENT_START_DATE, startDateStr);
params.put(RECIPIENT_STOP_DATE, endDateStr);
params.put(HOUR_SCALE, mailingStatistic.isHourScale());
}
collectParametersAccordingToType(mailingStatistic.getType(), admin, mailingStatistic, params);
return generateUrlWithParamsForExternalAccess(params);
}
@Override
public String serialize(LocalDate sourceValue) {
return sourceValue.format(ISO_FORMAT);
}
@Test
public void testSample() throws Exception {
assertThat(instanceId).isNotNull();
assertThat(databaseId).isNotNull();
String out = runSample("create");
assertThat(out).contains("Created database");
assertThat(out).contains(dbId.getName());
out = runSample("insert", "players");
assertThat(out).contains("Done inserting player records");
out = runSample("insert", "scores");
assertThat(out).contains("Done inserting score records");
out = runSample("insert", "scores");
assertThat(out).contains("Done inserting score records");
out = runSample("insert", "scores");
assertThat(out).contains("Done inserting score records");
out = runSample("insert", "scores");
assertThat(out).contains("Done inserting score records");
out = runSample("insert", "scores");
assertThat(out).contains("Done inserting score records");
// Query Top Ten Players of all time.
out = runSample("query");
assertThat(out).contains("PlayerId: ");
// Confirm output includes valid timestamps.
String columnText = "Timestamp: ";
String[] lines = out.split("\\r?\\n");
String valueToTest = lines[0].substring(lines[0].indexOf(columnText) + columnText.length());
DateTimeFormatter formatPattern = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate ld = LocalDate.parse(valueToTest, formatPattern);
String result = ld.format(formatPattern);
assertThat(result.equals(valueToTest)).isTrue();
// Test that Top Ten Players of the Year (within past 8760 hours) runs successfully.
out = runSample("query", "8760");
assertThat(out).contains("PlayerId: ");
// Test that Top Ten Players of the Month (within past 730 hours) runs successfully.
out = runSample("query", "730");
assertThat(out).contains("PlayerId: ");
// Test that Top Ten Players of the Week (within past 168 hours) runs successfully.
out = runSample("query", "168");
assertThat(out).contains("PlayerId: ");
}
@Override
public String toString(LocalDate value) {
return value.format(DateValueConstraintEditor.LOCAL_DATE_FORMAT);
}
@Override
String entityAttributeToString(LocalDate attr) {
if (attr == null)
return null;
return attr.format(ISO_DATE);
}
private static String processLocalDate(LocalDate localDate) {
return localDate.format(DateTimeFormatter.ISO_DATE);
}
String indexName(String name, LocalDate now) {
return name + "-" + now.format(indexDateFormatter);
}
public static String getString(LocalDate value) {
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE;
return value.format(formatter);
}
@Override
public String shortLocalizedDate(LocalDate date, Locale locale) {
DateTimeFormatter df = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM).withLocale(locale);
return date.format(df);
}
/**
* YYYYMMdd を返します。
* @return
*/
public String createYYYYMMdd() {
LocalDate localDate = LocalDate.now();
return localDate.format(dateFormatter);
}
@Test
public void format_date_in_java8 () {
java.time.format.DateTimeFormatter formatter =
java.time.format.DateTimeFormatter.ofPattern("MM/dd/yyyy");
LocalDate localDate = LocalDate.of(1967, Month.JANUARY, 15);
String dateFirstSuperBowlWasPlayed = localDate.format(formatter);
assertEquals("01/15/1967", dateFirstSuperBowlWasPlayed);
}
/**
* {@inheritDoc }
*
* @param context
* @param component
* @param value
* @return
*/
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
final LocalDate date = (LocalDate) value;
return date.format(DateTimeFormatter.ofPattern("dd/MM/yyyy"));
}
/**
* Formats this date in the provided, localized {@link java.time.format.FormatStyle}.
*
* @param self a LocalDate
* @param dateStyle the FormatStyle
* @return a formatted String
* @see java.time.format.DateTimeFormatter
* @since 2.5.0
*/
public static String format(final LocalDate self, FormatStyle dateStyle) {
return self.format(DateTimeFormatter.ofLocalizedDate(dateStyle));
}
/**
*
* @param localDate to convert
* @return a String in form of "yyyy-MM-dd"
*/
public static String toTdaYMD(LocalDate localDate) {
return localDate.format(TMD);
}