Example usage for java.util Formatter format

List of usage examples for java.util Formatter format

Introduction

In this page you can find the example usage for java.util Formatter format.

Prototype

public Formatter format(String format, Object... args) 

Source Link

Document

Writes a formatted string to this object's destination using the specified format string and arguments.

Usage

From source file:net.sourceforge.msscodefactory.cflib.v1_11.CFLib.CFLibXmlUtil.java

public static String formatDate(Calendar cal) {
    final String S_ProcName = "formatDate";
    if (cal == null) {
        throw CFLib.getDefaultExceptionFactory().newNullArgumentException(CFLibXmlUtil.class, S_ProcName, 1,
                "cal");
    }/*from   w w  w.  j  av a2  s  . com*/
    StringBuffer buff = new StringBuffer();
    Formatter fmt = new Formatter(buff);
    fmt.format("%1$04d", cal.get(Calendar.YEAR));
    buff.append('-');
    fmt.format("%1$02d", cal.get(Calendar.MONTH) + 1);
    buff.append('-');
    fmt.format("%1$02d", cal.get(Calendar.DAY_OF_MONTH));
    String retval = buff.toString();
    fmt.close();
    return (retval);
}

From source file:net.sourceforge.msscodefactory.cflib.v1_11.CFLib.CFLibXmlUtil.java

public static String formatTime(Calendar cal) {
    final String S_ProcName = "formatTime";
    if (cal == null) {
        throw CFLib.getDefaultExceptionFactory().newNullArgumentException(CFLibXmlUtil.class, S_ProcName, 1,
                "cal");
    }//from w w w.  j  a  va  2s  .c o  m
    StringBuffer buff = new StringBuffer();
    Formatter fmt = new Formatter(buff);
    fmt.format("%1$02d", cal.get(Calendar.HOUR_OF_DAY));
    buff.append(':');
    fmt.format("%1$02d", cal.get(Calendar.MINUTE));
    buff.append(':');
    fmt.format("%1$02d", cal.get(Calendar.SECOND));
    String retval = buff.toString();
    fmt.close();
    return (retval);
}

From source file:org.apache.camel.component.mongodb.MongoDbOperationsTest.java

@Test
public void testRemove() throws Exception {
    // Prepare test
    assertEquals(0, testCollection.count());
    for (int i = 1; i <= 100; i++) {
        String body = null;/*ww w  .  j  a va 2  s. c om*/
        Formatter f = new Formatter();
        if (i % 2 == 0) {
            body = f.format("{\"_id\":\"testSave%d\", \"scientist\":\"Einstein\"}", i).toString();
        } else {
            body = f.format("{\"_id\":\"testSave%d\", \"scientist\":\"Einstein\", \"extraField\": true}", i)
                    .toString();
        }
        template.requestBody("direct:insert", body);
    }
    assertEquals(100L, testCollection.count());

    // Testing the update logic
    DBObject extraField = new BasicDBObject("extraField", true);
    assertEquals("Number of records with 'extraField' flag on must equal 50", 50L,
            testCollection.count(extraField));

    Object result = template.requestBody("direct:remove", extraField);
    assertTrue(result instanceof WriteResult);

    assertEquals("Number of records with 'extraField' flag on must be 0 after remove", 0,
            testCollection.count(extraField));

}

From source file:org.apache.camel.component.mongodb.MongoDbOperationsTest.java

