/* XBN Java: Generically useful, non-GUI Java code. http://sourceforge.net/projects/xbnjava Copyright (C) 1997-2003, Jeff Epstein All rights reserved. Modifications: No Redistribution in binary form, with or without modifications, are permitted provided that the following conditions are met: * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * If modifications are made to source code then this license should indicate that fact in the "Modifications" section above. * Neither the author, nor the contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. [NOTE: This license contains NO advertising clause.] */ package xbn.output; import java.text.SimpleDateFormat; import java.util.Date; /**

A MsgFormat that precedes every line of output with a date-time stamp.

Source code:  MFDate.java

Example

Create a date-time stamp with two spaces at the end, and configure it to the Eastern Standard time zone. Then feed the SimpleDateFormat object into this class, for subseqent use in OConfig.
SimpleDateFormat sdf = (new SimpleDateFormat("MM-dd-yyyy HH:mm:ss  "));
sdf.setTimeZone(TimeZone.getTimeZone("EST"));
MFDate MFDate = new MFDate(sdf);
For further information, see Sun's documentation for java.text.SimpleDateFormat and java.text.TimeZone. @version 0.9b @author Jeff Epstein, http://sourceforge.net/projects/xbnjava. **/ public class MFDate extends MsgFormat { private SimpleDateFormat sdf = null; private boolean bIndentAfterDate = false; /**

Create a MFDate with the default SimpleDateFormat.

Equal to MFPlain(new SimpleDateFormat("MM-dd-yyyy HH:mm:ss "))

**/ public MFDate() { this(new SimpleDateFormat("MM-dd-yyyy HH:mm:ss ")); } /**

Create a MFDate.

Equal to MFPlain(sd_format, null)

**/ public MFDate(SimpleDateFormat sd_format) { this(sd_format, null, true); } /**

Create a MFDate.

Equal to MFPlain(sd_format, s_indent, true)

**/ public MFDate(SimpleDateFormat sd_format, String s_indent) { this(sd_format, s_indent, true); } /**

Create a MFDate.

@param sd_format The SimpleDateFormat object. See the documentation at the top of this page for an example. May not be null. @param s_indent Passed directly to MsgFormat constructor. @param b_indentAfterDate Should the indent text be put before or after the date stamp? It looks cooler when after. **/ public MFDate(SimpleDateFormat sd_format, String s_indent, boolean b_indentAfterDate) { super(s_indent); if(sd_format == null) { throw new NullPointerException("ERROR in MFDate.constructor: 'sd_format' is null."); } sdf = sd_format; setIndentAfterDate(b_indentAfterDate); } /**

Get the formatted message.

@return If both b_useIndent and hasIndent equal true and isIndentAfterDate equals...
Otherwise: Return getDateStamp() + s_message **/ public String getFormatted(boolean b_useIndent, String s_message) { if(b_useIndent && hasIndent()) { if(isIndentAfterDate()) { return getDateStamp() + getIndent() + s_message; } else { return getIndent() + getDateStamp() + s_message; } } //There is no indent. return getDateStamp() + s_message; } /**

Declare if the indent should go after the date stamp, or before.

@param b_indentAfterDate If true, then the indent text (if hasIndent equals true) will be printed after the date stamp. If false, the indent text will be printed before the date stamp. **/ public final void setIndentAfterDate(boolean b_indentAfterDate) { bIndentAfterDate = b_indentAfterDate; } /**

Will the indent be printed after or before the date stamp?

@return true If the indent text (only when hasIndent equals true) will be printed after the date stamp.
false If the indent text will be printed before the date stamp. **/ public boolean isIndentAfterDate() { return bIndentAfterDate; } /**

Get the date prefix as a string.

@return getSimpleDateFormat().format(new Date())

**/ public String getDateStamp() { return getSimpleDateFormat().format(new Date()); } /**

Get the SimpleDateFormat for direct manipulation.

**/ public SimpleDateFormat getSimpleDateFormat() { return sdf; } }