Java String to Timestamp getFileNameWithTimeStampInterposed(final String fileName)

Here you can find the source of getFileNameWithTimeStampInterposed(final String fileName)

Description

Interpose a time stamp between the file name and extension

License

BSD License

Parameter

Parameter Description
fileName <code>String</code> with the file name to parse out

Return

String with the updated file name containing a time stamp

Declaration

public static String getFileNameWithTimeStampInterposed(final String fileName) 

Method Source Code

//package com.java2s;
//License from project: BSD License 

import java.text.DateFormat;
import java.util.Date;

public class Main {
    /**/*from  ww w .j  a v a2s.  com*/
     * Interpose a time stamp between the file name and extension
     *
     * @param fileName
     *            <code>String</code> with the file name to parse out
     * @return <code>String</code> with the updated file name containing a time
     *         stamp
     */
    public static String getFileNameWithTimeStampInterposed(final String fileName) {
        String namePart = getFileNameUpToExtension(fileName);
        String extension = getFileExtension(fileName);
        StringBuilder newName = new StringBuilder(namePart);
        newName.append(".[backup from - ");
        DateFormat dateFormat = DateFormat.getDateTimeInstance();
        newName.append(dateFormat.format(new Date()));
        newName.append("]");
        newName.append(extension);
        return newName.toString();
    }

    /**
     * Parse a file name to get the stuff before last '.' character to treat as
     * the file name
     *
     * @param fileName
     *            <code>String</code> with the file name to parse out.
     * @return <code>String</code> with the file name before the extension,
     *         without the '.'
     */
    public static String getFileNameUpToExtension(final String fileName) {
        if (fileName == null || fileName.isEmpty()) {
            throw new IllegalArgumentException("null fileName");
        }

        int lastDot = fileName.lastIndexOf('.');
        if (lastDot == -1) {
            return "";
        } else {
            return (fileName.substring(0, lastDot));
        }

    }

    /**
     * Parse a file name to get the stuff after the last '.' character to treat
     * as the file extension
     *
     * @param fileName
     *            <code>String</code> with the file name to parse out.
     * @return <code>String</code> with the file extension
     */
    public static String getFileExtension(final String fileName) {
        if (fileName == null || fileName.isEmpty()) {
            throw new IllegalArgumentException("null fileName");
        }

        int lastDot = fileName.lastIndexOf('.');
        if (lastDot == -1) {
            return "";
        } else {
            return (fileName.substring(lastDot));
        }

    }
}

Related

  1. calculateClientRequestId(String timestamp)
  2. getStringToTimestamp(String string)
  3. getTimeStamp(String date)
  4. getTimeStamp(String dateTime)
  5. getTimestamp(String dateValue, String pattern, TimeZone timezone)