[ Back | Previous | Next ]

Creating your own DataFormate for formatting and parsing Date Strings?

Package:
java.text.*
Product:
JDK
Release:
1.1.x
Related Links:
DateFormat
DecimalFormat
NumberFormat
SimpleDateFormat
Comment:
The DateFormats in for example
logfile is different from application to application or webprovider to
webprovider. Even with de SimpleDateFormat tools in java.text.* it is hard
to match the pattern that we sometimes get. What is the solution?

Building your own DateFormat routine is not difficult therefor we leave you with my MyDateFormat class extended form the SimpleDateFormat class

//
// MyDataFormat.java
//

package nl.rotterdam.port.log;

import java.util.*;
import java.text.*;
 /**
  * A generic dataformatting class which multiple date formats can decoded and encoded.
  * The format string has the following special characters for syntax:
  * 
  * 
  * 
  * 
  * 
  * 
  * 
  * 
  * 
  * 
  * 
  * 
  * 
  * 
  * 
SymbolMeaningPresentationExample
Yyear (Number) 1996
Mmonth in year (Text) Jul
Nmonth in year (Number) 07
Dday in month (Number) 10
dweekday (Text)Tue
hhour in day (0~23) (Number) 0
mminute in hour (Number) 30
ssecond in minute(Number) 55
ZGMT zone (Text) GMT
HGMT plus/minus sign (Text) + or -
HGMT hour (Number) 01
IGMT minutes (Number) 00
* */ public class MyDateFormat extends SimpleDateFormat { private Calendar c = null; /** * @param pattern a string with format according to the syntax above */ public MyDateFormat(String pattern) { super( pattern ); this.setTimeZone( new SimpleTimeZone( +2 *60*60*1000, "GMT" )); } public StringBuffer format (Date date, StringBuffer sb, FieldPosition fp) { this.c = Calendar.getInstance(); c.setTime( date ); StringCharacterIterator sci = new StringCharacterIterator( toPattern() ); String result = ""; for (char ch = sci.first(); ch != CharacterIterator.DONE; ch = sci.next()) { result += processChar(ch); //System.out.println( "" + ch + " = " + result ); } return new StringBuffer(result); } private String processChar(char ch) { String zeros = "0000"; //String day = zeros.substring(DAY_OF_WEEK_IN_MONTH_FIELD); DecimalFormat df2 = new DecimalFormat("00") ; DecimalFormat df4 = new DecimalFormat("0000"); MessageFormat mf3 = new MessageFormat("xxx"); if ( ch== 'D' ) { // day return "" + df2.format(c.get(c.DAY_OF_MONTH)); } else if ( ch=='M') { //month String [] months = getDateFormatSymbols().getShortMonths(); return "" + months[c.get(c.MONTH)]; } else if ( ch=='Y') { //year return df4.format(c.get(c.YEAR)); } else if ( ch=='h') { //hours return df2.format(c.get(c.HOUR)); } else if ( ch=='m') { //minutes return df2.format(c.get(c.MINUTE)); } else if ( ch=='s') { //seconds return df2.format(c.get(c.SECOND)); } else if ( ch=='d') { //days in string String [] weekdays = getDateFormatSymbols().getShortWeekdays(); return "" + weekdays[ c.get(c.MONTH) ]; } else if ( ch=='N') { //months in number return df2.format(c.get(c.MONTH)); } else if ( ch=='Z') { //GMT Zoners return getTimeZone().getID(); } else if ( ch=='H') { //GMT Hours int ii = getTimeZone().getRawOffset()/(60*60*1000); return df2.format(ii); } else if ( ch=='p') { //GMT Hours int ii = getTimeZone().getRawOffset()/(60*60*1000); return (ii > 0 ? "+" : "-"); } else if ( ch=='I') { //GMT Minutes int ii = getTimeZone().getRawOffset()/(60*60*1000); int im = (getTimeZone().getRawOffset() - ii*60*60*1000)/(60*1000) ; return df2.format(im); } else return "" + ch; } public Date parse( String text) { StringCharacterIterator sci = new StringCharacterIterator( toPattern() ); long result = 0; Number month= new Integer(0),day= new Integer(0), year=new Integer(0), hour= new Integer(0), min=new Integer(0), sec=new Integer(0), gmthrs= new Integer(0), gmtmin= new Integer(0); String zone = "", dow = "", plm =""; DecimalFormat df2 = new DecimalFormat("00"); DecimalFormat df4 = new DecimalFormat("0000"); SimpleDateFormat sdf = new SimpleDateFormat("MMM"); System.out.println("\n"); for (char ch = sci.first(); ch != CharacterIterator.DONE; ch = sci.next()) { try { //deprocessChar(char ch) if ( ch== 'D' ) { // day day = df2.parse( text.substring(0,2) ); text = text.substring(2, text.length() ); dbg( "day=" + day + text ); } else if ( ch=='d') { //day of the week dow = text.substring(0,3); text = text.substring(3, text.length() ); dbg( "dow=" + dow ); } else if ( ch=='M') { //month month = new Integer( sdf.parse( text.substring(0,3), new ParsePosition(0) ).getMonth() ); text = text.substring(3, text.length() ); dbg( "month=" + month + text ); } else if ( ch=='Y') { //year year = df4.parse( text.substring(0,4) ); text = text.substring(4, text.length() ); dbg( "yr=" + year + text ); } else if ( ch=='h') { //hours hour = df2.parse( text.substring(0,2) ); text = text.substring(2, text.length() ); } else if ( ch=='m') { //minutes min = df2.parse( text.substring(0,2) ); text = text.substring(2, text.length() ); } else if ( ch=='s') { //seconds sec = df2.parse( text.substring(0,2) ); text = text.substring(2, text.length() ); } else if ( ch=='p') { //plusminus plm = text.substring(0,1); text = text.substring(1, text.length() ); dbg("plm = " + plm ); } else if ( ch=='Z') { //GMT Zoners zone = text.substring(0,3); text = text.substring(3, text.length() ); } else if ( ch=='H') { //GMT Hours gmthrs = df2.parse( text.substring(0,2) ); text = text.substring(2, text.length() ); } else if ( ch=='I') { //GMT Minutes gmtmin = df2.parse( text.substring(0,2) ); text = text.substring(2, text.length() ); } else { text = text.substring(1,text.length()); } } catch (Exception e) { //format error e.printStackTrace(); System.exit(1); } //System.out.println( "" + ch + " = " + new Date(result) ); } return new Date(year.intValue()-1900, month.intValue(), day.intValue(), hour.intValue(), min.intValue(), sec.intValue()); } public Date parse( String text, ParsePosition pp) { return new Date(); } private void dbg(String m) {/*System.out.println(m);*/ } public static void main( String [] args ) { MyDateFormat qdf = new MyDateFormat("d, D M Y h:m:s Z"); qdf.format( new Date( System.currentTimeMillis() ), null,null ); } }
1