Java Date Parse dateFromFilename(File file, Pattern fileNamePattern, DateFormat dateFormat)

Here you can find the source of dateFromFilename(File file, Pattern fileNamePattern, DateFormat dateFormat)

Description

Returns the date embedded in the given file name as milliseconds since the epoch.

License

Open Source License

Parameter

Parameter Description
fileNamePattern The pattern that the file must match. It's first group should be the date portion of the file name.
dateFormat The format used to parse the date portion of the file name.

Declaration

public static long dateFromFilename(File file, Pattern fileNamePattern, DateFormat dateFormat) 

Method Source Code


//package com.java2s;
import java.io.File;

import java.text.DateFormat;

import java.text.ParsePosition;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    /**/*from  w w  w.ja v  a 2  s .  c  o  m*/
     * Returns the date embedded in the given file name as milliseconds since
     * the epoch. Returns zero if the file does not match the given
     * {@code fileNamePattern}.
     *
     * @param fileNamePattern
     *            The pattern that the file must match. It's first group should
     *            be the date portion of the file name.
     * @param dateFormat
     *            The format used to parse the date portion of the file name.
     */
    public static long dateFromFilename(File file, Pattern fileNamePattern, DateFormat dateFormat) {
        long utc = 0;
        Matcher m = fileNamePattern.matcher(file.getName());
        if (m.matches()) {
            utc = dateFormat.parse(m.group(1), new ParsePosition(0)).getTime();
        }
        return utc;
    }
}

Related

  1. convertTimeStampToDate(String dateString, String srcFormat, String destFormat)
  2. convertTimestampToDateTime(Long timestamp, Boolean withSeconds)
  3. convertTimeZonesToDate(String fromTimeZone, String toTimeZone, DateTime fromDateTime)
  4. convertUTCDateToLocal(String sDate, TimeZone timezone)
  5. convertXMLDate(String date)
  6. dateFromHourAndMinutes(final String pTime)
  7. dateFromISO8601(String iso)
  8. dateFromISO8601(String time)
  9. dateFromISODate(String isoDate)