Search in sources :

Example 66 with DateFormat

use of java.text.DateFormat in project OpenGrok by OpenGrok.

the class RazorHistoryParser method parseContents.

protected History parseContents(BufferedReader contents) throws IOException {
    DateFormat df = repository.getDateFormat();
    String line;
    ArrayList<HistoryEntry> entries = new ArrayList<HistoryEntry>();
    HistoryEntry entry = null;
    boolean ignoreEntry = false;
    boolean seenActionType = false;
    boolean lastWasTitle = true;
    Matcher actionMatcher = ACTION_TYPE_PATTERN.matcher("");
    Matcher infoMatcher = ADDITIONAL_INFO_PATTERN.matcher("");
    while ((line = contents.readLine()) != null) {
        parseDebug("Processing '" + line + "'");
        if (StringUtils.isOnlyWhitespace(line)) {
            if (entry != null && entry.getDate() != null) {
                entries.add(entry);
                dumpEntry(entry);
            }
            entry = new HistoryEntry();
            ignoreEntry = false;
            seenActionType = false;
        } else if (!ignoreEntry) {
            if (seenActionType) {
                infoMatcher.reset(line);
                if (infoMatcher.find()) {
                    String infoType = infoMatcher.group(1);
                    String details = infoMatcher.group(2);
                    if ("TITLE".equals(infoType)) {
                        parseDebug("Setting Message : '" + details + "'");
                        entry.setMessage(details);
                        lastWasTitle = true;
                    } else if ("ISSUE".equals(infoType)) {
                        parseDebug("Adding CR : '" + details + "'");
                        entry.addChangeRequest(details);
                    } else {
                        parseDebug("Ignoring Info Type Line '" + line + "'");
                    }
                } else {
                    if (!line.startsWith("##") && line.charAt(0) == '#') {
                        parseDebug("Seen Comment : '" + line + "'");
                        if (lastWasTitle) {
                            entry.appendMessage("");
                            lastWasTitle = false;
                        }
                        entry.appendMessage(line.substring(1));
                    } else {
                        parseProblem("Expecting addlInfo and got '" + line + "'");
                    }
                }
            } else {
                actionMatcher.reset(line);
                if (actionMatcher.find()) {
                    seenActionType = true;
                    if (entry != null && entry.getDate() != null) {
                        entries.add(entry);
                        dumpEntry(entry);
                    }
                    entry = new HistoryEntry();
                    String actionType = actionMatcher.group(1);
                    String userName = actionMatcher.group(2);
                    String revision = actionMatcher.group(3);
                    String state = actionMatcher.group(4);
                    String dateTime = actionMatcher.group(5);
                    parseDebug("New History Event Seen : actionType = " + actionType + ", userName = " + userName + ", revision = " + revision + ", state = " + state + ", dateTime = " + dateTime);
                    if (actionType.startsWith("INTRODUCE") || actionType.contains("CHECK-IN") || "CHECK-POINT".equals(actionType) || "REVERT".equals(actionType)) {
                        entry.setAuthor(userName);
                        entry.setRevision(revision);
                        entry.setActive("Active".equals(state));
                        Date date = null;
                        try {
                            date = df.parse(dateTime);
                        } catch (ParseException pe) {
                            //
                            throw new IOException("Could not parse date: " + dateTime, pe);
                        }
                        entry.setDate(date);
                        ignoreEntry = false;
                    } else {
                        ignoreEntry = true;
                    }
                } else {
                    parseProblem("Expecting actionType and got '" + line + "'");
                }
            }
        }
    }
    if (entry != null && entry.getDate() != null) {
        entries.add(entry);
        dumpEntry(entry);
    }
    History history = new History();
    history.setHistoryEntries(entries);
    return history;
}
Also used : Matcher(java.util.regex.Matcher) DateFormat(java.text.DateFormat) ArrayList(java.util.ArrayList) ParseException(java.text.ParseException) IOException(java.io.IOException) Date(java.util.Date)

Example 67 with DateFormat

use of java.text.DateFormat in project OpenGrok by OpenGrok.

the class SubversionRepositoryTest method testDateFormats.

@Test
public void testDateFormats() {
    String[][] tests = new String[][] { { "abcd", "expected exception" }, { "2016-01-01 10:00:00", "expected exception" }, { "2016 Sat, 1 Apr 2008 15:12:51 +0000", "expected exception" }, { "Sat, 1 Dub 2008 15:12:51 +0000", "expected exception" }, { "2016_01_01T10:00:00Z", "expected exception" }, { "2016-01-01T10:00:00T", "expected exception" }, { "2016-01-01T10:00:00.200", "expected exception" }, { "2016-01-01 10:00:00Z", "expected exception" }, // lenient - wrong hour
    { "2016-01-01T40:00:00Z", null }, // lenient - wrong minute
    { "2016-01-01T00:70:00Z", null }, // lenient - wrong second
    { "2016-01-01T00:00:99Z", null }, // lenient - wrong day
    { "2016-03-40T00:00:00Z", null }, { "2016-01-01T10:00:00.200Z", null }, { "2016-01-01T11:00:00.200Z", null }, { "2016-01-01T10:00:00.Z", null }, { "2017-01-01T10:00:00.Z", null }, { "2016-01-01T10:00:00Z", null } };
    DateFormat format = new SubversionRepository().getDateFormat();
    for (String[] test : tests) {
        try {
            format.parse(test[0]);
            if (test[1] != null) {
                Assert.fail("Shouldn't be able to parse the date: " + test[0]);
            }
        } catch (ParseException ex) {
            if (test[1] == null) {
                // no exception
                Assert.fail("Shouldn't throw a parsing exception for date: " + test[0]);
            }
        }
    }
}
Also used : DateFormat(java.text.DateFormat) ParseException(java.text.ParseException) Test(org.junit.Test)

