use of java.text.SimpleDateFormat in project tomcat by apache.
the class ConcurrentDateFormat method format.
public String format(Date date) {
SimpleDateFormat sdf = queue.poll();
if (sdf == null) {
sdf = createInstance();
}
String result = sdf.format(date);
queue.add(sdf);
return result;
}
use of java.text.SimpleDateFormat in project tomcat by apache.
the class TestDateFormatCache method testBug54044.
// Note that there is a similar test:
// org.apache.catalina.valves.TestAccessLogValve.testBug54044()
@Test
public void testBug54044() throws Exception {
final String timeFormat = "dd-MMM-yyyy HH:mm:ss";
final int cacheSize = 10;
SimpleDateFormat sdf = new SimpleDateFormat(timeFormat, Locale.US);
sdf.setTimeZone(TimeZone.getDefault());
DateFormatCache dfc = new DateFormatCache(cacheSize, timeFormat, null);
// Get dfc.cache.cache field
Object dfcCache;
Field dfcCacheArray;
Field dfcCacheField = dfc.getClass().getDeclaredField("cache");
dfcCacheField.setAccessible(true);
dfcCache = dfcCacheField.get(dfc);
dfcCacheArray = dfcCache.getClass().getDeclaredField("cache");
dfcCacheArray.setAccessible(true);
// Create an array to hold the expected values
String[] expected = new String[cacheSize];
// Fill the cache & populate the expected values
for (int secs = 0; secs < (cacheSize); secs++) {
dfc.getFormat(secs * 1000);
expected[secs] = generateExpected(sdf, secs);
}
Assert.assertArrayEquals(expected, (String[]) dfcCacheArray.get(dfcCache));
// Cause the cache to roll-around by one and then confirm
dfc.getFormat(cacheSize * 1000);
expected[0] = generateExpected(sdf, cacheSize);
Assert.assertArrayEquals(expected, (String[]) dfcCacheArray.get(dfcCache));
// Jump 2 ahead and then confirm (skipped value should be null)
dfc.getFormat((cacheSize + 2) * 1000);
expected[1] = null;
expected[2] = generateExpected(sdf, cacheSize + 2);
Assert.assertArrayEquals(expected, (String[]) dfcCacheArray.get(dfcCache));
// Back 1 to fill in the gap
dfc.getFormat((cacheSize + 1) * 1000);
expected[1] = generateExpected(sdf, cacheSize + 1);
Assert.assertArrayEquals(expected, (String[]) dfcCacheArray.get(dfcCache));
// Return to 1 and confirm skipped value is null
dfc.getFormat(1 * 1000);
expected[1] = generateExpected(sdf, 1);
expected[2] = null;
Assert.assertArrayEquals(expected, (String[]) dfcCacheArray.get(dfcCache));
// Go back one further
dfc.getFormat(0);
expected[0] = generateExpected(sdf, 0);
Assert.assertArrayEquals(expected, (String[]) dfcCacheArray.get(dfcCache));
// Jump ahead far enough that the entire cache will need to be cleared
dfc.getFormat(42 * 1000);
for (int i = 0; i < cacheSize; i++) {
expected[i] = null;
}
expected[0] = generateExpected(sdf, 42);
Assert.assertArrayEquals(expected, (String[]) dfcCacheArray.get(dfcCache));
}
use of java.text.SimpleDateFormat in project tomcat by apache.
the class Async0 method service.
@Override
protected void service(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
if (Boolean.TRUE.equals(req.getAttribute("dispatch"))) {
log.info("Received dispatch, completing on the worker thread.");
log.info("After complete called started:" + req.isAsyncStarted());
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
resp.getWriter().write("Async dispatch worked: " + sdf.format(date) + "\n");
} else {
resp.setContentType("text/plain");
final AsyncContext actx = req.startAsync();
actx.setTimeout(Long.MAX_VALUE);
Runnable run = new Runnable() {
@Override
public void run() {
try {
req.setAttribute("dispatch", Boolean.TRUE);
Thread.currentThread().setName("Async0-Thread");
log.info("Putting AsyncThread to sleep");
Thread.sleep(2 * 1000);
log.info("Dispatching");
actx.dispatch();
} catch (InterruptedException x) {
log.error("Async1", x);
} catch (IllegalStateException x) {
log.error("Async1", x);
}
}
};
Thread t = new Thread(run);
t.start();
}
}
use of java.text.SimpleDateFormat in project zookeeper by apache.
the class Log4JSource method init.
private void init() throws IOException {
File f = new File(file);
RandomAccessFileReader in = new RandomAccessFileReader(f);
SimpleDateFormat dateformat = new SimpleDateFormat(DATE_FORMAT);
Pattern idp = Pattern.compile("\\[myid:(\\d+)\\]");
long lastFp = in.getPosition();
String line = in.readLine();
Matcher m = null;
// if we have read data from the file, and it matchs the timep pattern
if ((line != null) && (m = timep.matcher(line)).lookingAt()) {
starttime = timestampFromText(dateformat, m.group(1));
} else {
throw new IOException("Invalid log4j format. First line doesn't start with time");
}
/*
Count number of log entries. Any line starting with a timestamp counts as an entry
*/
String lastentry = line;
try {
while (line != null) {
m = timep.matcher(line);
if (m.lookingAt()) {
if (size % skipN == 0) {
long time = timestampFromText(dateformat, m.group(1));
skiplist.addMark(time, lastFp, size);
}
size++;
lastentry = line;
}
if (serverid == 0 && (m = idp.matcher(line)).find()) {
serverid = Integer.valueOf(m.group(1));
}
lastFp = in.getPosition();
line = in.readLine();
}
} catch (EOFException eof) {
// ignore, simply end of file, though really (line!=null) should have caught this
} finally {
in.close();
}
m = timep.matcher(lastentry);
if (m.lookingAt()) {
endtime = timestampFromText(dateformat, m.group(1));
} else {
throw new IOException("Invalid log4j format. Last line doesn't start with time");
}
}
use of java.text.SimpleDateFormat in project TakePhoto by crazycodeboy.
the class TUriParse method getTempUri.
/**
* 获取一个临时的Uri, 文件名随机生成
* @param context
* @return
*/
public static Uri getTempUri(Context context) {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
File file = new File(Environment.getExternalStorageDirectory(), "/images/" + timeStamp + ".jpg");
if (!file.getParentFile().exists())
file.getParentFile().mkdirs();
return getUriForFile(context, file);
}
Aggregations