001/*
002 * jPOS Project [http://jpos.org]
003 * Copyright (C) 2000-2026 jPOS Software SRL
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jpos.util;
020
021import java.text.DateFormat;
022import java.text.ParseException;
023import java.text.SimpleDateFormat;
024import java.util.Calendar;
025import java.util.Date;
026import java.util.GregorianCalendar;
027import java.util.Locale;
028import java.util.TimeZone;
029
030public class DateUtil {
031    static SimpleDateFormat dfDate = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.SHORT, Locale.US);
032    static SimpleDateFormat dfDateTime = (SimpleDateFormat) DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, Locale.US);
033
034    static SimpleDateFormat dfDate_mmddyyyy = new SimpleDateFormat("MM/dd/yyyy");
035    static SimpleDateFormat dfDate_yyyymmdd = new SimpleDateFormat("yyyyMMdd");
036    static SimpleDateFormat dfDateTime_mmddyyyy = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
037    
038    static SimpleDateFormat dfDate_mmddyy = new SimpleDateFormat("MM/dd/yy");
039    public static Date parseDate (String s) throws ParseException {
040        if (s == null)
041            return null;
042        return dfDate.parse (s);
043    }
044    public static Date parseDate_mmddyyyy (String s) throws ParseException {
045        if (s == null)
046            return null;
047        return dfDate_mmddyyyy.parse (s);
048    }
049    public static Date parseDate_yyyymmdd (String s) throws ParseException {
050        if (s == null)
051            return null;
052        return dfDate_yyyymmdd.parse (s);
053    }
054    public static Date parseDate_mmddyy (String s) throws ParseException {
055        if (s == null)
056            return null;
057        return dfDate_mmddyy.parse (s);
058    }
059    
060    public static Date parseDateTime (String s) throws ParseException {
061        if (s == null)
062            return null;
063        return dfDateTime.parse (s);
064    }
065    public static Date parseDateTime_mmddyyyy (String s) throws ParseException {
066        if (s == null)
067            return null;
068        return dfDateTime_mmddyyyy.parse (s);
069    }
070    public static Date parseTimestamp (String s) throws ParseException {
071        if (s == null)
072            return null;
073        return dfDateTime_mmddyyyy.parse (s);
074    }
075
076    public static Date parseDateTime_mmddyyyy (String s, String tzString) 
077        throws ParseException 
078    {
079        if (s == null)
080            return null;
081        DateFormat df = (DateFormat) dfDateTime_mmddyyyy.clone();
082        if (tzString != null)
083            df.setTimeZone (TimeZone.getTimeZone (tzString));
084        return df.parse (s);
085    }
086    
087    public static String dateToString (Date d) {
088        if (d == null)
089            return null;
090        return dfDate.format (d);
091    }
092    public static String dateToString_mmddyyyy (Date d) {
093        if (d == null)
094            return null;
095        return dfDate_mmddyyyy.format (d);
096    }
097    
098    public static String dateTimeToString (Date d) {
099        if (d == null)
100            return null;
101        return dfDateTime.format (d);
102    }
103    public static String dateTimeToString (Date d, String tzString) {
104        if (d == null)
105            return null;
106        DateFormat df = (DateFormat) dfDateTime.clone();
107        if (tzString != null)
108            df.setTimeZone (TimeZone.getTimeZone (tzString));
109        return df.format (d);
110    }
111    public static String dateTimeToString_mmddyyyy (Date d) {
112        if (d == null)
113            return null;
114        return dfDateTime_mmddyyyy.format (d);
115    }
116    public static String timestamp (Date d) {
117        if (d == null)
118            return null;
119        return dfDateTime_mmddyyyy.format (d);
120    }
121    public static String postdate (Date d) {
122        if (d == null)
123            return null;
124        return dfDate_mmddyyyy.format (d);
125    }
126
127   /**
128    * @param d MMDDAA
129    * @param t HHMMSS
130    * @return Date Object
131    */
132    public static Date parseDateTime (String d, String t) {
133        Calendar cal = new GregorianCalendar();
134        Date now = new Date ();
135        cal.setTime (now);
136
137        int YY = Integer.parseInt (d.substring (4));
138        int MM = Integer.parseInt (d.substring (0, 2))-1;
139        int DD = Integer.parseInt (d.substring (2, 4));
140        int hh = Integer.parseInt (t.substring (0, 2));
141        int mm = Integer.parseInt (t.substring (2, 4));
142        int ss = Integer.parseInt (t.substring (4));
143        int century = cal.get (Calendar.YEAR) / 100;
144
145        cal.set (Calendar.YEAR, (century * 100) + YY);
146        cal.set (Calendar.MONTH, MM);
147        cal.set (Calendar.DATE, DD);
148        cal.set (Calendar.HOUR_OF_DAY, hh);
149        cal.set (Calendar.MINUTE, mm);
150        cal.set (Calendar.SECOND, ss);
151
152        //
153        // I expect this program to continue running by 2099 ... --apr@jpos.org
154        //
155        Date thisCentury = cal.getTime();
156        cal.set (Calendar.YEAR, (--century * 100) + YY);
157        Date previousCentury = cal.getTime();
158
159        if (Math.abs (now.getTime() - previousCentury.getTime()) <
160            Math.abs (now.getTime() - thisCentury.getTime()) )
161            thisCentury = previousCentury;
162        return thisCentury;
163    }
164   /**
165    * @param t HHMM[SS]
166    * @return Date Object
167    */
168    public static Date parseTime (String t) {
169        return parseTime (t, new Date());
170    }
171    public static Date parseTime (String t, Date now) {
172        Calendar cal = new GregorianCalendar();
173        cal.setTime (now);
174
175        int hh = Integer.parseInt (t.substring (0, 2));
176        int mm = Integer.parseInt (t.substring (2, 4));
177        int ss = t.length() > 4 ? Integer.parseInt (t.substring (4)) : 0;
178
179        cal.set (Calendar.HOUR_OF_DAY, hh);
180        cal.set (Calendar.MINUTE, mm);
181        cal.set (Calendar.SECOND, ss);
182
183        return cal.getTime();
184    }
185
186    public static String dateToString (Date d, String tzString) {
187        DateFormat df = (DateFormat) DateFormat.getDateInstance().clone();
188        if (tzString != null)
189            df.setTimeZone (TimeZone.getTimeZone (tzString));
190        return df.format (d);
191    }
192    public static String timeToString (Date d, String tzString) {
193        DateFormat df = (DateFormat) 
194            DateFormat.getTimeInstance(DateFormat.SHORT).clone();
195        if (tzString != null)
196            df.setTimeZone (TimeZone.getTimeZone (tzString));
197        return df.format (d);
198    }
199    public static String timeToString (Date d) {
200        return timeToString (d, null);
201    }
202    public static String toDays (long period) {
203        StringBuffer sb = new StringBuffer();
204        long hours = period / 3600000L;
205        if (hours > 0) {
206            sb.append (hours);
207            sb.append ("h");
208            period -= (hours * 3600000L);
209        }
210        long mins = period / 60000L;
211        if (mins > 0) {
212            sb.append (mins);
213            sb.append ("m");
214            period -= (mins * 60000L);
215        }
216        long secs = period / 1000L;
217        sb.append (secs);
218        sb.append ("s");
219        return sb.toString();
220    }
221}
222