Skip to Content mrc logo mrc logo
  • What is m-Power?
    Overview Demos Build Process Case Studies Specs Pricing Trial
    m-Power Resources
    Overview How-To Videos Webinars & Podcasts White Papers Fact Sheets
  • What does m-Power build?
    Solutions by Industry
    Overview Database Front-Ends AI Reporting CRM Systems Business Intelligence Dashboards Inventory Management Mobile Apps ERP Enhancements Modernization
    Spreadsheets to the Web MS Access to the Web B2B/Web Portals Scheduling Embedded Analytics Web Forms Workflow Data Exploration Budgeting & Forecasting APIs & Web Services Db2 Web Query Alternative
    Solutions by Industry
    Overview Manufacturing Government Foodservice Software Vendors Logistics & Supply Chain Software Consultants Healthcare
  • Development Services Training Mentoring
  • Overview Partners Press Releases Careers Events Contact Blog
  • Support Home FAQ Documentation Customer Portal Enhancements Updates Roadmap Techblog
Try m-Power

m-Power Manual

Browse:

  • Home
  • General
  • JDEDateConvert Source Code
Back to Manual

JDEDateConvert Source Code

 

package com.abc_co.mrcextension;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.mrc.ext.FieldConvertAbstract;

/**
 *  Sample class to convert display value to db value and vice versa.
 *  (cyynnn <--> mmddyy)
 *
 * (1) Custom class must extend com.mrc.ext.FieldConvertAbstract
 *     (which is in mrcapps.jar)
 * (2) Implement method dbValue(String s) to Convert display string to DB value
 * (3) Implement method customNumber(BigDecimal value) to convert db value
 *     to display value.
 * (4) Implement editInput() to edit input value.
 * (5) If you are converting a character field, Implement method
 *     customString(String value) to convert db value to display value.
 * (6) Make a jar file and put it in folder m-power/mrcjava/WEB-INF/lib.
 * (7) Attach this class to the field you want to convert in
 *     Update Dictionary Field screen.
 *
 */
public class JDEDateConvert extends  FieldConvertAbstract {

    /** Default cyynnn value for incorrect input */
    static String dftVal = "99999";
    /*********************************************************************
     * Convert display string mmddyy to DB value cyynnn
     *********************************************************************/
    public String dbValue(String dspstr) {
        if (dspstr == null || dspstr.toUpperCase().indexOf("*BLANK")>=0) {
            return "0";
        }

        //if passed as mm/dd/yy instead of mmddyy
        if (dspstr.indexOf("/") > 0) {
            String msg = check(dspstr);
            if (msg != null) {
                return dftVal;
            }
            dspstr = dspstr.replaceAll("/", "");
        }

        String str = dspstr;
        int year = 0;
        String cyynnn = "";
        if (str.length() == 6) { //mmddyy
            String mm = str.substring(0,2);
            String dd = str.substring(2,4);
            String yy = str.substring(4);
            cyynnn = cyynnn(yy, mm, dd);
        } else     if (str.length() == 5){ //mddyy
            String mm = str.substring(0,1);
            String dd = str.substring(1,3);
            String yy = str.substring(3);
            year = Integer.parseInt(yy);
            cyynnn = cyynnn(yy, mm, dd);
        } else {
            return dftVal;
        }
        return cyynnn;
    }

    /*********************************************************************
     * Convert DB value cyynnn to mmddyy
     *********************************************************************/
    public BigDecimal customNumber(BigDecimal value) {

        String cyynnn = "" + value.intValue();
        int year = 0;
        int day = 0;
        String yy = "";
        if (cyynnn.length() == 6) {
            yy = cyynnn.substring(1,3);
            String nnn = cyynnn.substring(3);
            year = 2000 + Integer.parseInt(yy);
            day = Integer.parseInt(nnn);
        } else if (cyynnn.length() == 5) {
            yy = cyynnn.substring(0,2);
            String nnn = cyynnn.substring(2);
            year = 1900 + Integer.parseInt(yy);
            day = Integer.parseInt(nnn);
        } else {
            return value;
        }

        Calendar cal = Calendar.getInstance();
        cal.clear();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.DAY_OF_YEAR, day);
        int dd = cal.get(Calendar.DAY_OF_MONTH);
        int mm = cal.get(Calendar.MONTH) + 1;
        String mms = mm > 9 ? "" + mm : "0" + mm;
        String dds = dd > 9 ? "" + dd : "0" + dd;
        String mmddyy = mms + dds + yy;
        BigDecimal valuenew = new BigDecimal(mmddyy);