@Test
public void testUpdate() throws Exception {
    // Prepare test
    assertEquals(0, testCollection.count());
    for (int i = 1; i <= 100; i++) {
        String body = null;/*from w w w . jav  a2  s . co m*/
        Formatter f = new Formatter();
        if (i % 2 == 0) {
            body = f.format("{\"_id\":\"testSave%d\", \"scientist\":\"Einstein\"}", i).toString();
        } else {
            body = f.format("{\"_id\":\"testSave%d\", \"scientist\":\"Einstein\", \"extraField\": true}", i)
                    .toString();
        }
        template.requestBody("direct:insert", body);
    }
    assertEquals(100L, testCollection.count());

    // Testing the update logic
    DBObject extraField = new BasicDBObject("extraField", true);
    assertEquals("Number of records with 'extraField' flag on must equal 50", 50L,
            testCollection.count(extraField));
    assertEquals("Number of records with 'scientist' field = Darwin on must equal 0", 0,
            testCollection.count(new BasicDBObject("scientist", "Darwin")));

    DBObject updateObj = new BasicDBObject("$set", new BasicDBObject("scientist", "Darwin"));

    Object result = template.requestBodyAndHeader("direct:update", new Object[] { extraField, updateObj },
            MongoDbConstants.MULTIUPDATE, true);
    assertTrue(result instanceof WriteResult);

    assertEquals("Number of records with 'scientist' field = Darwin on must equal 50 after update", 50,
            testCollection.count(new BasicDBObject("scientist", "Darwin")));

}

From source file:net.sourceforge.msscodefactory.cflib.v1_11.CFLib.CFLibXmlUtil.java

public static String formatTimestamp(Calendar cal) {
    final String S_ProcName = "formatTimestamp";
    if (cal == null) {
        throw CFLib.getDefaultExceptionFactory().newNullArgumentException(CFLibXmlUtil.class, S_ProcName, 1,
                "cal");
    }//from   www .j  a  v a2  s. co  m
    StringBuffer buff = new StringBuffer();
    Formatter fmt = new Formatter(buff);
    fmt.format("%1$04d", cal.get(Calendar.YEAR));
    buff.append('-');
    fmt.format("%1$02d", cal.get(Calendar.MONTH) + 1);
    buff.append('-');
    fmt.format("%1$02d", cal.get(Calendar.DAY_OF_MONTH));
    buff.append('T');
    fmt.format("%1$02d", cal.get(Calendar.HOUR_OF_DAY));
    buff.append(':');
    fmt.format("%1$02d", cal.get(Calendar.MINUTE));
    buff.append(':');
    fmt.format("%1$02d", cal.get(Calendar.SECOND));
    String retval = buff.toString();
    fmt.close();
    return (retval);
}

From source file:org.kalypso.kalypsomodel1d2d.sim.TelemacKalypsoSimulation.java

