Android URL Read getFileDateTime(URL url)

Here you can find the source of getFileDateTime(URL url)

Description

Gets file date and time.

License

Open Source License

Parameter

Parameter Description
url The URL of the file for which date and time will be returned.

Return

Returns long value which is the date and time of the file. If any error occures returns -1 (=no file date and time available).

Declaration

public static long getFileDateTime(URL url) 

Method Source Code

//package com.java2s;
/*//  w w w . j ava  2  s.  co m
 * IzPack - Copyright 2001-2008 Julien Ponge, All Rights Reserved.
 *
 * http://izpack.org/
 * http://izpack.codehaus.org/
 *
 * Copyright 2005 Marc Eppelmann
 *
 * 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.
 */

import java.io.*;
import java.net.URL;

public class Main {
    /**
     * Gets file date and time.
     *
     * @param url The URL of the file for which date and time will be returned.
     * @return Returns long value which is the date and time of the file. If any error
     *         occures returns -1 (=no file date and time available).
     */
    public static long getFileDateTime(URL url) {
        if (url == null) {
            return -1;
        }

        String fileName = url.getFile();
        if (fileName.charAt(0) == '/' || fileName.charAt(0) == '\\') {
            fileName = fileName.substring(1, fileName.length());
        }

        try {
            File file = new File(fileName);
            // File name must be a file or a directory.
            if (!file.isDirectory() && !file.isFile()) {
                return -1;
            }

            return file.lastModified();
        } catch (java.lang.Exception e) { // Trap all Exception based exceptions and return -1.
            return -1;
        }
    }
}

Related

  1. getResource(String filename)
  2. readFile(URL url)
  3. getXML(String url)
  4. readXmlFromUrl(String address)
  5. getStringFromUrl(String url)
  6. readTextFromUri(Context c, Uri uri)
  7. getHtmlByteArray(final String url)