Java Code Examples for java.time.LocalDate#format()

The following examples show how to use java.time.LocalDate#format() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StatisticsServiceTest.java    From pacbot with Apache License 2.0 6 votes vote down vote up
/**
 * 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());
}
 
Example 2
Source File: StatisticsControllerTest.java    From pacbot with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example 3
Source File: CoreUtil.java    From elexis-3-core with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * 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 "?";
}
 
Example 4
Source File: ProgramIncrementServiceTest.java    From mirrorgate with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 5
Source File: JavaUtilTimeUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@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);
}
 
Example 6
Source File: TemplateMaker.java    From axelor-open-suite with GNU Affero General Public License v3.0 4 votes vote down vote up
@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));
}
 
Example 7
Source File: IdGenerator.java    From FX-AlgorithmTrading with MIT License 4 votes vote down vote up
/**
 * YYYYMMdd を返します。
 * @return
 */
public String createYYYYMMdd() {
    LocalDate localDate = LocalDate.now();
    return localDate.format(dateFormatter);
}
 
Example 8
Source File: UserTimeServiceImpl.java    From sakai with Educational Community License v2.0 4 votes vote down vote up
@Override
public String shortLocalizedDate(LocalDate date, Locale locale) {
    DateTimeFormatter df = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM).withLocale(locale);
    return date.format(df);
}
 
Example 9
Source File: TypeConverter.java    From sailfish-core with Apache License 2.0 4 votes vote down vote up
public static String getString(LocalDate value) {
    DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE;
    return value.format(formatter);
}
 
Example 10
Source File: IndexService.java    From core-ng-project with Apache License 2.0 4 votes vote down vote up
String indexName(String name, LocalDate now) {
    return name + "-" + now.format(indexDateFormatter);
}
 
Example 11
Source File: PgResultTransformer.java    From vertx-postgresql-starter with MIT License 4 votes vote down vote up
private static String processLocalDate(LocalDate localDate) {
  return localDate.format(DateTimeFormatter.ISO_DATE);
}
 
Example 12
Source File: LocalDateConverter.java    From blog with Apache License 2.0 4 votes vote down vote up
@Override
String entityAttributeToString(LocalDate attr) {
    if (attr == null)
        return null;
    return attr.format(ISO_DATE);
}
 
Example 13
Source File: DateColumnConstraintProvider.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Override
public String toString(LocalDate value) {
	return value.format(DateValueConstraintEditor.LOCAL_DATE_FORMAT);
}
 
Example 14
Source File: AppTest.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
@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: ");
}
 
Example 15
Source File: LocalDateTypeAdapter.java    From javers with Apache License 2.0 4 votes vote down vote up
@Override
public String serialize(LocalDate sourceValue) {
    return sourceValue.format(ISO_FORMAT);
}
 
Example 16
Source File: BirtStatisticsServiceImpl.java    From openemm with GNU Affero General Public License v3.0 4 votes vote down vote up
@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);
}
 
Example 17
Source File: FormatDate.java    From levelup-java-examples with Apache License 2.0 3 votes vote down vote up
@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);
}
 
Example 18
Source File: LocalDateConverter.java    From library with Apache License 2.0 2 votes vote down vote up
/**
 * {@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"));
}
 
Example 19
Source File: DateTimeExtensions.java    From groovy with Apache License 2.0 2 votes vote down vote up
/**
 * 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));
}
 
Example 20
Source File: Utils.java    From td-ameritrade-client with Apache License 2.0 2 votes vote down vote up
/**
 *
 * @param localDate to convert
 * @return a String in form of "yyyy-MM-dd"
 */
public static String toTdaYMD(LocalDate localDate) {
  return localDate.format(TMD);
}