        return valuenew;
    }

    /*********************************************************************
     * Format database value for display.
     * Return null to default to mrc format
     *********************************************************************/
    public String formatDisplay(BigDecimal value) {
        return null;
    }

    /*********************************************************************
     * Make cyynnn from yy mm dd
     *********************************************************************/
    String cyynnn(String yy, String mm, String dd) {

        //if mm dd yy not valid, pass back 99999
        String mmddyy = mm + "/" + dd + "/" + yy;
        if (editInput(mmddyy, "EQ") != null) {
            return dftVal;
        }

        int ddi =  Integer.parseInt(dd);
        int mmi =  Integer.parseInt(mm) - 1;
        int yyi =  Integer.parseInt(yy);
        yyi = yyi > 70 ? 1900 + yyi : 2000 + yyi;
        Calendar cal = Calendar.getInstance();
        cal.clear();
        cal.set(yyi, mmi, ddi);
        int day = cal.get(Calendar.DAY_OF_YEAR);
        String days = "" + day;
        if (day < 10) {             days = "00" + day;         } else if (day < 100) {             days = "0" + day;         }         String c = yyi < 2000 ? "" : "1"; //only good for 60 years!         String cyynnn = c + yy + days;         return cyynnn;     }     /*********************************************************************      * Edit input field value      * value - s tring of the input      * rls - relation code (EQ, LS, CT etc.)      * Return null if input is Ok or return the error message.      *      * Edit if input is in mm/dd/yy format      *********************************************************************/     public String editInput(String value, String rls) {         String msg = null;         if (value == null  ) {             return "invalid input";         }         String[] vals = value.split(" ");         if ((rls.equals("RG") || rls.equals("NR")) && vals.length != 2) {             return "RG or NR require 2 values";         }         //for these for do not need to edit for mm/dd/yy         //if (rls.equals("CT") || rls.equals("CA") || rls.equals("CO") ||         //rls.equals("SW")) {         //    return null; //         //}         //These relations require single value         if (vals.length > 1 && (rls.equals("EQ") || rls.equals("NE")
             ||  rls.equals("LT") ||  rls.equals("GT") ||  rls.equals("LE")
             ||  rls.equals("GE") ||  rls.equals("CT") ||  rls.equals("SW"))) {
            return "Enter one value for relation " + rls;
        }

        Date[] date2 = new Date[2]; //used for check range

        for (int i = 0; i < vals.length; i++) {             String val = vals[i];             msg = check(val);             if (msg != null) { //has error                 break;             }             if (i < 2) { //only store 0, 1                  try {                     String date = addSlash(val);                     date2[i] =  (Date)formatter.parse(date);                 } catch (ParseException e) {                     e.printStackTrace();                 }             }         }         //check for range         if (msg == null && (rls.equals("RG") || rls.equals("NR"))) {             if (date2[1].compareTo(date2[0]) < 0) {                 msg = "First date must be smaller than the second";             }         }         return msg;     }     /*********************************************************************      * Check string of mm/dd/yy is a valid date      *********************************************************************/     public String check(String value) {         String msg = null;         if (value.indexOf("/") < 0) {             msg = checkNoSlash(value);             msg = msg == null ? msg : "Invalid input: " + value;         } else {             msg = check1(value);         }         return msg;     }     /*********************************************************************      * Check string of mm/dd/yy is a valid date      *********************************************************************/     public String check1(String value) {         String msg = checkBasic(value);         if(msg == null){               msg = checkDate(value);  //then check is true date 12/22/09         }         return msg;     }     /*********************************************************************      * Check date must be nn/nn/nn      *********************************************************************/     String expression = "^\\d?\\d/\\d{2}/\\d{2}$";  //0 or 1 d + d/dd/dd     public String checkBasic(String value) {         String msg = null;         CharSequence inputStr = value;         Pattern pattern = Pattern.compile(expression);         Matcher matcher = pattern.matcher(inputStr);         if(!matcher.matches()){           return "Invalid input: " + value;         }         return msg;     }     /*********************************************************************      * Check date must be a good date      *********************************************************************/     DateFormat formatter = new SimpleDateFormat("MM/dd/yy");     public String checkDate(String val) {         String msg = null;         formatter.setLenient(false);         try {             Date date = (Date)formatter.parse(val);         } catch (ParseException e) {             msg = "Invalid input: " + val;         }         return msg;     }     /*********************************************************************      * Check input that has no slash.      *********************************************************************/     public String checkNoSlash(String value) {         String str = value.trim();         if (str.length() != 5 && str.length() != 6) {             return "Invalid input: " + value;         }         String date = addSlash(str);         String msg = check1(date);         return msg;     }     /*********************************************************************      *Add slash      *********************************************************************/     public String addSlash(String str) {         if (str.indexOf("/") > 0) {
            return str;
        }
        String date = "";
        if (str.length() == 6) { //mmddyy
            date = str.substring(0,2) + "/" + str.substring(2,4) +  "/"
             + str.substring(4);
        } else     if (str.length() == 5){ //mddyy
            date = str.substring(0,1) +  "/" + str.substring(1,3) +  "/"
             + str.substring(3);
        }
        return date;
    }

    /**
     * Test
     */
    public static void main(String[] args) {
        JDEDateConvert test = new JDEDateConvert();

        BigDecimal bd = new BigDecimal(111365);
        BigDecimal bd0 = test.customNumber(bd );
        System.out.println(111365 + " ---> " + bd0.intValue());
        String dsp = "122298";
        System.out.println(dsp + " d---> " + test.dbValue(dsp));
        dsp = "122211";
        System.out.println(dsp + " d---> " + test.dbValue(dsp));
        dsp = "122201";
        System.out.println(dsp + " d---> " + test.dbValue(dsp));
        dsp = "12298";
        System.out.println(dsp + " d---> " + test.dbValue(dsp));
        dsp = "11/22/08";
        System.out.println(dsp + " d---> " + test.dbValue(dsp));
        dsp = "21106";
        System.out.println(dsp + " ---> " + test.editInput(dsp, "EQ"));
        dsp = "12/11/06";
        System.out.println(dsp + " ---> " + test.editInput(dsp, "EQ"));

        dsp = "21106 122206";
        System.out.println(dsp + " ---> " + test.editInput(dsp, "RG"));
    }

}