Example 68 with DateFormat

use of java.text.DateFormat in project OpenGrok by OpenGrok.

the class RepositoryTest method testDateFormats.

@Test
public void testDateFormats() throws ParseException {
    String[][] tests = new String[][] { { "2016-01-01 10:00:00", "yyyy-MM-dd HH:mm:ss" }, { "2016 Sat, 5 Apr 2008 15:12:51 +0000", "yyyy EE, d MMM yyyy HH:mm:ss Z" }, { "Sat, 5 Apr 2008 15:12:51 +0000", "EE, d MMM yyyy HH:mm:ss Z" }, { "2016_01_01T10:00:00Z", "yyyy_MM_dd'T'HH:mm:ss'Z'" }, { "2016-01-01 10:00", "yyyy-MM-dd HH:mm" }, { "2016_01_01 10:00 +0000", "yyyy_MM_dd HH:mm Z" }, { "2016-01-01 10:00:00 +0000", "yyyy-MM-dd HH:mm:ss Z" }, { "2016-01-01 10:00 +0000", "yyyy-MM-dd HH:mm Z" }, { "2016-01-02 10:00 +0000", "yyyy-MM-dd HH:mm Z" }, { "2017-01-01 10:00 +0700", "yyyy-MM-dd HH:mm Z" }, { "2016-12-31 10:00 +0000", "yyyy-MM-dd HH:mm Z" } };
    for (String[] test : tests) {
        RepositoryImplementation repository = new RepositoryImplementation();
        repository.setDatePatterns(new String[] { test[1] });
        DateFormat format = repository.getDateFormat();
        format.parse(test[0]);
    }
}
Also used : DateFormat(java.text.DateFormat) Test(org.junit.Test)

Example 69 with DateFormat

use of java.text.DateFormat in project OpenGrok by OpenGrok.

the class RepositoryTest method testMultipleInvalidDateFormats.

@Test
public void testMultipleInvalidDateFormats() {
    String[][] tests = new String[][] { { "2016-01-01 10:00:00", "'abcd'", "MMM yy:ss ", "EE, d MMM yyyy Hss Z" }, { "2016 Sat, 5 Apr 2008 15:12:51 +0000", "yyyy d MMM yy:ss Z", "yyyy EE, d MMM ss Z" }, { "Sat, 5 Apr 2008 15:12:51 +0000", "d MMM yyyy Z", "EE, d MMM yyyy Hss Z" }, { "2016_01_01T10:00:00Z", "yyyy_MM_dd'T'ss'Z'" }, { "2016-01-01 10:00", "MM-dd Z", "EE-MM-dd HH", "Z dd-dd HH:mm" }, { "2016_01_01 10:00 +0000", "yyyy-MM_dd HH:mm Z" }, { "2016-01-01 10:00:00 +0000", "yyyy_MM_dd HH:s Z", "'bad format'" }, { "2016-01-01 10:00 +0000", "MM-d HH:mm", "yyyy-MM-dmm Z" }, { "2016-01-02 10:00 +0000", "yyyy-Mmm Z", "yyyy-M HH:mm Z" } };
    for (String[] test : tests) {
        RepositoryImplementation repository = new RepositoryImplementation();
        repository.setDatePatterns(Arrays.copyOfRange(test, 1, test.length));
        DateFormat format = repository.getDateFormat();
        try {
            format.parse(test[0]);
            Assert.fail("Shouldn't be able to parse the date: " + test[0]);
        } catch (ParseException ex) {
        }
    }
}
Also used : DateFormat(java.text.DateFormat) ParseException(java.text.ParseException) Test(org.junit.Test)

Example 70 with DateFormat

use of java.text.DateFormat in project FastDev4Android by jiangqqlmj.

the class StrUtils method getSystemTime.

// 获取系统时间
public static String getSystemTime() {
    Date date = new Date();
    DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String result = format.format(date);
    return result;
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Aggregations

DateFormat (java.text.DateFormat)763 SimpleDateFormat (java.text.SimpleDateFormat)593 Date (java.util.Date)387 ParseException (java.text.ParseException)142 Test (org.junit.Test)86 Calendar (java.util.Calendar)85 ArrayList (java.util.ArrayList)62 IOException (java.io.IOException)47 File (java.io.File)35 HashMap (java.util.HashMap)32 List (java.util.List)29 GregorianCalendar (java.util.GregorianCalendar)25 TimeZone (java.util.TimeZone)25 Locale (java.util.Locale)22 Timestamp (java.sql.Timestamp)19 Map (java.util.Map)16 InputStream (java.io.InputStream)15 LocalDate (java.time.LocalDate)10 Matcher (java.util.regex.Matcher)10 Position (org.traccar.model.Position)10