convert Date to String with J2ME

Discussion in 'Java' started by Molz, Sep 11, 2003.

  1. Molz

    Molz Guest

    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
     
    Molz, Sep 11, 2003
    #1
    1. Advertising

  2. Molz

    Erwin Moller Guest

    Molz wrote:

    > 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??

    >
    > Can yuo help me?
    >
    > Thanks
    > Bye
     
    Erwin Moller, Sep 11, 2003
    #2
    1. Advertising

  3. Erwin Moller wrote:
    > Molz wrote:
    >
    >
    >>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??
    >


    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.
     
    David Zimmerman, Sep 11, 2003
    #3
  4. Molz

    joe zobkiw Guest

    (Molz) wrote in message news:<>...
    > 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
     
    joe zobkiw, Sep 11, 2003
    #4
  5. Molz

    Roedy Green Guest

    On 11 Sep 2003 04:19:15 -0700, (Molz) wrote or quoted :

    >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.


    have at look at the toString method in BigDate. You can do it that
    way.

    http://mindprod.com/jgloss/bigdate.html

    It is very little more work that using SimpleDateFormat

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Sep 11, 2003
    #5
  6. Roedy Green wrote:
    > On 11 Sep 2003 04:19:15 -0700, (Molz) wrote or quoted :
    >
    >
    >>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.

    >
    >
    > have at look at the toString method in BigDate. You can do it that
    > way.
    >


    Does BigDate work in J2ME?
     
    David Zimmerman, Sep 11, 2003
    #6
  7. Molz

    Roedy Green Guest

    On Thu, 11 Sep 2003 21:44:12 GMT, David Zimmerman
    <> wrote or quoted :

    >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.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Sep 12, 2003
    #7
  8. Molz

    Molz Guest

    > 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()
     
    Molz, Sep 12, 2003
    #8
  9. Molz

    Molz Guest

    Roedy Green <> wrote in message news:<>...
    > On Thu, 11 Sep 2003 21:44:12 GMT, David Zimmerman
    > <> wrote or quoted :
    >
    > >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.


    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?
     
    Molz, Sep 12, 2003
    #9
  10. Molz

    Molz Guest

    (joe zobkiw) wrote in message news:<>...
    > (Molz) wrote in message news:<>...
    > > 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


    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
     
    Molz, Sep 12, 2003
    #10
  11. Molz wrote:
    > (joe zobkiw) wrote in message news:<>...
    >
    >> (Molz) wrote in message news:<>...
    >>
    >>>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.
    >>>


    /**
    * 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;
    }
     
    David Zimmerman, Sep 12, 2003
    #11
  12. David Zimmerman wrote:

    >
    >
    > Molz wrote:
    >
    >> (joe zobkiw) wrote in message
    >> news:<>...
    >>
    >>> (Molz) wrote in message
    >>> news:<>...
    >>>
    >>>> 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.
    >>>>

    >
    > /**
    > * 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
     
    David Zimmerman, Sep 12, 2003
    #12
  13. Molz

    Roedy Green Guest

    On 12 Sep 2003 01:53:50 -0700, (Molz) wrote or quoted :

    >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.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Sep 12, 2003
    #13
  14. Molz

    Roedy Green Guest

    On 12 Sep 2003 01:56:30 -0700, (Molz) wrote or quoted :

    >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.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Sep 12, 2003
    #14
  15. Molz

    Molz Guest

    David Zimmerman <> wrote in message news:<Hpi8b.20$>...
    > David Zimmerman wrote:
    >
    > >
    > >
    > > Molz wrote:
    > >
    > >> (joe zobkiw) wrote in message
    > >> news:<>...
    > >>
    > >>> (Molz) wrote in message
    > >>> news:<>...
    > >>>
    > >>>> 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.
    > >>>>

    > >
    > > /**
    > > * 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




    Thanks a lot.
    Bye
     
    Molz, Sep 13, 2003
    #15
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. robin
    Replies:
    0
    Views:
    876
    robin
    Jul 20, 2003
  2. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,302
    Michael Borgwardt
    May 30, 2004
  3. wgan
    Replies:
    7
    Views:
    606
    Roedy Green
    Jul 8, 2004
  4. Replies:
    8
    Views:
    793
    Boris
    May 1, 2007
  5. davyb
    Replies:
    7
    Views:
    217
    Gregory Brown
    Nov 18, 2005
Loading...

Share This Page