Example usage for java.time.format DateTimeFormatter ofPattern

List of usage examples for java.time.format DateTimeFormatter ofPattern

Introduction

In this page you can find the example usage for java.time.format DateTimeFormatter ofPattern.

Prototype

public static DateTimeFormatter ofPattern(String pattern) 

Source Link

Document

Creates a formatter using the specified pattern.

Usage

From source file:com.armeniopinto.time.ApplicationStarter.java

@RequestMapping("/time/{format}")
public String time(@PathVariable String format) {
    final String time;
    if ("iso".equals(format)) {
        time = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mmX").withZone(ZoneOffset.UTC)
                .format(Instant.now());
    } else if ("unix".equals(format)) {
        time = Long.toString(System.currentTimeMillis());
    } else if ("pretty".equals(format)) {
        time = new Date().toString();
    } else {//  ww  w  . j a v a 2 s  .  co  m
        throw new IllegalArgumentException(String.format("Unkonwn date format: %s", format));
    }
    return time;
}

From source file:datojava.jcalendar.DJJCalendar.java

public DJJCalendar() throws IOException, ClientProtocolException, JSONException {
    rutasLeer = new Leer();
    formato = new SimpleDateFormat("yyyy-MM-dd");
    fechasOcupadasArray = rutasLeer.leer("http://localhost/API_Citas/public/Diasocupados/diasOcupados");
    formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
}

From source file:org.jbb.frontend.impl.format.LocalDateTimeFormatter.java

public void setPattern(String pattern) {
    Validate.notBlank(pattern);//from  w  w  w  . j av a  2  s .c o  m
    DateTimeFormatter.ofPattern(pattern);
    frontendProperties.setProperty(FrontendProperties.LOCAL_DATE_TIME_FORMAT_KEY, pattern);
}

From source file:dhbw.clippinggorilla.external.mailserver.MailUtils.java

public static void sendClipping(Clipping clipping) {
    User user = UserUtils.getUser(clipping.getUserid());
    String email = user.getEmail();
    String html = CLIPPING_ROOT_HTML;
    String rows = "";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd. LLLL. yyyy");
    formatter.withZone(ZoneId.of("Europe/Berlin"));

    rows = clipping.getArticles().entrySet().stream().map((entry) -> {
        InterestProfile userprofile = entry.getKey();
        LinkedHashSet<Article> articleSet = entry.getValue();
        String profileRow = CLIPPING_GROUP_HTML.replace("[GROUPNAME]", userprofile.getName());
        String articleRows = articleSet.stream()
                .map((article) -> CLIPPING_ARTICLE_HTML
                        .replace("[ARTICLE_IMAGE_URL]",
                                article.getUrlToImage().isEmpty()
                                        ? article.getSourceAsSource().getLogo().toExternalForm()
                                        : article.getUrlToImage())
                        .replace("[ARTICLE_TITLE]", article.getTitle())
                        .replace("[ARTICLE_DESCRIPTION]",
                                article.getDescription().length() > 400
                                        ? article.getDescription().substring(0, 400) + "..."
                                        : article.getDescription())
                        .replace("[SOURCE_NAME]", article.getSourceAsSource().getName())
                        .replace("[ARTICLE_DATE]",
                                article.getPublishedAtAsLocalDateTime().toLocalDate().format(formatter))
                        .replace("[AUTHOR]", article.getAuthor()))
                .reduce("", String::concat);
        return profileRow + "\n" + articleRows;
    }).reduce(rows, String::concat);

    rows = clipping.getArticlesFromGroup().entrySet().stream().map((entry) -> {
        GroupInterestProfile groupprofile = entry.getKey();
        LinkedHashSet<Article> articleSet = entry.getValue();
        String profileRow = CLIPPING_GROUP_HTML.replace("[GROUPNAME]", groupprofile.getName());
        String articleRows = articleSet.stream()
                .map((article) -> CLIPPING_ARTICLE_HTML
                        .replace("[ARTICLE_IMAGE_URL]",
                                article.getUrlToImage().isEmpty()
                                        ? article.getSourceAsSource().getLogo().toExternalForm()
                                        : article.getUrlToImage())
                        .replace("[ARTICLE_TITLE]", article.getTitle())
                        .replace("[ARTICLE_DESCRIPTION]",
                                article.getDescription().length() > 400
                                        ? article.getDescription().substring(0, 400) + "..."
                                        : article.getDescription())
                        .replace("[SOURCE_NAME]", article.getSourceAsSource().getName())
                        .replace("[ARTICLE_DATE]",
                                article.getPublishedAtAsLocalDateTime().toLocalDate().format(formatter))
                        .replace("[AUTHOR]", article.getAuthor()))
                .reduce("", String::concat);
        return profileRow + "\n" + articleRows;
    }).reduce(rows, String::concat);

    String clippingText = Language.get(Word.CLIPPING_TEXT).replace("[FIRSTNAME]", user.getFirstName())
            .replace("[LASTNAME]", user.getLastName()).replace("[DATE]", clipping.getDate().format(formatter));
    html = html.replace("[CLIPPING_TEXT]", clippingText).replace("[GROUPS]", rows);
    //        try {
    //            Files.write(Paths.get(System.getProperty("user.home"), "Desktop", "email.html"), html.getBytes("UTF-8"));
    //        } catch (IOException ex) {
    //            Logger.getLogger(MailUtils.class.getName()).log(Level.SEVERE, null, ex);
    //        }/* w  w w .  ja  v a 2  s . co m*/
    try {
        Mail.send(email, "ClippingGorilla Clippings", "Your email client does not support HTML messages", html);
    } catch (EmailException ex) {
        Log.error("Could not send Clipping to user " + user.getFirstName() + " " + user.getLastName() + " ("
                + user.getUsername() + ")", ex);
    }
}