Created: January 31, 2011 | Modified: December 27, 2011

Search


Browse By Category

Build Process (13)
Starting with m-Power (8)
Retrievals (10)
Reports (15)
Summaries (4)
Maintainers (17)
Graphs (8)
m-Power Data Explorer (4)
General (24)
Calculations (5)
Utilities (9)
m-Power Administration (23)
Security (11)
Freemarker (6)
m-Painter (29)
Form Validation (5)
External Objects & UDFs (12)
Deprecated Documentation (23)
Bootstrap Templates (7)

Popular Tags

Maintainers Email Administration Summaries SQL External Objects Compiling Popular Getting Started DB2 Record Selections Freemarker Bootstrap Templates Maintainer Calculations Tomcat Performance Dates Advanced mrc-Productivity Series Graphs Build Process Java Security m-Painter Graphing Dropdowns App Properties Retrieval Video Admin Excel Form Validation Application Properties Production Prompt Screens Retrievals Bar Graphs Data Dictionary Report Graph Properties RPG Reports Parameters Database

See all tags »

mrc

Build the custom apps and AI tools your business needs with m-Power, or have our Chicago-based team build them with you. One platform, one team that makes sure you succeed.

Since 1981 · 1,500+ customers · 4.7/5 on G2

m-Power
Overview Demos Build Process Licensing Specs Get a Demo
Solutions
Database Front-Ends Reporting AI Tools Dashboards ERP Enhancements Modernization See all solutions
Company
Case Studies Services About Blog Careers Contact Support

Privacy Policy Cookie Policy Cookie Settings

michaels, ross & cole, ltd. (mrc)
US: 2001 Midwest Road, Suite 310, Oak Brook, IL 60523 · 630-916-0662
UK: Mortlake Business Centre, 20 Mortlake High Street, London, SW14 8JN · +44-20-335-59566
© mrc. All rights reserved.