Example usage for java.text SimpleDateFormat applyPattern

List of usage examples for java.text SimpleDateFormat applyPattern

Introduction

In this page you can find the example usage for java.text SimpleDateFormat applyPattern.

Prototype

public void applyPattern(String pattern) 

Source Link

Document

Applies the given pattern string to this date format.

Usage

From source file:org.pentaho.di.job.entries.zipfile.JobEntryZipFile.java

public String getFullFilename(String filename, boolean add_date, boolean add_time, boolean specify_format,
        String datetime_folder) {
    String retval = "";
    if (Const.isEmpty(filename)) {
        return null;
    }//from w w w.j a  v a 2s .co  m

    // Replace possible environment variables...
    String realfilename = environmentSubstitute(filename);
    int lenstring = realfilename.length();
    int lastindexOfDot = realfilename.lastIndexOf('.');
    if (lastindexOfDot == -1) {
        lastindexOfDot = lenstring;
    }

    retval = realfilename.substring(0, lastindexOfDot);

    final SimpleDateFormat daf = new SimpleDateFormat();
    Date now = new Date();

    if (specify_format && !Const.isEmpty(datetime_folder)) {
        daf.applyPattern(datetime_folder);
        String dt = daf.format(now);
        retval += dt;
    } else {
        if (add_date) {
            daf.applyPattern("yyyyMMdd");
            String d = daf.format(now);
            retval += "_" + d;
        }
        if (add_time) {
            daf.applyPattern("HHmmssSSS");
            String t = daf.format(now);
            retval += "_" + t;
        }
    }
    retval += realfilename.substring(lastindexOfDot, lenstring);
    return retval;

}

From source file:edu.ku.brc.af.prefs.AppPrefsCache.java

/**
 * Creates or gets the pref node, creates an entry and then hooks it up as a listener.
 * The current value of the SimpleDateFormat because the default.
 * @param simpleFormat the SimpleDateFormat object 
 * @param section the section or category of the pref
 * @param pref the pref's name/*from   w w  w  . j a  v a2s  .  c o m*/
 * @param attrName the actual attribute
 */
public void registerInternal(final SimpleDateFormat simpleFormat, final String section, final String pref,
        final String attrName) {
    checkName(section, pref, attrName);

    String fullName = makeKey(section, pref, attrName);
    if (hash.get(fullName) == null) {
        String defValue = simpleFormat.toPattern();
        String prefVal = checkForPref(fullName, defValue);

        //-------------------------------------------------------------------------------
        // This corrects the formatter when it has a two digit year (Bug 7555)
        // still have not found out what is causing the problem.
        //-------------------------------------------------------------------------------
        if (prefVal.length() == 8 && StringUtils.countMatches(prefVal, "yyyy") == 0) {
            prefVal = StringUtils.replace(prefVal, "yy", "yyyy");
        }

        simpleFormat.applyPattern(prefVal);
        DateFormatCacheEntry dateEntry = new DateFormatCacheEntry(simpleFormat, fullName, prefVal, defValue);
        getPref().addChangeListener(fullName, dateEntry);
        hash.put(fullName, dateEntry);
    }

}

From source file:com.panet.imeta.job.entries.unzip.JobEntryUnZip.java

/**
 * @param string//w  w  w .j  av  a2 s  .c  o  m
 *            the filename from the FTP server
 * 
 * @return the calculated target filename
 */
protected String getTargetFilename(String filename) {

    String retval = "";
    // Replace possible environment variables...
    if (filename != null)
        retval = filename;

    int lenstring = retval.length();
    int lastindexOfDot = retval.lastIndexOf('.');
    if (lastindexOfDot == -1)
        lastindexOfDot = lenstring;

    retval = retval.substring(0, lastindexOfDot);

    SimpleDateFormat daf = new SimpleDateFormat();
    Date now = new Date();

    if (SpecifyFormat && !Const.isEmpty(date_time_format)) {
        daf.applyPattern(date_time_format);
        String dt = daf.format(now);
        retval += dt;
    } else {

        if (adddate) {
            daf.applyPattern("yyyyMMdd");
            String d = daf.format(now);
            retval += "_" + d;
        }
        if (addtime) {
            daf.applyPattern("HHmmssSSS");
            String t = daf.format(now);
            retval += "_" + t;
        }
    }

    retval += filename.substring(lastindexOfDot, lenstring);

    return retval;

}

From source file:com.funambol.foundation.items.dao.PIMCalendarDAO.java

