001    package jp.osdl.jbento2;
002    
003    import java.text.ParseException;
004    import java.text.SimpleDateFormat;
005    import java.util.Calendar;
006    import java.util.Date;
007    
008    public class StateDateFormatter {
009    
010        public static int DATE = Calendar.DATE;
011    
012        private SimpleDateFormat formatter = null;
013    
014        private Date previousTime = null;
015    
016        private int addValue = 0;
017    
018        private int addedField = 0;
019    
020        private Calendar calendar = Calendar.getInstance();
021    
022        public StateDateFormatter(String formatString, int addedField) {
023            formatter = new SimpleDateFormat(formatString);
024            this.addedField = addedField;
025        }
026    
027        /**
028         * If str is earlier than previous str, increment addedField and return
029         * incremented Date.
030         * 
031         * @param str date format.
032         * @return calculated date
033         * @throws ParseException
034         */
035        public Date nextDate(String str) throws ParseException {
036            Date date = formatter.parse(str);
037            if (previousTime != null) {
038                if (previousTime.compareTo(date) > 0) {
039                    addValue++;
040                }
041            }
042            previousTime = date;
043    
044            if (addValue != 0) {
045                calendar.setTime(date);
046                calendar.add(addedField, addValue);
047                date = calendar.getTime();
048            }
049            return date;
050        }
051    
052    }