Java Path File Time nio addTimestamp(Path base, TimeUnit unit)

Here you can find the source of addTimestamp(Path base, TimeUnit unit)

Description

add Timestamp

License

Apache License

Declaration

public static Path addTimestamp(Path base, TimeUnit unit) 

Method Source Code

//package com.java2s;
/* Copyright (c) 2011 Danish Maritime Authority.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.//ww  w  .  j  av  a 2 s. co  m
 */

import java.nio.file.Path;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

public class Main {
    public static Path addTimestamp(Path base, TimeUnit unit) {
        long now = System.currentTimeMillis();
        String filename = base.getFileName().toString();
        int i = filename.indexOf('.');
        String postfix = addTimeStampGetPostFix(new Date(now), unit);
        if (i > 0) {
            filename = filename.substring(0, i) + "-" + postfix + filename.substring(i);
        } else {
            filename = filename + "-" + postfix;
        }
        return base.getParent().resolve(filename);
    }

    public static String addTimeStampGetPostFix(Date now, TimeUnit unit) {
        if (unit == TimeUnit.MINUTES) {
            // does not handle daylight saving properties
            // Ogsaa lige et problem hvis den allerede har vaere aabnet
            // og saa bliver service genstarted
            return new SimpleDateFormat("yyyy-MM-dd_HH:mm").format(now);
        } else if (unit == TimeUnit.HOURS) {
            return new SimpleDateFormat("yyyy-MM-dd_HH").format(now);
        } else if (unit == TimeUnit.DAYS) {
            return new SimpleDateFormat("yyyy-MM-dd").format(now);
        } else {
            throw new IllegalArgumentException(unit.toString());
        }
    }
}

Related

  1. awaitLogMessage(String message, long timeout, Path logFilePath)
  2. fileCreationTime(Path path)
  3. getLastAccessTime(Path file)
  4. getLastModificationTime(Path path)