/**
 * Converts a String object representing a date into a corresponding Date
 * object apt to represent a date in the DB.
 *
 * @param allDay true if the information is expected to be in the all-day
 *                    format/*  www .j a  v a2  s.co m*/
 * @param date if allDay is true, it should be in the "yyyy-MM-dd" format:
 *             the proper time will be attached for DB-saving purposes; if
 *             allDay is false, it should be in the "yyyyMMdd'T'HHmmss'Z'"
 *             format, but will be forced into the right format also if it's
 *             in the "yyyyMMdd'T'HHmmss" format (a 'Z' will be appended) or
 *             in the "yyyy-MM-dd" format (in this case, as for all-day
 *             dates, the time will be considered to be equal to the second
 *             parameter)
 * @param time a String object representing the default time in the "HHmmss"
 *             format, to be used when the date is in an untimed format but
 *             this non-all-day is nevertheless used (see above); if it's
 *             null, the "yyyy-MM-dd" format can't be used and if the date
 *             is in that format a null value will be returned
 * @return a Date object
 */
private static Date getDateFromString(boolean allDay, String date, String time) throws ParseException {

    if (date == null) { // Preserves null content which in this context is
        return null; // meaningful
    }
    if (date.length() == 0) { // Considers "" as a null but meaningful value
        return null;
    }

    //
    // Trimming the given date because some devices send it with a space at
    // the beginning
    //
    date = date.trim();

    SimpleDateFormat dateFormatter = new SimpleDateFormat();
    dateFormatter.setTimeZone(TimeUtils.TIMEZONE_UTC);

    if (allDay) {
        dateFormatter.applyPattern(TimeUtils.PATTERN_UTC_WOZ);
    } else {
        dateFormatter.applyPattern(TimeUtils.PATTERN_UTC);
    }

    String dateOK;

    String format = TimeUtils.getDateFormat(date);

    if (format.equals(TimeUtils.PATTERN_YYYY_MM_DD) || format.equals(TimeUtils.PATTERN_YYYYMMDD)) {
        if (time == null) { // that means that untimed dates mustn't be
            return null; // accepted, so a null value is returned
        }
        dateOK = TimeUtils.convertDateFromInDayFormat(date, time, !allDay);
    } else if (format.equals(TimeUtils.PATTERN_UTC_WOZ)) {
        if (allDay) {
            dateOK = date;
        } else {
            dateOK = date + 'Z'; // the non-all-day formatter wants a 'Z'
        }
    } else { // then format should be = TimeUtils.PATTERN_UTC
        dateOK = date;
    }

    // At this point dateOK will be either in the "yyyyMMdd'T'HHmmss'Z'"
    // (non-all-day) or in the "yyyyMMdd'T'HHmmss" (all-day) format.

    return dateFormatter.parse(dateOK);
}

From source file:org.pentaho.di.job.entries.ftpsget.JobEntryFTPSGet.java

/**
 * @param string//from  w w  w. j  av  a 2 s . c  o m
 *          the filename from the FTPS server
 *
 * @return the calculated target filename
 */
private String returnTargetFilename(String filename) {
    String retval = null;
    // Replace possible environment variables...
    if (filename != null) {
        retval = filename;
    } else {
        return null;
    }

    int lenstring = retval.length();
    int lastindexOfDot = retval.lastIndexOf(".");
    if (lastindexOfDot == -1) {
        lastindexOfDot = lenstring;
    }

    if (isAddDateBeforeExtension()) {
        retval = retval.substring(0, lastindexOfDot);
    }

    SimpleDateFormat daf = new SimpleDateFormat();
    Date now = new Date();

    if (SpecifyFormat && !Const.isEmpty(date_time_format)) {
        daf.applyPattern(date_time_format);
        String dt = daf.format(now);
        retval += dt;
    } else {
        if (adddate) {
            daf.applyPattern("yyyyMMdd");
            String d = daf.format(now);
            retval += "_" + d;
        }
        if (addtime) {
            daf.applyPattern("HHmmssSSS");
            String t = daf.format(now);
            retval += "_" + t;
        }
    }

    if (isAddDateBeforeExtension()) {
        retval += retval.substring(lastindexOfDot, lenstring);
    }

    // Add foldername to filename
    retval = localFolder + Const.FILE_SEPARATOR + retval;
    return retval;
}

From source file:org.pentaho.di.job.entries.movefiles.JobEntryMoveFiles.java

