Friday, June 18, 2021

Date format convesrions

     public void reversalDateVCE(ValueChangeEvent vce) {

           vce.getComponent().processUpdates(FacesContext.getCurrentInstance());

           try {

               Row rHdr = CommonCode.getCurrentRowByIteratorName("FaTransHdr1Iterator");

               oracle.jbo.domain.Date vchDT = (oracle.jbo.domain.Date)rHdr.getAttribute("VchDt");

               oracle.jbo.domain.Date reversalDT = (oracle.jbo.domain.Date)rHdr.getAttribute("AutoReversalDt");

               /* oracle.jbo.domain.Date thirtyDaysAfter = CommonCode.addDayToOracleDate(vchDT, 30);

              // SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/YYYY");

               System.out.println("reversaldt@ : " + reversalDT + " thirtyDaysAfter: " + thirtyDaysAfter);


               java.util.Date vchDTUtil = CommonCode.convertOracleDateToJavaUtilDate(vchDT);

               java.util.Date thirtyDaysAfterUtil = CommonCode.convertOracleDateToJavaUtilDate(thirtyDaysAfter); */

               /* if (vchDTUtil.after(thirtyDaysAfterUtil)) {

                   CommonCode.infomsg(reversalDT + " is greater than 30 days!","");

               } */

               Long daysDiff =CommonCode.getDifferenceDaysBetweenTwoDates(reversalDT,vchDT);

               System.out.println(reversalDT+ " :reversalDT - vchDT : "+vchDT + "daysDiff = "+daysDiff);

               

               if(daysDiff > 30){

                   SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

                   java.util.Date d1 = null;

                   try {

                       d1 = sdf.parse(reversalDT.toString());


                   } catch (ParseException e) {


                       e.printStackTrace();

                   }

                   SimpleDateFormat dateFormat2 = new SimpleDateFormat("dd/MMM/yyyy");


                   String date = dateFormat2.format(d1);

                   CommonCode.infomsg(date+ " is greater than 30 days!","");

               }

           } catch (NullPointerException npe) {

               CommonCode.showExceptionMessage(npe);

           } catch (Exception e) {

               CommonCode.showExceptionMessage(e);

           }

       }

/

    //added by Naren

    public static java.util.Date convertOracleDateToJavaUtilDate(oracle.jbo.domain.Date oracleDate) {

        if (oracleDate == null)

            return null;


        java.sql.Date javaSqlDate = oracleDate.dateValue();

        long javaMilliSeconds = javaSqlDate.getTime();

        return new java.util.Date(javaMilliSeconds);

    }


    //added by Naren

    public static oracle.jbo.domain.Date addDayToOracleDate(oracle.jbo.domain.Date date, int days) {

        if (date != null) {

            Calendar c1 = Calendar.getInstance();

            c1.setTime(date.getValue());

            c1.add(Calendar.DATE, days);

            java.util.Date javaUtilDate = c1.getTime();

            long javaMilliseconds = javaUtilDate.getTime();

            java.sql.Date javaSqlDate = new java.sql.Date(javaMilliseconds);

            return new oracle.jbo.domain.Date(javaSqlDate);

        }

        return null;

    }

    

    public static long getDifferenceDaysBetweenTwoDates(oracle.jbo.domain.Date d1, oracle.jbo.domain.Date d2)

      {

        if(d1 != null && d2 != null)

        {

          return (d1.getValue().getTime() - d2.getValue().getTime())/(24 * 60 * 60 * 1000);

        }

        return 0;

      }


/

[15:57] NARENDRA ENAMALA

    public void frmDtChange(ValueChangeEvent vce) {
        Date d1;
        if(vce.getNewValue()!=null){
            d1 = (Date)vce.getNewValue();
        if (this.getId2().getValue() != null) {
            Date d2 = (Date)this.getId2().getValue();
            if (d1.getValue().after(d2.getValue())) {
                FacesContext context = FacesContext.getCurrentInstance();
                FacesMessage fm = new FacesMessage("From Date cannot be greater than To Date");
                fm.setSeverity(FacesMessage.SEVERITY_ERROR);
                context.addMessage(this.getId1().getClientId(), fm);
            }
        }
    }
    }


