cancel
Showing results for 
Search instead for 
Did you mean: 

Convert String in Date format

Former Member
0 Kudos

Hi.

I need to call a Bapi in java. The Bapi has a import parameter type Date. If , in Java, I've a variable type String. How Can i do the conversion?

Thanks

Accepted Solutions (1)

Accepted Solutions (1)

prashil
Advisor
Advisor
0 Kudos

Hi Guillermo,

Get the BAPI exporting parameter in the String value say "strDate".

Convert this string value in the date using the following code:

SimpleDateFormat sdf = new SimpleDateFormat(strDate);

Date returnDate = sdf.parse(date);

The returnDate will be in the Date format.

Regards,

Prashil

Former Member
0 Kudos

Prashil.

Is there a reason for you posting an identical response (to mine) in this thread, or do you have something more specific to provide to answer this question?

Regards,

Joseph

Answers (4)

Answers (4)

Former Member
0 Kudos

Guillermo,

It appears that a lot of responses to your question are now just repetition...

Has the initial answer provided been of any value for you?

If yes, please consider closing this question and rewarding points for a helpful answer!

Thanks in advance!

Regards,

Joseph

Former Member
0 Kudos

Thanks, I Solved the problem. I call the Bapi and I convert the date in String.

Former Member
0 Kudos

Hi

U can do it by using SimpleDateFormat class.

Try this

String dtstr="4-10-2007";

<b>try</b>{

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

Date dt=sdf.parse(dtstr);

} <b>catch</b>(ParseException ex) {

System.out.println(ex);

}

Hope this will help u

Regards,

Nithya

Former Member
0 Kudos

Nithya,

Same question that was asked to Prashil: is there a reason for you posting an identical response (to mine and now Prashil) in this thread, or do you have something more specific to provide to answer this question?

Regards,

Joseph

Former Member
0 Kudos

Hi Guillermo another way for converting date format......

Wherever u want to convert date simply call this method..

public String strDate(Date objDate)

{

Calendar objCal=Calendar.getInstance();

objCal.setTime(objDate);

int intDate=objCal.get(Calendar.DATE);

int intMonth=objCal.get(Calendar.MONTH)+1;

int intYear=objCal.get(Calendar.YEAR);

return ""intDate"."intMonth"."+intYear;

}

And another process

StringTokenizer objstr1=new StringTokenizer(objstr,"-");

String year=objstr1.nextToken();

String month=objstr1.nextToken();

String total=objstr1.nextToken();

StringTokenizer objstr3=new StringTokenizer(total," ");

String date=objstr3.nextToken();

String Time=objstr3.nextToken();

String objstr2=""date"/"month"/"+year;

wherever u want to add date simply add objstr2....

Hope this is helpful to u......

Regards...

Sumalatha.....

Former Member
0 Kudos

Hi Guillermo,

one approach is to use the <b>SimpleDateFormat</b> formatter object (Javadoc <a href="http://java.sun.com/javase/6/docs/api/java/text/SimpleDateFormat.html">here</a>) for your string value.

Here is a sample code:


public class StringToDate
{
    public static void main(String[] args)
    {
        DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
 
        try
        {
            Date today = df.parse("01/01/2007");            
            System.out.println("Jan 1st 2007 = " + df.format(today));
        } catch (ParseException e)
        {
            e.printStackTrace();
        }
    }
}

Hope this helps.

Regards,

Joseph