Java DateTimeFormatter getTimestampedPath(Path baseFile, TemporalAccessor time, DateTimeFormatter formatter)

Here you can find the source of getTimestampedPath(Path baseFile, TemporalAccessor time, DateTimeFormatter formatter)

Description

Creates a timestamped path from the given base file.

License

Open Source License

Parameter

Parameter Description
baseFile the base name.
time the timestamp to format.
formatter the formatter for the timestamp.

Exception

Parameter Description

Return

a new path which has a timestamp in its filename.

Declaration

public static Path getTimestampedPath(Path baseFile, TemporalAccessor time, DateTimeFormatter formatter) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.nio.file.Path;

import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;

public class Main {
    /**/*w  w  w .  j  a v  a2s .co m*/
     * Creates a timestamped path from the given base file. Example: "/path/basefile.ext" becomes
     * "/path/basefile.2017-01-23.14-34-20.ext" if the formatter is of pattern "yyyy-MM-dd.HH-mm-ss".
     *
     * @param baseFile  the base name.
     * @param time      the timestamp to format.
     * @param formatter the formatter for the timestamp.
     * @return a new path which has a timestamp in its filename.
     * @throws java.time.DateTimeException if the time cannot be formatted.
     */
    public static Path getTimestampedPath(Path baseFile, TemporalAccessor time, DateTimeFormatter formatter) {
        return getTimestampedPath(baseFile, formatter.format(time));
    }

    /**
     * Creates a timestamped path from the given base file. Example: "/path/basefile.ext" becomes
     * "/path/basefile.2017-01-23.14-34-20.ext" if the given timestamp equals to "2017-01-23.14-34-20".
     *
     * @param baseFile           the base name.
     * @param formattedTimestamp the pre-formatted timestamp. A leading dot will be added automatically.
     * @return a new path which has a timestamp in its filename.
     * @throws java.nio.file.InvalidPathException if the path cannot be constructed from the given string.
     */
    public static Path getTimestampedPath(Path baseFile, String formattedTimestamp) {
        String ext = getFileExtension(baseFile);
        String timestamp = ".".concat(formattedTimestamp);
        return baseFile.resolveSibling(getFileNameWithoutExtension(baseFile).concat(timestamp).concat(ext));
    }

    /**
     * Gets the file extension of the given path. The extension begins at the last occurrence of ".". Returns an empty
     * string if the file does not contain a dot (".") or begins with one (hidden file in unix).
     *
     * @param path the path.
     * @return the extension, with leading dot (".").
     * @throws NullPointerException if path is null or has zero elements.
     * @see Path#getFileName()
     */
    public static String getFileExtension(Path path) {
        String ext = path.getFileName().toString();
        int index = ext.lastIndexOf('.');
        if (index <= 0) {
            return "";
        } else {
            return ext.substring(index, ext.length());
        }
    }

    /**
     * Gets the file name of the given path without the extension. The extension begins at the last occurrence of ".".
     * Returns the base name if there is no extension or the file begins with a dot (".").
     *
     * @param path the path.
     * @return the base file name (without directory path and leading ".").
     * @throws NullPointerException if the path has zero elements or is null.
     * @see Path#getFileName()
     */
    public static String getFileNameWithoutExtension(Path path) {
        String name = path.getFileName().toString();
        int index = name.lastIndexOf('.');
        if (index <= 0) {
            return name;
        } else {
            return name.substring(0, index);
        }
    }
}

Related

  1. getStartOfDay(Date dateToFormat)
  2. getTime(final String time, final DateTimeFormatter formatter)
  3. getTime(String format)
  4. getTimeFromEpochMilli(String value, boolean shortFormat, String errorText)
  5. getTimeStamp(final String dateTimeFormatPattern)
  6. parse(DateTimeFormatter formatter, String string)
  7. parse(final DateTimeFormatter formatter, final String value, final ZoneId zoneId)
  8. parse(String _date, String _format)
  9. parse(String format, String datetime)