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;
  }

No comments:

Post a Comment