[15:57] NARENDRA ENAMALA

    public void toDtChange(ValueChangeEvent vce) {
        Date d1;
        if(vce.getNewValue()!=null){
            d1 = (Date)vce.getNewValue();
        if (this.getId1().getValue() != null) {
            Date d2 = (Date)this.getId1().getValue();
            if (d1.getValue().before(d2.getValue())) {
                FacesContext context = FacesContext.getCurrentInstance();
                FacesMessage fm = new FacesMessage("To Date cannot be less than From Date");
                fm.setSeverity(FacesMessage.SEVERITY_ERROR);
                context.addMessage(this.getId2().getClientId(), fm);
            }
        }
        }
    }


---- from dt and To date timestamp validations


       public void fromDateVce(ValueChangeEvent vce) {
            vce.getComponent().processUpdates(FacesContext.getCurrentInstance());
        
                /*  if (vce.getNewValue() != null) {
                
                        Row hdrRow = ReusableMethodsClass.getCurrentRowFromVO("PrCancelHdrVo1");
                        Timestamp frmDt = (Timestamp) hdrRow.getAttribute("PrFromDate");
                        Timestamp toDt = (Timestamp) hdrRow.getAttribute("PrToDate");
                
                    if (frmDt !=null && toDt != null && frmDt.compareTo(toDt)==1 ) {
                        CommonCode.errmsg("From Date should not be greater than To Date.", "");
                        return ;
                    }
                
                } */
                 oracle.jbo.domain.Date d1;
                       if(vce.getNewValue()!=null){
                      java.sql.Timestamp tmpFrom = (java.sql.Timestamp)getId1().getValue();
                       java.util.Date dateUtilFrom = new java.util.Date(tmpFrom.getTime());
                       System.out.println(vce.getNewValue()+":from vce.getNewValue() this.getId1().getValue():"+this.getId1().getValue()+" dateUtilFrom :"+dateUtilFrom);
                          d1 = (oracle.jbo.domain.Date)convertJavaUtilDateToOracleDate(dateUtilFrom); //vce.getNewValue();
                          System.out.println("from Job Domine Date D1 : "+d1);
                       if (this.getId2().getValue() != null) {
                           //oracle.jbo.domain.Date d2 = (oracle.jbo.domain.Date)this.getId2().getValue();
                           java.sql.Timestamp tmpTo = (java.sql.Timestamp)getId2().getValue();
                            java.util.Date dateUtilTo = new java.util.Date(tmpTo.getTime());
                           oracle.jbo.domain.Date d2 = (oracle.jbo.domain.Date)convertJavaUtilDateToOracleDate(dateUtilTo);
                           System.out.println("from Job Domine Date D2 : "+d2);
                           if (d1.getValue().after(d2.getValue())) {
                               FacesContext context = FacesContext.getCurrentInstance();
                               FacesMessage fm = new FacesMessage("From Date cannot be greater than To Date");
                               fm.setSeverity(FacesMessage.SEVERITY_ERROR);
                               context.addMessage(this.getId1().getClientId(), fm);
                           }
                       }
        
                   }
        }
    
    public void toDateVce(ValueChangeEvent vce) {
        vce.getComponent().processUpdates(FacesContext.getCurrentInstance());  
              oracle.jbo.domain.Date d1;
                    if(vce.getNewValue()!=null){
                        System.out.println(vce.getNewValue()+":to vce.getNewValue() this.getId2().getValue():"+this.getId2().getValue());
                       // java.sql.Date javaSqlDate = new java.sql.Date(vce.getNewValue());
                       // d1 = (oracle.jbo.domain.Date)this.getId2().getValue();
                        
                        java.sql.Timestamp tmpTo = (java.sql.Timestamp)getId2().getValue();
                         java.util.Date dateUtilTo = new java.util.Date(tmpTo.getTime());
                         System.out.println(vce.getNewValue()+":@to vce.getNewValue() this.getId1().getValue():"+this.getId1().getValue()+" dateUtilFrom :"+dateUtilTo);
                            d1 = (oracle.jbo.domain.Date)convertJavaUtilDateToOracleDate(dateUtilTo);
                            
                        System.out.println("to Job Domine Date D1 : "+d1);
                    if (this.getId1().getValue() != null) {
                       // oracle.jbo.domain.Date d2 = (oracle.jbo.domain.Date)this.getId1().getValue();
                       java.sql.Timestamp tmpFrom = (java.sql.Timestamp)getId1().getValue();
                        java.util.Date dateUtilFrom = new java.util.Date(tmpFrom.getTime());
                       oracle.jbo.domain.Date d2 = (oracle.jbo.domain.Date)convertJavaUtilDateToOracleDate(dateUtilFrom);
                        System.out.println(" @to Job Domine Date D2 : "+d2);
                        if (d1.getValue().before(d2.getValue())) {
                            FacesContext context = FacesContext.getCurrentInstance();
                            FacesMessage fm = new FacesMessage("To Date cannot be less than From Date");
                            fm.setSeverity(FacesMessage.SEVERITY_ERROR);
                            context.addMessage(this.getId2().getClientId(), fm);
                        }
                        
                         oracle.jbo.domain.Date currdate = getCurrentOracleDate();
                         System.out.println("currdate.getValue() :"+currdate.getValue()+" currdate :"+currdate);
                        if (d1.getValue().after(currdate.getValue())) {
                            FacesContext context = FacesContext.getCurrentInstance();
                            FacesMessage fm = new FacesMessage("To Date cannot be Future Date");
                            fm.setSeverity(FacesMessage.SEVERITY_ERROR);
                            context.addMessage(this.getId2().getClientId(), fm);
                        }
                    }
                    }
        
    
         
            
    }
    



   /** function receive java.util date and convert it to oracle date then return oracle date */
      public static Date convertJavaUtilDateToOracleDate(java.util.Date javaUtilDate)
      {
        if(javaUtilDate == null)
          return null;


       long javaMilliseconds = javaUtilDate.getTime();
        java.sql.Date javaSqlDate = new java.sql.Date(javaMilliseconds);
        return new oracle.jbo.domain.Date(javaSqlDate);
      }
    
    /** function will return current date as an oracle Date object */
      public static oracle.jbo.domain.Date getCurrentOracleDate()
      {
        return new oracle.jbo.domain.Date(oracle.jbo.domain.Date.getCurrentDate());
      }