private String getMoveDestinationFilename(String shortsourcefilename, String DateFormat) throws Exception {
    String shortfilename = shortsourcefilename;
    int lenstring = shortsourcefilename.length();
    int lastindexOfDot = shortfilename.lastIndexOf('.');
    if (lastindexOfDot == -1) {
        lastindexOfDot = lenstring;/*from www. j a v  a  2  s  .  c  o m*/
    }

    if (isAddMovedDateBeforeExtension()) {
        shortfilename = shortfilename.substring(0, lastindexOfDot);
    }

    SimpleDateFormat daf = new SimpleDateFormat();
    Date now = new Date();

    if (DateFormat != null) {
        daf.applyPattern(DateFormat);
        String dt = daf.format(now);
        shortfilename += dt;
    } else {

        if (isSpecifyMoveFormat() && !Const.isEmpty(getMovedDateTimeFormat())) {
            daf.applyPattern(getMovedDateTimeFormat());
            String dt = daf.format(now);
            shortfilename += dt;
        } else {
            if (isAddMovedDate()) {
                daf.applyPattern("yyyyMMdd");
                String d = daf.format(now);
                shortfilename += "_" + d;
            }
            if (isAddMovedTime()) {
                daf.applyPattern("HHmmssSSS");
                String t = daf.format(now);
                shortfilename += "_" + t;
            }
        }
    }
    if (isAddMovedDateBeforeExtension()) {
        shortfilename += shortsourcefilename.substring(lastindexOfDot, lenstring);
    }

    return shortfilename;
}

From source file:com.panet.imeta.job.entries.movefiles.JobEntryMoveFiles.java

private String getMoveDestinationFilename(String shortsourcefilename, String DateFormat) throws Exception {
    String shortfilename = shortsourcefilename;
    int lenstring = shortsourcefilename.length();
    int lastindexOfDot = shortfilename.lastIndexOf('.');
    if (lastindexOfDot == -1)
        lastindexOfDot = lenstring;/*www  .  j ava  2  s. com*/

    if (isAddMovedDateBeforeExtension())
        shortfilename = shortfilename.substring(0, lastindexOfDot);

    SimpleDateFormat daf = new SimpleDateFormat();
    Date now = new Date();

    if (DateFormat != null) {
        daf.applyPattern(DateFormat);
        String dt = daf.format(now);
        shortfilename += dt;
    } else {

        if (isSpecifyMoveFormat() && !Const.isEmpty(getMovedDateTimeFormat())) {
            daf.applyPattern(getMovedDateTimeFormat());
            String dt = daf.format(now);
            shortfilename += dt;
        } else {
            if (isAddMovedDate()) {
                daf.applyPattern("yyyyMMdd");
                String d = daf.format(now);
                shortfilename += "_" + d;
            }
            if (isAddMovedTime()) {
                daf.applyPattern("HHmmssSSS");
                String t = daf.format(now);
                shortfilename += "_" + t;
            }
        }
    }
    if (isAddMovedDateBeforeExtension())
        shortfilename += shortsourcefilename.substring(lastindexOfDot, lenstring);

    return shortfilename;
}

From source file:org.pentaho.di.trans.steps.rssoutput.RssOutputMeta.java

public String buildFilename(VariableSpace space, int stepnr) throws KettleStepException {

    SimpleDateFormat daf = new SimpleDateFormat();

    // Replace possible environment variables...
    String retval = getFilename(space);

    Date now = new Date();

    if (dateInFilename) {
        daf.applyPattern("yyyMMdd");
        String d = daf.format(now);
        retval += "_" + d;
    }//from   www  . j  a  v a  2  s  . c o  m
    if (timeInFilename) {
        daf.applyPattern("HHmmss");
        String t = daf.format(now);
        retval += "_" + t;
    }
    if (stepNrInFilename) {
        retval += "_" + stepnr;
    }

    if (extension != null && extension.length() != 0) {
        retval += "." + extension;
    }

    return retval;
}

From source file:hydrograph.ui.dataviewer.filter.FilterHelper.java

private Date getParsedDate(String nextToken, SimpleDateFormat formatter) throws ParseException {
    Date parsedDate = formatter.parse(nextToken);
    formatter.applyPattern(AdapterConstants.DATE_FORMAT);
    return parsedDate;
}

From source file:translator.logic.AllVendorAnnotationTranslator.java

/**
 * Translates CSV file to XML file and save it
 * @param annotation_file annotation file name 
 * @param stage_file stage file name//from   www  .  j  a va  2s. c o m
 * @param edf_file edf file name
 * @param mapping_file mapping file name
 * @param output_file output file name
 * @return true if translation is successful
 */
