Monday, October 22, 2012

How to achieve deepCopy() of an Object in Java


Subject :

    we might need to get a deep copy of a specific Java object, For eg : an EmployeeDTO ( POJO - Plain Old Java Object) may contain collection of other Object, Again each child object can have separate object internally . If  you want to clone the main object then you need to perform a deep copy ,

Solution


  public static Object deepCopy(Object pObject)
  {
    String methodName = "deepCopy(Object)";
    _logger.entering(CLAZZ_NAME, methodName);
    ObjectOutputStream mObjOutStrm = null;
    ObjectInputStream mObjInStrm = null;
    try
    {
      ByteArrayOutputStream mByteArrOutStrm = new ByteArrayOutputStream();
      mObjOutStrm = new ObjectOutputStream(mByteArrOutStrm);
      // serialize and pass the object
      mObjOutStrm.writeObject(pObject);
      mObjOutStrm.flush();
      ByteArrayInputStream bin =
        new ByteArrayInputStream(mByteArrOutStrm.toByteArray());
      mObjInStrm = new ObjectInputStream(bin);
      // return the new object
      return mObjInStrm.readObject();
    }
    catch (Exception ex)
    {
      _logger.logp(Level.FINER, CLAZZ_NAME, methodName,
                   "Exception in deepCopy = " + ex);
      return pObject;
    }
    finally
    {
      try
      {
        mObjOutStrm.close();
        mObjOutStrm.close();
      }
      catch (IOException ioe)
      {
        _logger.log(Level.FINER,
                    "IO Exception in Deep Copy while closing stream");
      }
      finally
      {
        _logger.exiting(CLAZZ_NAME, methodName);
      }
    }
  }

Friday, October 19, 2012

Date Conversions



- How to get Day of Week from a given Date ?


Calendar c = Calendar.getInstance();
c.setTime(yourDate);
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);


-> To convert Date String to java.util.Date based on given date/time format and timezone

    public static Date parse(String dateString,
                             String dateFormat,String pTimezone) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);
simpleDateFormat .setTimeZone(pTimeZone);
        simpleDateFormat.setLenient(false); // Don't automatically convert invalid date.
        return simpleDateFormat.parse(dateString);
    }


-> To convert a java.util.Date to oracle.jbo.domain.Date


  public static oracle.jbo.domain.Date toJboDate(Date pJavaDate)
  {
    return new oracle.jbo.domain.Date(new Timestamp(pJavaDate.getTime()));
  }

-> To convert Date String to java.util.Date based on given format and and timezone


  public String formatDateTime(Date pJavaDate, String pFormat,
                               TimeZone pTimeZone)
  {
    String formattedString = null;
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pFormat);


    simpleDateFormat .setLenient(false);
    simpleDateFormat .setTimeZone(pTimeZone);

    formattedString = simpleDateFormat.format(pJavaDate);
    return formattedString;
  }

Wednesday, October 10, 2012

How to declare a List by initializing a value

Just curious to share this info, Many times we might have a requirement to pass a List as a input parameter , Instead of declaring variable, and adding elements one by one to the list, The below tip may allows you to directly pass List without creating a variable and add the elements.


getEmployeeDetails(Arrays.asList("123","456",222"));

Arrays.asList() - will return a java.util.List