/


    public static oracle.jbo.domain.Date castToJBODate(String aDate) {

        DateFormat formatter;

        SimpleDateFormat dateFormat;

        java.util.Date date;


        if (aDate != null) {


            try {


                //formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss ");

                dateFormat = new SimpleDateFormat("dd/MM/yyyy");

                //date = formatter.parse(aDate);

                date = (java.util.Date)dateFormat.parse(aDate);

                System.out.println("Sql Date:" + date.getTime() + " : " + date);

                java.sql.Date sqlDate = new java.sql.Date(date.getTime());

                java.sql.Date timestamp = new java.sql.Date(date.getTime());

                System.out.println("Date after :" + sqlDate);

                //oracle.jbo.domain.Timestamp jboDate = new oracle.jbo.domain.Timestamp(sqlDate);

                oracle.jbo.domain.Date jboDate = new oracle.jbo.domain.Date(timestamp);

                System.out.println("Jbo Date:" + jboDate);

                return jboDate;

            } catch (ParseException e) {

                e.printStackTrace();

            }


        }


        return null;

    }


    

Dealing With Dates in Java

 

Dealing With Dates in Java

Many developers sometimes face a problems when they dealing with dates. In java there are many types of dates as:

- java.util.Date
- java.sql.Date
- java.sql.Timestamp
- oracle.jbo.domain.Date
- oracle.jbo.domain.Timestamp

