convert Date to String with J2ME

M

Molz

Hi,
How can I convert a Date object into a String using J2ME?
Obviously toString() method doesn't works and there aren't classes
SimpleDateFormat and DateFormat.

Can yuo help me?

Thanks
Bye
 
E

Erwin Moller

Molz said:
Hi,
How can I convert a Date object into a String using J2ME?
Obviously toString() method doesn't works and there aren't classes
SimpleDateFormat and DateFormat.

according to the API toString() does work.

Are you sure you are not getting a nullpointerException??
 
D

David Zimmerman

Erwin said:
Molz wrote:




according to the API toString() does work.

Are you sure you are not getting a nullpointerException??

Not all implementations use toString() to give a formatted version of
the date. Some just use the default Object.toString() which isn't
terriably useful.

OP: You'll have to do it by hand, IE format the day, month and year,
hour, minute and second and put them in a String as you need.
 
J

joe zobkiw

Hi,
How can I convert a Date object into a String using J2ME?
Obviously toString() method doesn't works and there aren't classes
SimpleDateFormat and DateFormat.

Can yuo help me?

Thanks
Bye

Date d = new Date();
System.out.println(d); // This works
System.out.println(d.toString()); // And this works
 
R

Roedy Green

Does BigDate work in J2ME?

I would think it would. If not, you have the source so you could prune
out the conversion methods that use Date and Timezone.

You could also just see the technique. It is not rocket science to put
left zeros on numbers and string them together with punctuation.
 
M

Molz

Not all implementations use toString() to give a formatted version of
the date. Some just use the default Object.toString() which isn't
terriably useful.

In fact :(
OP: You'll have to do it by hand, IE format the day, month and year,
hour, minute and second and put them in a String as you need.

Yes but how I can do?

In J2ME there aren't methods getYear() getMonth() and getDate()
 
M

Molz

Roedy Green said:
I would think it would. If not, you have the source so you could prune
out the conversion methods that use Date and Timezone.

You could also just see the technique. It is not rocket science to put
left zeros on numbers and string them together with punctuation.

Yes but BigDate haven't a constructor or an other method that takes a
Date object or a long (the # of milliseconds) as parameter to make a
BigDate object
Am I wrong?
 
M

Molz

Date d = new Date();
System.out.println(d); // This works
System.out.println(d.toString()); // And this works

Yes, they print

java.lang.object.Date@0

It is not really what I want.
They print the date only in java.sql.Date that rewrite method
toString(), but in J2ME there isn't this class
 
D

David Zimmerman

/**
* Create a string from the TimeOfDay portion of a time/date as
hh::mm::ss
* @param data The date/time as milliseconds since the epoch.
*/
public static String timeToString (long date)
{
Calendar c = Calendar.getInstance();
c.setTime(new Date(date));
int h = c.get(Calendar.HOUR_OF_DAY);
int m = c.get(Calendar.MINUTE);
int s = c.get(Calendar.SECOND);
String t = (h<10? "0": "")+h+":"+(m<10? "0": "")+m+":"+(s<10?
"0": "")+s;
return t;
}

/**
* Create a string from the date portion of a time/date as yyyy-mm-dd
* @param date the dat/time as milliseconds since the epoch
*/
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;
}
 
D

David Zimmerman

David said:
/**
* Create a string from the TimeOfDay portion of a time/date as
hh::mm::ss
* @param data The date/time as milliseconds since the epoch.
*/
public static String timeToString (long date)
{
Calendar c = Calendar.getInstance();
c.setTime(new Date(date));
int h = c.get(Calendar.HOUR_OF_DAY);
int m = c.get(Calendar.MINUTE);
int s = c.get(Calendar.SECOND);
String t = (h<10? "0": "")+h+":"+(m<10? "0": "")+m+":"+(s<10?
"0": "")+s;
return t;
}

/**
* Create a string from the date portion of a time/date as yyyy-mm-dd
* @param date the dat/time as milliseconds since the epoch
*/
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;
}

This ignores timezone issues, but that shouldn't be too hard to add
 
R

Roedy Green

Yes but how I can do?

In J2ME there aren't methods getYear() getMonth() and getDate()

What is your starting point?

Are you trying to get current time? Is there no System.timeInmillis?

Is there a Date class?

Sorry I have no idea what is missing in J2ME.
 
R

Roedy Green

Yes but BigDate haven't a constructor or an other method that takes a
Date object or a long (the # of milliseconds) as parameter to make a
BigDate object

It does now. There is a constructor that takes a Date and a TimeZone.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top