tags:

views:

713

answers:

5

I need to get the data from a datefield calender and be able to display it in a string and later store it in a recordstore. I tried the toString() method but i had an error once run.

        StartDate = new DateField("Start Date ", DateField.DATE);
        StartDate.setDate(Calendar.getInstance().getTime());

I now have this code

     public static String dateToString (long date)
    {

     Calendar c = Calendar.getInstance() ;

     c.setTime(new Date(date));
     int y = c.get(Calendar.YEAR);
     int m = c.get(Calendar.MONTH) + 1; 
     int d = c.get(Calendar.DATE);
     String t = (y<10? "0": "")+y+"-"+(m<10? "0": "")+m+"-"+(d<10?
     "0": "")+d;
     return t;  
      }

How can I change this in order for this to get the date from Startdate and make that t.?

If anyone could help!

A: 

There are some sample implementations that may help you get started, since it appears your runtime doesn't provide a useful toString implementation.

Hank Gay
A: 

Does this solve your problem?

...
  date = new Date();
  currentDate = new DateField("", DateField.DATE_TIME);
  currentDate.setDate(date);
  dateinstring = date.toString();
  System.out.println("Date And Time Is In String Format: "+dateinstring);
...
Peter
A: 

Try to use something like this


...
   Calendar c = Calendar.getInstance() ;

   c.setTime(new Date(date));
   int y = c.get(Calendar.YEAR);
   int m = c.get(Calendar.MONTH) + 1; 
   int d = c.get(Calendar.DATE);

   Date t = c.getTime();
   String str = t.format("YY-MM-dd");
...

A: 

First you need to change the parameter type of dateToString to Date: Public static String dateToString(Date date)

Then after StartDate.setDate(Calendar.getInstance().getTime(); add this line: String dateString = dateToString(StartDate.getDate());

Well, at least that works in my MIDlet. Hopefully this helps.

Regards,

wahyu a

A: 

you might wanna use this:

public String getDateFormat(Date inDate){

String stDate = null;
String dd = null;
String mm = null;
String yyyy = null;
Calendar cd = Calendar.getInstance();
cd.setTime(inputDT);
int iD = cd.get( Calendar.DAY_OF_MONTH );
if (iD < 10){ dd = "0" + String.valueOf(iD); }
    else { dd = String.valueOf(iD); }
int iM = cd.get( Calendar.MONTH )+1;
    if (iM < 10){ mm = "0" + String.valueOf(iM); }
    else { mm = String.valueOf(iM); }
int iY = cd.get( Calendar.YEAR );
    yyyy = String.valueOf(iY);
stDate = yyyy+"-"+mm+"-"+dd;
return stDate;

}

to get the 'long' variable become 'Date' use the Date(long date) from java.util.Date}

Pharmineous