ADF developers sometimes need to:

- Get current date (oracle.jbo.domain.Date or  oracle.jbo.domain.Timestamp) to set attribute in ViewObject programmatically .

- Convert between dates types.

- Add/Subtract some days from date.

- Display current date on the screen (as string) in specific format.

- Display name of current day in this week (Saturday - Sunday - ...... - Friday).

- Get current time.

- Get current day.

- Get current month.

- Get current year.

- Compare between two dates.

- Get the number of days difference between two dates.

All these previous functions developers need to know so, I made a set of functions to help developers to deal with dates.

  /** function return current date time in format (dd / MM / yyyy - HH:mm:ss) */
  public static String getDisplayedDateTime()
  {
    Calendar cal = Calendar.getInstance();
    String dateFormat = "dd / MM / yyyy - HH:mm:ss";
    SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
    return sdf.format(cal.getTime());
  }

  /** function return current date or time depending on the format which you are want. Just send a format and the function will return the current date or time depending on the 
      format which you are sent (e.g. getDisplayedDateFormat("yyyy-mm-dd") it will return 2013-11-18 */
  public static String getDisplayedDateFormat(String dateFormat)
  {
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
    return sdf.format(cal.getTime());
  }

  /** function return the index of current day in this week (if today is Sunday it will retun 1 and if the today is Monday this function will return 2 and so on... 
      till Saturday it will return 7) */
  public static int getCurrentDayOfWeek()
  {
    Calendar cal = Calendar.getInstance();
    return cal.get(Calendar.DAY_OF_WEEK);
  }

  /** function return the name of current day in this week (Saturday - Sunday - ...... - Friday) */
  public static String getCurrentDayOfWeekName()
  {
    int numberOfDay = getCurrentDayOfWeek();
    if(numberOfDay == 7)
    {
      return "Saturday";
    }
    else if(numberOfDay == 1)
    {
      return "Sunday";
    }
    else if(numberOfDay == 2)
    {
      return "Monday";
    }
    else if(numberOfDay == 3)
    {
      return "Tuesday";
    }
    else if(numberOfDay == 4)
    {
      return "Wednesday";
    }
    else if(numberOfDay == 5)
    {
      return "Thursday";
    }
    return "Friday";
  }

  /** function return current day. ( e.g. today is 2013/11/18 this function will return 18) */
  public static int getCurrentDay()
  {
    Calendar cal = Calendar.getInstance();
    return cal.get(Calendar.DAY_OF_MONTH);
  }

  /** function return current month. ( e.g. today is 2013/11/18 this function will return 11) */
  public static int getCurrentMonth()
  {
    Calendar cal = Calendar.getInstance();
    return cal.get(Calendar.MONTH) + 1;
  }

  /** function return current year. ( e.g. today is 2013/11/18 this function will return 2013) */
  public static int getCurrentYear()
  {
    Calendar cal = Calendar.getInstance();
    return cal.get(Calendar.YEAR);
  }

  /** function will return current date as an oracle Date object */
  public static oracle.jbo.domain.Date getCurrentOracleDate()
  {
    return new oracle.jbo.domain.Date(oracle.jbo.domain.Date.getCurrentDate());
  }

  /** function will return current date and time as an oracle Date object */
  public static oracle.jbo.domain.Date getCurrentOracleDateTime()
  {
    return new oracle.jbo.domain.Date(new java.sql.Timestamp(System.currentTimeMillis()));
  }

  /** function will return current date and time as an oracle Timestamp object */
  public static oracle.jbo.domain.Timestamp getCurrentOracleTimestampDateTime()
  {
    return new oracle.jbo.domain.Timestamp(new java.util.Date().getTime());
  }

  /** function will return current time */
  public static String getCurrentTime()
  {
    return  getDisplayedDateFormat("HH:mm");
  }

  /** function receive oracle date and return true or false (if sent date greater than current date it will return true 
      but if sent date less than or equal current date it will return false */
  public static boolean isDateGreaterThanCurrentDate(oracle.jbo.domain.Date d)
  {
    if(d == null)
      return false;
 
    oracle.jbo.domain.Date currentDate = new oracle.jbo.domain.Date(oracle.jbo.domain.Date.getCurrentDate());
    if(d.compareTo(currentDate) > 0)
      return true;
 
    return false;
  }

  /** function receive two oracle dates and return true or false (if first date greater than second date it will return true 
      but if first date less than or equal second date it will return false */
  public static boolean isFirstDateGreaterThanSecondDate(oracle.jbo.domain.Date d1, oracle.jbo.domain.Date d2)
  {
    if(d1 == null && d2 == null)
      return false;
 
    if(d1 == null && d2 != null)
      return false;
 
    if(d1 != null && d2 == null)
      return true;
 
    if(d1.compareTo(d2) > 0)
      return true;
 
    return false;
  }

  /** function receive three oracle dates and return true or false (if first date greater than or equal second date and first date less than or equal third date it will return true. 
      otherwise it will return false */
  public static boolean isFirstDateBetweenTwoDates(oracle.jbo.domain.Date d1, oracle.jbo.domain.Date d2, oracle.jbo.domain.Date d3)
  {
    if(d1 == null || d2 == null || d3 == null)
      return false;
 
    if(d1.compareTo(d2) >= 0 && d1.compareTo(d3) <= 0)
      return true;
 
    return false;
  }

  /** function receive date and integer then add the integer to the date then return the result date.
      (e.g. if you send 2013/07/13 and 5 to this function the function will return 2013/07/18 */
  public static Date addDayToOracleDate(oracle.jbo.domain.Date date, int days)
  {
    if (date != null)
    {
      Calendar c1 = Calendar.getInstance();
      c1.setTime(date.getValue());
      c1.add(Calendar.DATE, days);
      java.util.Date javaUtilDate = c1.getTime();
      long javaMilliseconds = javaUtilDate.getTime();
      java.sql.Date javaSqlDate = new java.sql.Date(javaMilliseconds);
      return new oracle.jbo.domain.Date(javaSqlDate);
    }
    return null;
  }

  /** function receive two oracle dates then return the number of days difference between them. (e.g. if you send 2013/07/20 , 2013/07/13 to the function the function will return 7 */
  public static long getDifferenceDaysBetweenTwoDates(oracle.jbo.domain.Date d1, oracle.jbo.domain.Date d2)
  {
    if(d1 != null && d2 != null)
    {
      return (d1.getValue().getTime() - d2.getValue().getTime())/(24 * 60 * 60 * 1000);
    }
    return 0;
  }

  /** function receive oracle date and convert it to java.util date then return java.util date */
  public static java.util.Date convertOracleDateToJavaUtilDate(oracle.jbo.domain.Date oracleDate)
  {
    if(oracleDate == null)
      return null;
 
    java.sql.Date javaSqlDate = oracleDate.dateValue();
    long javaMilliSeconds = javaSqlDate.getTime();
    return new java.util.Date(javaMilliSeconds);
  }

  /** function receive oracle date and convert it to java.sql date then return java.sql date */
  public static java.sql.Date convertOracleDateToJavaSqlDate(oracle.jbo.domain.Date oracleDate)
  {
    if(oracleDate == null)
      return null;
 
    return oracleDate.dateValue();
  }

  /** function receive java.util date and convert it to oracle date then return oracle date */
  public static Date convertJavaUtilDateToOracleDate(java.util.Date javaUtilDate)
  {
    if(javaUtilDate == null)
      return null;
 
    long javaMilliseconds = javaUtilDate.getTime();
    java.sql.Date javaSqlDate = new java.sql.Date(javaMilliseconds);
    return new oracle.jbo.domain.Date(javaSqlDate);
  }

  /** function receive java.sql date and convert it to oracle date then return oracle date */
  public static Date convertJavaSqlDateToOracleDate(java.sql.Date javaSqlDate)
  {
    if(javaSqlDate == null)
      return null;
 
    return new oracle.jbo.domain.Date(javaSqlDate);
  }

  /** function receive java.util date and convert it to java.sql date then return java.sql date */
  public static java.sql.Date convertJavaUtilDateToJavaSqlDate(java.util.Date javaUtilDate)
  {
    if(javaUtilDate == null)
      return null;
 
    long javaMilliseconds = javaUtilDate.getTime();
    return new java.sql.Date(javaMilliseconds);
  }

  /** function receive java.sql date and convert it to java.util date then return java.util date */
  public static java.util.Date convertJavaSqlDateToJavaUtilDate(java.sql.Date javaSqlDate)
  {
    if(javaSqlDate == null)
      return null;
 
    long javaMilliseconds = javaSqlDate.getTime();
    return new java.util.Date(javaMilliseconds);
  }