@SuppressWarnings("unchecked")
public boolean convertCSV(String annotation_file, String stage_file, String edf_file, String mapping_file,
        String output_file) {

    HashMap<String, Object>[] map = readMapFile(mapping_file);
    Document doc = new DocumentImpl();

    Element root = doc.createElement("PSGAnnotaion");

    Element software = doc.createElement("SoftwareVersion");
    software.appendChild(doc.createTextNode("Respironics"));
    root.appendChild(software);

    Element epoch = doc.createElement("EpochLength");
    int epochLength = Integer.parseInt((String) map[0].get("EpochLength"));
    epoch.appendChild(doc.createTextNode(Integer.toString(epochLength)));
    root.appendChild(epoch);

    Element events = doc.createElement("ScoredEvents");
    // Recording start time
    String[] times = readEDF(edf_file);
    String[] elmtStr = { "Recording Start Time", "0", times[1] };
    Element eventElmt = addElements(doc, elmtStr);
    Element clockTime = doc.createElement("ClockTime");
    clockTime.appendChild(doc.createTextNode(times[0]));
    eventElmt.appendChild(clockTime);
    events.appendChild(eventElmt);
    // get start time to compute relative start time
    SimpleDateFormat df = new SimpleDateFormat("dd.mm.yy hh.mm.ss");
    Date clocktime = new Date();
    try {
        clocktime = df.parse(times[0]);
    } catch (Exception e) {
        e.printStackTrace();
        StringWriter errors = new StringWriter();
        e.printStackTrace(new PrintWriter(errors));
        log(errors.toString());
    }
    df.applyPattern("hh:mm:ss a mm/dd/yyyy");

    boolean bTranslation = true;
    try {
        // get events
        File file = new File(annotation_file);
        Scanner s = new Scanner(file);
        // wei wang, 2014-7-11: "Scanner s never closed"
        try {
            String line = s.nextLine();
            while (s.hasNext()) {
                line = s.nextLine();
                //.out.println(line);
                String[] data = line.split("\",");
                Date time = df.parse(data[2].substring(1) + " " + data[4].substring(1));
                String[] elmts = new String[3];
                // System.out.println(map[1].keySet().toString());

                if (map[1].keySet().contains(data[0].substring(1))) {
                    // System.out.println("here");
                    elmts[0] = ((ArrayList<String>) map[1].get(data[0].substring(1))).get(1);
                    long starttime = (time.getTime() - clocktime.getTime()) / 1000;
                    elmts[1] = Long.toString(starttime);
                    elmts[2] = data[5].substring(1);
                    Element event = addElements(doc, elmts);
                    if (((ArrayList<String>) map[1].get(data[0].substring(1))).get(0)
                            .contains("Desaturation")) {
                        Element nadir = doc.createElement("SpO2Nadir");
                        nadir.appendChild(doc.createTextNode(data[10].substring(1)));
                        event.appendChild(nadir);
                        Element baseline = doc.createElement("SpO2Baseline");
                        baseline.appendChild(doc.createTextNode(data[9].substring(1)));
                        event.appendChild(baseline);
                    }
                    events.appendChild(event);
                } else {
                    elmts[0] = "Technical Note Event";
                    long starttime = (time.getTime() - clocktime.getTime()) / 1000;
                    elmts[1] = Long.toString(starttime);
                    elmts[2] = data[5].substring(1);
                    Element event = addElements(doc, elmts);
                    Element note = doc.createElement("Notes");
                    note.appendChild(doc.createTextNode(data[0].substring(1)));
                    event.appendChild(note);
                    events.appendChild(event);
                    String log = annotation_file + "," + data[0].substring(1) + ", " + data[2].substring(1)
                            + ", " + data[4].substring(1);
                    log(log);
                }
            }
            // get stages
            s = new Scanner(new File(stage_file));
            line = s.nextLine();
            line = s.nextLine();
            String[] sleep = line.split(",");
            String stage = "";
            int count = 0;
            int start = 0;
            if (map[2].keySet().contains(sleep[0])) {
                stage = sleep[0];
                count = 1;
            }
            while (s.hasNext()) {
                line = s.nextLine();

                sleep = line.split(",");
                if (sleep[0].equalsIgnoreCase(stage)) {
                    count = count + 1;
                } else {
                    String[] elmts = new String[3];
                    elmts[0] = (String) map[2].get(stage);
                    elmts[1] = Integer.toString(start);
                    elmts[2] = Long.toString(Integer.parseInt((String) map[0].get("EpochLength")) * count);
                    Element event = addElements(doc, elmts);
                    events.appendChild(event);

                    start = start + Integer.parseInt((String) map[0].get("EpochLength")) * count;
                    count = 1;
                    stage = sleep[0];
                }
            }
            String[] elmts = new String[3];
            elmts[0] = (String) map[2].get(stage);
            elmts[1] = Integer.toString(start);
            elmts[2] = Long.toString(Integer.parseInt((String) map[0].get("EpochLength")) * count);
            Element event = addElements(doc, elmts);
            events.appendChild(event);
            // wei wang:
        } finally {
            s.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
        bTranslation = false;
        StringWriter errors = new StringWriter();
        e.printStackTrace(new PrintWriter(errors));
        log(errors.toString());
    }

    root.appendChild(events);
    doc.appendChild(root);
    saveXML(doc, output_file);
    return bTranslation;
}