Here you can find the source of getFileNameWithTimeStampInterposed(final String fileName)
Parameter | Description |
---|---|
fileName | <code>String</code> with the file name to parse out |
String
with the updated file name containing a time stamp
public static String getFileNameWithTimeStampInterposed(final String fileName)
//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)); } } }