public static String dateToStrig(String d){
        SimpleDateFormat sdf =     new  SimpleDateFormat("yyyy-MM-dd");
          Date d1=null;
          try {
              d1 = sdf.parse(d);
          } catch (ParseException e) {
              e.printStackTrace();
          }
        System.out.println(d1);
          SimpleDateFormat sdff = new SimpleDateFormat("dd-MM-yyyy");
          String date1 = sdff.format(d1);
          return date1;
    }

Latest Date conversion///
    /** function receive java.util date and convert it to oracle date then return oracle date */
    public oracle.jbo.domain.Date convertJavaUtilDateToOracleDate(java.util.Date javaUtilDate) {
        if (javaUtilDate == null)
            return null;

        long javaMilliseconds = javaUtilDate.getTime();
        java.sql.Date javaSqlDate = new java.sql.Date(javaMilliseconds);
        return new oracle.jbo.domain.Date(javaSqlDate);
    }

    /** function will return current date as an oracle Date object */
    public oracle.jbo.domain.Date getCurrentOracleDate() {
        return new oracle.jbo.domain.Date(oracle.jbo
                                                .domain
                                                .Date
                                                .getCurrentDate());
    }

    public java.util.Date convertStringToUtilDate(String dt) {
        SimpleDateFormat DateFor = new SimpleDateFormat("dd-MMM-yyyy");
        java.util.Date utilDate = null;
        try {
            utilDate = DateFor.parse(dt);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return utilDate;
    }

    /** function receive two oracle dates then return the number of days difference between them. (e.g. if you send 2013/07/20 , 2013/07/13 to the function the function will return 7 */
    public long getDifferenceDaysBetweenTwoDates(oracle.jbo.domain.Date d1, oracle.jbo.domain.Date d2) {
        if (d1 != null && d2 != null) {
            return (d1.getValue().getTime() - d2.getValue().getTime()) / (24 * 60 * 60 * 1000);
        }
        return 0;
    }
    
    public oracle.jbo.domain.Date convertJavaSqlDateToOracleDate(java.sql.Date javaSqlDate)
      {
        if(javaSqlDate == null)
          return null;
     
        return new oracle.jbo.domain.Date(javaSqlDate);
      }
    
    public java.util.Date convertSqlTimestampToUtilDate(java.sql.Timestamp sqlTimestamp){
    java.sql.Timestamp ts=sqlTimestamp;  
    java.util.Date date=ts;  
    return  date;            
    }  

    public oracle.jbo.domain.Date convertJavaSqlTimestampToOracleDate(java.sql.Timestamp javaSqlDate)
      {
        if(javaSqlDate == null)
          return null;
     
        return new oracle.jbo.domain.Date(javaSqlDate);
      }