下面列出了怎么用java.text.DateFormat.Field的API类实例代码及写法,或者点击链接到github查看源代码。
int getCalendarField(Field field) {
int cf = field.getCalendarField();
if (cf < 0) {
if (field.toString().indexOf("hour 1") > -1) { //$NON-NLS-1$
cf = Calendar.HOUR;
} else if (field.toString().contains("zone")) { //$NON-NLS-1$
cf = Calendar.ZONE_OFFSET;
} else if (field.toString().contains("hour of day 1")) { //$NON-NLS-1$
cf = Calendar.HOUR_OF_DAY;
}
}
return cf;
}
private Vector<FieldContainer> getDateVector1() {
// "19990913171901"
Vector<FieldContainer> v = new Vector<FieldContainer>();
v.add(new FieldContainer(0, 4, Field.YEAR));
v.add(new FieldContainer(4, 6, Field.MONTH));
v.add(new FieldContainer(6, 8, Field.DAY_OF_MONTH));
v.add(new FieldContainer(8, 10, Field.HOUR_OF_DAY0));
v.add(new FieldContainer(10, 12, Field.MINUTE));
v.add(new FieldContainer(12, 14, Field.SECOND));
return v;
}
private Vector<FieldContainer> getDateVector2() {
// "12 3 5 March 2002 Monday"
Vector<FieldContainer> v = new Vector<FieldContainer>();
v.add(new FieldContainer(0, 2, Field.WEEK_OF_YEAR));
v.add(new FieldContainer(3, 4, Field.WEEK_OF_MONTH));
v.add(new FieldContainer(5, 7, Field.DAY_OF_MONTH));
v.add(new FieldContainer(8, 17, Field.MONTH));
v.add(new FieldContainer(18, 22, Field.YEAR));
v.add(new FieldContainer(23, 29, Field.DAY_OF_WEEK));
return v;
}
private Vector<FieldContainer> getDateVector3() {
// "5:19 EDT"
Vector<FieldContainer> v = new Vector<FieldContainer>();
v.add(new FieldContainer(0, 1, Field.HOUR1));
v.add(new FieldContainer(2, 4, Field.MINUTE));
v.add(new FieldContainer(5, 10, Field.TIME_ZONE));
return v;
}
private Vector<FieldContainer> getDateVector5() {
// "5:19 -0400"
Vector<FieldContainer> v = new Vector<FieldContainer>();
v.add(new FieldContainer(0, 1, Field.HOUR1));
v.add(new FieldContainer(2, 4, Field.MINUTE));
v.add(new FieldContainer(5, 10, Field.TIME_ZONE));
return v;
}
@Test
public void testExecuteJobsForSingleEngine() {
// configure and build a process engine
StandaloneProcessEngineConfiguration standaloneProcessEngineConfiguration = new StandaloneInMemProcessEngineConfiguration();
standaloneProcessEngineConfiguration.setProcessEngineName(getClass().getName() + "-engine1");
standaloneProcessEngineConfiguration.setJdbcUrl("jdbc:h2:mem:jobexecutor-test-engine");
standaloneProcessEngineConfiguration.setJobExecutorActivate(false);
standaloneProcessEngineConfiguration.setJobExecutor(jobExecutor);
standaloneProcessEngineConfiguration.setDbMetricsReporterActivate(false);
ProcessEngine engine = standaloneProcessEngineConfiguration.buildProcessEngine();
createdProcessEngines.add(engine);
engine.getRepositoryService().createDeployment()
.addClasspathResource(PROCESS_RESOURCE)
.deploy();
jobExecutor.shutdown();
engine.getRuntimeService()
.startProcessInstanceByKey("intermediateTimerEventExample");
Assert.assertEquals(1, engine.getManagementService().createJobQuery().count());
Calendar calendar = Calendar.getInstance();
calendar.add(Field.DAY_OF_YEAR.getCalendarField(), 6);
ClockUtil.setCurrentTime(calendar.getTime());
jobExecutor.start();
waitForJobExecutorToProcessAllJobs(10000, 100, jobExecutor, engine.getManagementService(), true);
Assert.assertEquals(0, engine.getManagementService().createJobQuery().count());
}
/**
* inspects all of the calendar fields in the <code>field</code> array to
* determine what style is appropriate and then sets that style to the
* picker using the setPickerStyle method.<br>
*/
private boolean updateFields() {
Field[] bak = new Field[field == null ? 0 : field.length];
if (bak.length > 0) {
System.arraycopy(field, 0, bak, 0, field.length);
}
AttributedCharacterIterator aci = df
.formatToCharacterIterator(calendar.getTime());
field = new Field[aci.getAllAttributeKeys().size()];
separator = new String[field.length + 1]; // there can be a separator
// before and after
int i = 0;
Object last = null;
for (char c = aci.first(); c != CharacterIterator.DONE; c = aci
.next()) {
Object[] oa = aci.getAttributes().keySet().toArray();
if (oa.length > 0) {
if (oa[0] != last && i < field.length) {
if (getCalendarField((Field) oa[0]) < 0) {
if (bak.length > 0) {
field = new Field[bak.length];
System.arraycopy(bak, 0, field, 0, bak.length);
}
return false;
} else {
field[i] = (Field) oa[0];
last = oa[0];
}
i++;
}
} else {
if (separator[i] == null) {
separator[i] = String.valueOf(c);
}
}
}
df.setLenient(false);
setActiveField(FIELD_NONE);
return true;
}
public void t_format_with_FieldPosition() {
TimeZone tz = TimeZone.getTimeZone("EST");
Calendar cal = new GregorianCalendar(tz);
cal.set(1999, Calendar.SEPTEMBER, 13, 17, 19, 01);
cal.set(Calendar.MILLISECOND, 0);
Date date = cal.getTime();
SimpleDateFormat format = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.US);
format.setTimeZone(tz);
// test with all pattern chars, and multiple occurences
format.applyPattern("G GGGG y yy yyyy M MM MMM MMMM d dd ddd " +
"k kk kkk H HH HHH h hh hhh m mmm s ss sss S SS SSS EE EEEE " +
"D DD DDD F FF w www W WWW " +
"a aaa K KKK z zzzz Z ZZZZ");
StringBuffer textBuffer = new StringBuffer();
// Really, GGGG should be "Anno Domini", but the RI doesn't support that and no one cares.
textBuffer.append("AD AD 1999 99 1999 9 09 Sep September 13 13 013 ");
textBuffer.append("17 17 017 17 17 017 5 05 005 19 019 1 01 001 0 00 000 Mon Monday ");
textBuffer.append("256 256 256 2 02 38 038 3 003 ");
textBuffer.append("PM PM 5 005 GMT-5 GMT-05:00 -0500 GMT-05:00");
// to avoid passing the huge StringBuffer each time.
super.text = textBuffer.toString();
// test if field positions are set correctly for these fields occurring
// multiple times.
t_FormatWithField(0, format, date, null, Field.ERA, 0, 2);
t_FormatWithField(1, format, date, null, Field.YEAR, 6, 10);
t_FormatWithField(2, format, date, null, Field.MONTH, 19, 20);
t_FormatWithField(3, format, date, null, Field.DAY_OF_MONTH, 38, 40);
t_FormatWithField(4, format, date, null, Field.HOUR_OF_DAY1, 48, 50);
t_FormatWithField(5, format, date, null, Field.HOUR_OF_DAY0, 58, 60);
t_FormatWithField(6, format, date, null, Field.HOUR1, 68, 69);
t_FormatWithField(7, format, date, null, Field.MINUTE, 77, 79);
t_FormatWithField(8, format, date, null, Field.SECOND, 84, 85);
t_FormatWithField(9, format, date, null, Field.MILLISECOND, 93, 94);
t_FormatWithField(10, format, date, null, Field.DAY_OF_WEEK, 102, 105);
t_FormatWithField(11, format, date, null, Field.DAY_OF_YEAR, 113, 116);
t_FormatWithField(12, format, date, null, Field.DAY_OF_WEEK_IN_MONTH, 125, 126);
t_FormatWithField(13, format, date, null, Field.WEEK_OF_YEAR, 130, 132);
t_FormatWithField(14, format, date, null, Field.WEEK_OF_MONTH, 137, 138);
t_FormatWithField(15, format, date, null, Field.AM_PM, 143, 145);
t_FormatWithField(16, format, date, null, Field.HOUR0, 151, 152);
t_FormatWithField(17, format, date, null, Field.TIME_ZONE, 157, 162);
// test fields that are not included in the formatted text
t_FormatWithField(18, format, date, null, NumberFormat.Field.EXPONENT_SIGN, 0, 0);
// test with simple example
format.applyPattern("h:m z");
super.text = "5:19 GMT-5";
t_FormatWithField(21, format, date, null, Field.HOUR1, 0, 1);
t_FormatWithField(22, format, date, null, Field.MINUTE, 2, 4);
t_FormatWithField(23, format, date, null, Field.TIME_ZONE, 5, 10);
// test fields that are not included in the formatted text
t_FormatWithField(24, format, date, null, Field.ERA, 0, 0);
t_FormatWithField(25, format, date, null, Field.YEAR, 0, 0);
t_FormatWithField(26, format, date, null, Field.MONTH, 0, 0);
t_FormatWithField(27, format, date, null, Field.DAY_OF_MONTH, 0, 0);
t_FormatWithField(28, format, date, null, Field.HOUR_OF_DAY1, 0, 0);
t_FormatWithField(29, format, date, null, Field.HOUR_OF_DAY0, 0, 0);
t_FormatWithField(30, format, date, null, Field.SECOND, 0, 0);
t_FormatWithField(31, format, date, null, Field.MILLISECOND, 0, 0);
t_FormatWithField(32, format, date, null, Field.DAY_OF_WEEK, 0, 0);
t_FormatWithField(33, format, date, null, Field.DAY_OF_YEAR, 0, 0);
t_FormatWithField(34, format, date, null, Field.DAY_OF_WEEK_IN_MONTH, 0, 0);
t_FormatWithField(35, format, date, null, Field.WEEK_OF_YEAR, 0, 0);
t_FormatWithField(36, format, date, null, Field.WEEK_OF_MONTH, 0, 0);
t_FormatWithField(37, format, date, null, Field.AM_PM, 0, 0);
t_FormatWithField(38, format, date, null, Field.HOUR0, 0, 0);
t_FormatWithField(39, format, date, null, NumberFormat.Field.EXPONENT, 0, 0);
// test with simple example with pattern char Z
format.applyPattern("h:m Z z");
super.text = "5:19 -0500 GMT-5";
t_FormatWithField(40, format, date, null, Field.HOUR1, 0, 1);
t_FormatWithField(41, format, date, null, Field.MINUTE, 2, 4);
t_FormatWithField(42, format, date, null, Field.TIME_ZONE, 5, 10);
}
private Vector<FieldContainer> getDateVector4() {
Vector<FieldContainer> v = new Vector<FieldContainer>();
// "AD AD 1999 99 1999 9 09 Sep September 13 13 013 17 17 017 17 17 017 5
// 05
// 005 19 019 1 01 001 0 00 000 Mon Monday 256 256 256 2 02 38 038 3 003
// PM
// PM 5 005 EDT Eastern Daylight Time -0400 -0400"
v.add(new FieldContainer(0, 2, Field.ERA));
v.add(new FieldContainer(3, 5, Field.ERA));
v.add(new FieldContainer(6, 10, Field.YEAR));
v.add(new FieldContainer(11, 13, Field.YEAR));
v.add(new FieldContainer(14, 18, Field.YEAR));
v.add(new FieldContainer(19, 20, Field.MONTH));
v.add(new FieldContainer(21, 23, Field.MONTH));
v.add(new FieldContainer(24, 27, Field.MONTH));
v.add(new FieldContainer(28, 37, Field.MONTH));
v.add(new FieldContainer(38, 40, Field.DAY_OF_MONTH));
v.add(new FieldContainer(41, 43, Field.DAY_OF_MONTH));
v.add(new FieldContainer(44, 47, Field.DAY_OF_MONTH));
v.add(new FieldContainer(48, 50, Field.HOUR_OF_DAY1));
v.add(new FieldContainer(51, 53, Field.HOUR_OF_DAY1));
v.add(new FieldContainer(54, 57, Field.HOUR_OF_DAY1));
v.add(new FieldContainer(58, 60, Field.HOUR_OF_DAY0));
v.add(new FieldContainer(61, 63, Field.HOUR_OF_DAY0));
v.add(new FieldContainer(64, 67, Field.HOUR_OF_DAY0));
v.add(new FieldContainer(68, 69, Field.HOUR1));
v.add(new FieldContainer(70, 72, Field.HOUR1));
v.add(new FieldContainer(73, 76, Field.HOUR1));
v.add(new FieldContainer(77, 79, Field.MINUTE));
v.add(new FieldContainer(80, 83, Field.MINUTE));
v.add(new FieldContainer(84, 85, Field.SECOND));
v.add(new FieldContainer(86, 88, Field.SECOND));
v.add(new FieldContainer(89, 92, Field.SECOND));
v.add(new FieldContainer(93, 94, Field.MILLISECOND));
v.add(new FieldContainer(95, 97, Field.MILLISECOND));
v.add(new FieldContainer(98, 101, Field.MILLISECOND));
v.add(new FieldContainer(102, 105, Field.DAY_OF_WEEK));
v.add(new FieldContainer(106, 112, Field.DAY_OF_WEEK));
v.add(new FieldContainer(113, 116, Field.DAY_OF_YEAR));
v.add(new FieldContainer(117, 120, Field.DAY_OF_YEAR));
v.add(new FieldContainer(121, 124, Field.DAY_OF_YEAR));
v.add(new FieldContainer(125, 126, Field.DAY_OF_WEEK_IN_MONTH));
v.add(new FieldContainer(127, 129, Field.DAY_OF_WEEK_IN_MONTH));
v.add(new FieldContainer(130, 132, Field.WEEK_OF_YEAR));
v.add(new FieldContainer(133, 136, Field.WEEK_OF_YEAR));
v.add(new FieldContainer(137, 138, Field.WEEK_OF_MONTH));
v.add(new FieldContainer(139, 142, Field.WEEK_OF_MONTH));
v.add(new FieldContainer(143, 145, Field.AM_PM));
v.add(new FieldContainer(147, 149, Field.AM_PM));
v.add(new FieldContainer(151, 152, Field.HOUR0));
v.add(new FieldContainer(153, 156, Field.HOUR0));
v.add(new FieldContainer(157, 162, Field.TIME_ZONE));
v.add(new FieldContainer(163, 172, Field.TIME_ZONE));
v.add(new FieldContainer(173, 178, Field.TIME_ZONE));
v.add(new FieldContainer(179, 188, Field.TIME_ZONE));
return v;
}
@Test
public void testExecuteJobsForTwoEnginesSameAcquisition() {
// configure and build a process engine
StandaloneProcessEngineConfiguration engineConfiguration1 = new StandaloneInMemProcessEngineConfiguration();
engineConfiguration1.setProcessEngineName(getClass().getName() + "-engine1");
engineConfiguration1.setJdbcUrl("jdbc:h2:mem:activiti1");
engineConfiguration1.setJobExecutorActivate(false);
engineConfiguration1.setJobExecutor(jobExecutor);
engineConfiguration1.setDbMetricsReporterActivate(false);
ProcessEngine engine1 = engineConfiguration1.buildProcessEngine();
createdProcessEngines.add(engine1);
// and a second one
StandaloneProcessEngineConfiguration engineConfiguration2 = new StandaloneInMemProcessEngineConfiguration();
engineConfiguration2.setProcessEngineName(getClass().getName() + "engine2");
engineConfiguration2.setJdbcUrl("jdbc:h2:mem:activiti2");
engineConfiguration2.setJobExecutorActivate(false);
engineConfiguration2.setJobExecutor(jobExecutor);
engineConfiguration2.setDbMetricsReporterActivate(false);
ProcessEngine engine2 = engineConfiguration2.buildProcessEngine();
createdProcessEngines.add(engine2);
// stop the acquisition
jobExecutor.shutdown();
// deploy the processes
engine1.getRepositoryService().createDeployment()
.addClasspathResource(PROCESS_RESOURCE)
.deploy();
engine2.getRepositoryService().createDeployment()
.addClasspathResource(PROCESS_RESOURCE)
.deploy();
// start one instance for each engine:
engine1.getRuntimeService().startProcessInstanceByKey("intermediateTimerEventExample");
engine2.getRuntimeService().startProcessInstanceByKey("intermediateTimerEventExample");
Assert.assertEquals(1, engine1.getManagementService().createJobQuery().count());
Assert.assertEquals(1, engine2.getManagementService().createJobQuery().count());
Calendar calendar = Calendar.getInstance();
calendar.add(Field.DAY_OF_YEAR.getCalendarField(), 6);
ClockUtil.setCurrentTime(calendar.getTime());
jobExecutor.start();
// assert task completed for the first engine
waitForJobExecutorToProcessAllJobs(10000, 100, jobExecutor, engine1.getManagementService(), true);
jobExecutor.start();
// assert task completed for the second engine
waitForJobExecutorToProcessAllJobs(10000, 100, jobExecutor, engine2.getManagementService(), true);
Assert.assertEquals(0, engine1.getManagementService().createJobQuery().count());
Assert.assertEquals(0, engine2.getManagementService().createJobQuery().count());
}
@Test
public void testJobAddedGuardForTwoEnginesSameAcquisition() throws InterruptedException {
// configure and build a process engine
StandaloneProcessEngineConfiguration engineConfiguration1 = new StandaloneInMemProcessEngineConfiguration();
engineConfiguration1.setProcessEngineName(getClass().getName() + "-engine1");
engineConfiguration1.setJdbcUrl("jdbc:h2:mem:activiti1");
engineConfiguration1.setJobExecutorActivate(false);
engineConfiguration1.setJobExecutor(jobExecutor);
engineConfiguration1.setDbMetricsReporterActivate(false);
ProcessEngine engine1 = engineConfiguration1.buildProcessEngine();
createdProcessEngines.add(engine1);
// and a second one
StandaloneProcessEngineConfiguration engineConfiguration2 = new StandaloneInMemProcessEngineConfiguration();
engineConfiguration2.setProcessEngineName(getClass().getName() + "engine2");
engineConfiguration2.setJdbcUrl("jdbc:h2:mem:activiti2");
engineConfiguration2.setJobExecutorActivate(false);
engineConfiguration2.setJobExecutor(jobExecutor);
engineConfiguration2.setDbMetricsReporterActivate(false);
ProcessEngine engine2 = engineConfiguration2.buildProcessEngine();
createdProcessEngines.add(engine2);
// stop the acquisition
jobExecutor.shutdown();
// deploy the processes
engine1.getRepositoryService().createDeployment()
.addClasspathResource(PROCESS_RESOURCE)
.deploy();
engine2.getRepositoryService().createDeployment()
.addClasspathResource(PROCESS_RESOURCE)
.deploy();
// start one instance for each engine:
engine1.getRuntimeService().startProcessInstanceByKey("intermediateTimerEventExample");
engine2.getRuntimeService().startProcessInstanceByKey("intermediateTimerEventExample");
Calendar calendar = Calendar.getInstance();
calendar.add(Field.DAY_OF_YEAR.getCalendarField(), 6);
ClockUtil.setCurrentTime(calendar.getTime());
Assert.assertEquals(1, engine1.getManagementService().createJobQuery().count());
Assert.assertEquals(1, engine2.getManagementService().createJobQuery().count());
// assert task completed for the first engine
jobExecutor.start();
waitForJobExecutorToProcessAllJobs(10000, 100, jobExecutor, engine1.getManagementService(), false);
// assert task completed for the second engine
jobExecutor.start();
waitForJobExecutorToProcessAllJobs(10000, 100, jobExecutor, engine2.getManagementService(), false);
Thread.sleep(2000);
Assert.assertFalse(((SequentialJobAcquisitionRunnable) jobExecutor.getAcquireJobsRunnable()).isJobAdded());
Assert.assertEquals(0, engine1.getManagementService().createJobQuery().count());
Assert.assertEquals(0, engine2.getManagementService().createJobQuery().count());
}
/**
* Determine whether the provided field is the most <b>precise</b> field.
* According to the used pattern, e.g.
* <ul>
* <li>dd.mm.yyyy
* <li>MMMM yyyy
* <li>yyyy
* </ul>
* the date picker provides the panels for selecting a day, month or year
* respectively. The panel should close itself and set the selection in the
* CDateTime field when the user selects the most precise field. The
* constants from the {@link Calendar} class may be used to determine the
* most precise field:
* <ul>
* <li>{@link Calendar#YEAR} -> 1
* <li>{@link Calendar#MONTH} -> 2
* <li>{@link Calendar#DATE} -> 5
* </ul>
* e.g. the <i>highest</i> constant is the closing field.
*
* @param calendarField
* The calendar field identifying a pattern field
* @return true if the highest pattern field
*/
boolean isClosingField(int calendarField) {
// find the "highest" constant in the pattern fields
int i = Integer.MIN_VALUE;
for (Field f : field) {
i = Math.max(i, f.getCalendarField());
}
// compare the highest constant with the field
if (i == calendarField) {
return true;
}
return false;
}