From source file:ch.jamiete.hilda.moderatortools.commands.UserinfoCommand.java

public UserinfoCommand(final Hilda hilda) {
    super(hilda);

    this.dtf = DateTimeFormatter.ofPattern("EEEE d MMMM yyyy hh:mm a");

    this.setName("userinfo");
    this.setDescription("Displays information about the user.");
}

From source file:org.sakaiproject.component.app.scheduler.jobs.cm.processor.sis.AbstractCMProcessor.java

public Date getDate(String str) {
    if (StringUtils.isBlank(str)) {
        return null;
    }//from w w  w . j  a  v a 2  s  .c  om
    DateTimeFormatter df = DateTimeFormatter.ofPattern(dateFormat);
    try {
        return Date.from(LocalDate.parse(str, df).atStartOfDay(ZoneId.systemDefault()).toInstant());
    } catch (DateTimeParseException dtpe) {
        throw new RuntimeException("Cannot parse the date from: " + str, dtpe);
    }
}

From source file:org.mascherl.example.page.data.SignUpStep1Bean.java

@Past
@NotNull/*w w  w. j a  v a2s.  c o  m*/
@JsonIgnore
public LocalDate getDateOfBirthParsed() {
    try {
        return LocalDate.parse(dateOfBirth, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    } catch (RuntimeException e) {
        return null;
    }
}

From source file:br.gov.sp.policiamilitar.bopmtc.conversion.DateFormatter.java

private DateTimeFormatter createDateFormat() {
    final String format = "dd/MM/yyyy HH:mm";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
    return formatter;
}

From source file:ch.jamiete.hilda.moderatortools.commands.ServerinfoCommand.java

public ServerinfoCommand(final Hilda hilda) {
    super(hilda);

    this.dtf = DateTimeFormatter.ofPattern("EEEE d MMMM yyyy hh:mm a");

    this.setName("serverinfo");
    this.setDescription("Displays information about the server.");
}

From source file:h2backup.BackupFileService.java

public Path backupDatabase(BackupMethod method, String directory, String filePrefix,
        String dateTimeFormatPattern) {
    try {/*from   www .  j  ava  2  s.com*/
        String fileNameWithoutExt = filePrefix + "_"
                + DateTimeFormatter.ofPattern(dateTimeFormatPattern).format(LocalDateTime.now(clock)) + "_"
                + method.getFileSuffix();
        final Path path = Paths.get(directory, fileNameWithoutExt + ZIP_EXTENSION);
        final Path tmpPath = Paths.get(directory, fileNameWithoutExt + TMP_SUFFIX + ZIP_EXTENSION);

        log.info("Starting backup to file {} using {} method", tmpPath, method);
        method.getStrategy().backupDatabase(dataSource, tmpPath.toString());

        if (Files.exists(path) && FileUtils.contentEquals(tmpPath.toFile(), path.toFile())) {
            log.info("Deleting backup {} because it is identical to previous backup {}", tmpPath, path);
            Files.delete(tmpPath);
            return null;
        }

        int maxIndex = 0;
        do {
            maxIndex++;
        } while (Files.exists(Paths.get(directory, fileNameWithoutExt + "_" + maxIndex + ZIP_EXTENSION)));

        for (int index = maxIndex; index >= 1; index--) {
            Path oldPath = Paths.get(directory,
                    fileNameWithoutExt + (index > 1 ? "_" + (index - 1) : "") + ZIP_EXTENSION);
            Path newPath = Paths.get(directory, fileNameWithoutExt + "_" + index + ZIP_EXTENSION);
            if (Files.exists(oldPath)) {
                log.debug("Moving file {} to {}", oldPath, newPath);
                Files.move(oldPath, newPath);
            }
        }

        Files.move(tmpPath, path);

        return path;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}