private File findTelemacBatch(final String exeVersionName, File tmpdir) throws CoreException {
    final File batchFile = new File(tmpdir, "runTelemacKalypso.bat"); //$NON-NLS-1$

    OutputStream batchOutStream = null;
    try {/*from  w w w .  j  av a  2  s .  c o m*/
        batchOutStream = new BufferedOutputStream(new FileOutputStream(batchFile));

        Formatter formatter = new Formatter(batchOutStream);
        formatter.format("%s", exeVersionName);
        batchFile.setExecutable(true);
        formatter.close();
        batchOutStream.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (batchFile.exists())
        return batchFile;
    // final Location installLocation = Platform.getInstallLocation();
    // final File installDir = FileUtils.toFile( installLocation.getURL() );
    //    final File exeDir = new File( installDir, "bin" ); //$NON-NLS-1$
    // final File exeFile = new File( exeDir, exeName );
    // return exeFile;

    final String exeMissingMsg = String.format(
            Messages.getString("org.kalypso.kalypsomodel1d2d.sim.SWANCalculation.26"), //$NON-NLS-1$
            batchFile.getAbsolutePath());
    throw new CoreException(StatusUtilities.createErrorStatus(exeMissingMsg));
}

From source file:net.sourceforge.msscodefactory.cflib.v1_11.CFLib.CFLibXmlUtil.java

public static String formatTZDate(Calendar cal) {
    final String S_ProcName = "formatTZDate";
    if (cal == null) {
        throw CFLib.getDefaultExceptionFactory().newNullArgumentException(CFLibXmlUtil.class, S_ProcName, 1,
                "cal");
    }//  w w w.  j  a  va  2s. co m
    StringBuffer buff = new StringBuffer();
    Formatter fmt = new Formatter(buff);
    fmt.format("%1$04d", cal.get(Calendar.YEAR));
    buff.append('-');
    fmt.format("%1$02d", cal.get(Calendar.MONTH) + 1);
    buff.append('-');
    fmt.format("%1$02d", cal.get(Calendar.DAY_OF_MONTH));
    int tzoff = cal.getTimeZone().getRawOffset() / 60000;
    if (tzoff < 0) {
        tzoff = 0 - tzoff;
        buff.append('-');
    } else {
        buff.append('+');
    }
    int tzhour = tzoff / 60;
    int tzmin = tzoff % 60;
    if (tzhour > 12) {
        fmt.close();
        throw CFLib.getDefaultExceptionFactory().newInvalidArgumentException(CFLibXmlUtil.class, S_ProcName, 0,
                "tzhour", tzhour);
    }
    fmt.format("%1$02d", tzhour);
    buff.append(':');
    fmt.format("%1$02d", tzmin);
    String retval = buff.toString();
    fmt.close();
    return (retval);
}

From source file:net.sourceforge.msscodefactory.cflib.v1_11.CFLib.CFLibXmlUtil.java

public static String formatTZTime(Calendar cal) {
    final String S_ProcName = "formatTZTime";
    if (cal == null) {
        throw CFLib.getDefaultExceptionFactory().newNullArgumentException(CFLibXmlUtil.class, S_ProcName, 1,
                "cal");
    }/*from   ww  w  .j  a v a 2s.c  o  m*/
    StringBuffer buff = new StringBuffer();
    Formatter fmt = new Formatter(buff);
    fmt.format("%1$02d", cal.get(Calendar.HOUR_OF_DAY));
    buff.append(':');
    fmt.format("%1$02d", cal.get(Calendar.MINUTE));
    buff.append(':');
    fmt.format("%1$02d", cal.get(Calendar.SECOND));
    int tzoff = cal.getTimeZone().getRawOffset() / 60000;
    if (tzoff < 0) {
        tzoff = 0 - tzoff;
        buff.append('-');
    } else {
        buff.append('+');
    }
    int tzhour = tzoff / 60;
    int tzmin = tzoff % 60;
    if (tzhour > 12) {
        fmt.close();
        throw CFLib.getDefaultExceptionFactory().newInvalidArgumentException(CFLibXmlUtil.class, S_ProcName, 0,
                "tzhour", tzhour);
    }
    fmt.format("%1$02d", tzhour);
    buff.append(':');
    fmt.format("%1$02d", tzmin);
    String retval = buff.toString();
    fmt.close();
    return (retval);
}

From source file:fll.web.scoreEntry.ScoreEntry.java

/**
 * Genrate the increment methods and variable declarations for the goals in
 * the performance element of document. Generate the methods to compute goals
 * as well./*from w w w.  j  a  va  2 s .c  o m*/
 * 
 * @throws ParseException
 */
public static void generateIncrementMethods(final Writer writer, final ServletContext application)
        throws IOException, ParseException {
    final ChallengeDescription description = ApplicationAttributes.getChallengeDescription(application);
    final PerformanceScoreCategory performanceElement = description.getPerformance();
    final Formatter formatter = new Formatter(writer);

    for (final AbstractGoal goal : performanceElement.getGoals()) {
        if (goal.isComputed()) {
            // generate the method to update the computed goal variables
            final String goalName = goal.getName();
            formatter.format("<!-- %s -->%n", goalName);
            formatter.format("var %s;%n", getVarNameForComputedScore(goalName));
            generateComputedGoalFunction(formatter, (ComputedGoal) goal);
        } else {
            final String name = goal.getName();
            final double min = goal.getMin();
            final double max = goal.getMax();
            final String rawVarName = getVarNameForRawScore(name);

            formatter.format("<!-- %s -->%n", name);
            formatter.format("var %s;%n", rawVarName);
            formatter.format("var %s;%n", getVarNameForComputedScore(name));

            // set method
            formatter.format("function %s(newValue) {%n", getSetMethodName(name));
            formatter.format("  var temp = %s;%n", rawVarName);
            formatter.format("  %s = newValue;%n", rawVarName);
            formatter.format("  if(!isConsistent()) {%n");
            formatter.format("    %s = temp;%n", rawVarName);
            formatter.format("  }%n");
            formatter.format("  refresh();%n");
            formatter.format("}%n");

            // check input method
            formatter.format("function %s() {%n", getCheckMethodName(name));
            formatter.format("  var str = document.scoreEntry.%s.value;%n", name);
            if (ScoreType.FLOAT == goal.getScoreType()) {
                formatter.format("  var num = parseFloat(str);%n");
            } else {
                formatter.format("  var num = parseInt(str);%n");
            }
            formatter.format("  if(!isNaN(num)) {%n");
            formatter.format("    %s(num);%n", getSetMethodName(name));
            formatter.format("  }%n");
            formatter.format("  refresh();%n");
            formatter.format("}%n");

            if (!goal.isEnumerated() && !goal.isYesNo()) {
                formatter.format("function %s(increment) {%n", getIncrementMethodName(name));
                formatter.format("  var temp = %s%n", rawVarName);
                formatter.format("  %s += increment;%n", rawVarName);
                formatter.format("  if(%s > %s) {%n", rawVarName, max);
                formatter.format("    %s = %s;%n", rawVarName, max);
                formatter.format("   }%n");
                formatter.format("  if(%s < %s) {%n", rawVarName, min);
                formatter.format("    %s = %s;%n", rawVarName, min);
                formatter.format("   }%n");
                formatter.format("  if(!isConsistent()) {%n");
                formatter.format("    %s = temp;%n", rawVarName);
                formatter.format("  }%n");
                formatter.format("  refresh();%n");
                formatter.format("}%n");
            }

            formatter.format("%n%n");
        }
    } // end for each goal

    // method for double-check field
    formatter.format("<!-- Verified -->%n");
    formatter.format("var Verified;%n");
    formatter.format("function %s(newValue) {%n", getSetMethodName("Verified"));
    formatter.format("  Verified = newValue;%n");
    formatter.format("  if (newValue == 1 && document.getElementsByName('EditFlag').length == 0) {");
    formatter.format("    replaceText('verification_error', 'Are you sure this score has been Verified?');");
    formatter.format("  } else if (newValue == 0) {");
    formatter.format("    replaceText('verification_error', '');");
    formatter.format("  }");
    formatter.format("  refresh();%n");
    formatter.format("}%n%n%n");
}

From source file:net.sourceforge.msscodefactory.cflib.v1_11.CFLib.CFLibXmlUtil.java

public static String formatTZTimestamp(Calendar cal) {
    final String S_ProcName = "formatTZTimestamp";
    if (cal == null) {
        throw CFLib.getDefaultExceptionFactory().newNullArgumentException(CFLibXmlUtil.class, S_ProcName, 1,
                "cal");
    }// ww  w. ja  v a  2s . co  m
    StringBuffer buff = new StringBuffer();
    Formatter fmt = new Formatter(buff);
    fmt.format("%1$04d", cal.get(Calendar.YEAR));
    buff.append('-');
    fmt.format("%1$02d", cal.get(Calendar.MONTH) + 1);
    buff.append('-');
    fmt.format("%1$02d", cal.get(Calendar.DAY_OF_MONTH));
    buff.append('T');
    fmt.format("%1$02d", cal.get(Calendar.HOUR_OF_DAY));
    buff.append(':');
    fmt.format("%1$02d", cal.get(Calendar.MINUTE));
    buff.append(':');
    fmt.format("%1$02d", cal.get(Calendar.SECOND));
    int tzoff = cal.getTimeZone().getRawOffset() / 60000;
    if (tzoff < 0) {
        tzoff = 0 - tzoff;
        buff.append('-');
    } else {
        buff.append('+');
    }
    int tzhour = tzoff / 60;
    int tzmin = tzoff % 60;
    if (tzhour > 12) {
        fmt.close();
        throw CFLib.getDefaultExceptionFactory().newInvalidArgumentException(CFLibXmlUtil.class, S_ProcName, 0,
                "tzhour", tzhour);
    }
    fmt.format("%1$02d", tzhour);
    buff.append(':');
    fmt.format("%1$02d", tzmin);
    String retval = buff.toString();
    fmt.close();
    return (retval);
}