DateResolver.java :  » Database-Client » SQLMinus » util » Java Open Source

Java Open Source » Database Client » SQLMinus 
SQLMinus » util » DateResolver.java
/*
 * $Author: rahul_kumar $
 * $Id: DateResolver.java,v 1.1 2004/01/03 07:47:33 rahul_kumar Exp rahul $
 * This is free software, as software should be; you can redistribute 
 * it and/or modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.

 * See LICENSE.txt for the full license covering this software/program/code.
 */
package util;
 
import java.util.*;
import java.text.*;
import util.ArrayUtil;

/**
 * Does date resolution for single and ranges of dates as per our
 * format for filtering.
 * No, it doesnt yet take in full dates.
 * @author rahul kumar <rahul_kumar@yahoo.com>
 * @see XXX
 */

public class DateResolver {

    /** ctor/constructor.
     */
    public DateResolver (){
    }


    public static void main (String args[]){ 
        System.out.println(  formatDate("d") );
        System.out.println(  formatDate("m") );
        System.out.println(  formatDate("y") );
        System.out.println(  formatDate("d-1") );
        System.out.println(  formatDate("d+1") );
        System.out.println(  formatDate("m+1") );
        System.out.println(  formatDate("y+1" ));
        System.out.println(  formatDate(":d+1" ));
        System.out.println(  formatDate("d+1:" ));
        System.out.println(  formatDate("d-1:d+1" ));
        System.out.println(  formatDate("m-1:m+1" ));
        System.out.println(  formatDate("y-1:y+1" ));
        System.out.println(  formatDate(":y+1" ));
        System.out.println(  formatDate("y+1:" ));
        System.out.println(  "\n parseToDateString()");
        System.out.println(  parseToDateString("d") );
        System.out.println(  parseToDateString("m") );
        System.out.println(  parseToDateString("y") );
        System.out.println(  parseToDateString("d-1") );
        System.out.println(  parseToDateString("d+1") );
        System.out.println(  parseToDateString("m+1") );
        System.out.println(  parseToDateString("y+1" ));

    }
        /**
     *  valid formats for date
     *  [:][dmy][+-](\d+)?[:]
     *  d, m , y
     *  d+1
     *  m+1 next month
     *  y+1 next year
     *  :d+1
     *  d+1:
     *  :y-1
     * 
     *  
    */
    public static String formatDate (String s){
        // check for single strings
        // split with :. if none forget, else make ranges
        s = s.trim();
        //System.out.print(  "==GOT:"+ s + "  ");
        // shortcut for today
        if (s.equals("d")){
            return NextDateString(Calendar.DATE, 0);
        } else 
        if (s.equals("m")){
            // shortcut for this month
            String start = StartDateString(Calendar.MONTH, 0);
            String end = EndDateString(Calendar.MONTH, 0);
            return start+":"+end;
        } else 
        if (s.equals("y")){
            // shortcut for this year
            String start = StartDateString(Calendar.YEAR, 0);
            String end = EndDateString(Calendar.YEAR, 0);
            return start+":"+end;
        } 
            
        boolean range = false;
        if (s.indexOf(':')>-1) range = true;
        if (!range){
            // single code passed
            String ret[] = parseToDateRange(s);
            if (ret[1] == null) return ret[0];
            return ret[0]+":"+ret[1];
        }
        String p[] = ArrayUtil.split(s,':');
        String start = "";
        String end = "";
        if (p[0] != null && p[0].length()>0)
            start = parseToDateString(p[0]);
        if (p.length == 2)
            if (p[1] != null && p[1].length()>0)
                end = parseToDateString(p[1]);
        return start+":"+end;
    }
    public static String[] parseToDateRange(String dcode){
        // now that this is called externally too, i need a sanity check
        //System.out.println(  "inside parseToDateRange:"+dcode+"]");
        if (dcode.length()==1)
            dcode = dcode + "+0";

        String speriod;
        if (dcode.charAt(1)=='+')
            speriod = dcode.substring(2);
        else
            speriod = dcode.substring(1);
        int period = Integer.parseInt(speriod);
        char sunit = dcode.charAt(0);
        int unit = toUnit(sunit);
        //System.out.println(  speriod +":"+sunit+":"+unit+":"+"");
        String ret[] = new String[2];
        ret[0] = StartDateString(unit, period);
        if (unit != Calendar.DATE)
            ret[1] = EndDateString(unit, period);
        //System.out.println(  ret[0]+" : "+ret[1]);
                
        return ret;
    }
 
    /** takes a user entered code such as "m+1" or "y-1" or "d+1" and
     * returns the date string for that.
     */
    public static String parseToDateString(String dcode){
        //System.out.println(  "inside parseToDateString:"+dcode);
        String speriod;
        //treat a shortcut
        if (dcode.equals("=")) dcode = "d";
        // dirty but will work for now
        if (dcode.length()==1) dcode = dcode + "+0";
        // hack since parse fails if a + is encountered
        if (dcode.charAt(1)=='+')
            speriod = dcode.substring(2);
        else
            speriod = dcode.substring(1);
        // how many months, years, days to add
        int period = Integer.parseInt(speriod);
        char sunit = dcode.charAt(0);
        int unit = toUnit(sunit);
        return NextDateString(unit, period);
    }
    public static int toUnit (char c){
        if (c == 'm') return Calendar.MONTH;
        if (c == 'y') return Calendar.YEAR;
        if (c == 'd') return Calendar.DATE;
        return Calendar.DATE;
    }
    public static String NextDateString(int unit, int period)
    {
        java.util.Date mydate = new java.util.Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(mydate);
        cal.add(unit, period);
        mydate = cal.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat ("yyyy'-'MM'-'dd");
        String dateString = sdf.format(mydate);
        return dateString;
    }
    public static String StartDateString(int unit, int period)
    {
        java.util.Date mydate = new java.util.Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(mydate);
        cal.add(unit, period);
        if (unit == Calendar.YEAR)
            cal.set(Calendar.MONTH, 0);
        if (unit == Calendar.YEAR || unit == Calendar.MONTH)
            cal.set(Calendar.DATE, 1);

        mydate = cal.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat ("yyyy'-'MM'-'dd");
        String dateString = sdf.format(mydate);
        return dateString;
    }
    public static String EndDateString(int unit, int period)
    {
        java.util.Date mydate = new java.util.Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(mydate);
        cal.add(unit, period);
        if (unit == Calendar.YEAR){
            cal.add(Calendar.YEAR, 1);
            cal.set(Calendar.MONTH, 0);
        } else {
            cal.add(Calendar.MONTH, 1);
        }
        cal.set(Calendar.DATE, 1);

        mydate = cal.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat ("yyyy'-'MM'-'dd");
        String dateString = sdf.format(mydate);
        return dateString;
    }
    public static String getDBString(String s, Map htParams){
        return htParams.get("fs_date") + s +  htParams.get("fe_date");
    }



    ////// START INSTANCE VARIABLES //////

    ////// START CONSTANTS AND CLASS LEVEL VARIABLES  //////
    static final String P = "DateResolver"; // used in exception strings

} // end of class
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.